Folding Paragraphs in Vim

Haskell, But Quickly

Heads Up!

I'm writing a book about building and deploying web applications with Haskell and Yesod. Want to know when it's released? Click here.

I find myself working with long, hairy text files surprisingly often. The translated strings for the Vaamo web app and the legacy database annotations for Oddjob are two examples. The format is always basically the same; chunks of related text together, separated by blank lines:


auth.login.failed=Bitte überprüfe Deine Eingaben.
auth.login.forgotPassword=Passwort vergessen?

error.iban.invalidChecksum=Die Prüfziffer ist leider nicht korrekt
error.max=Der Wert muss kleiner als {0} sein
error.min=Der Wert muss größer als {0} sein

Imagine instead of three lines per paragraph, it’s a hundred. I don’t want to see every single line; it’s too much noise. What I want to see is a high-level overview of the file so I have a chance at navigating through it. I want each paragraph to fold into its top line. I keep forgetting how to do this in vim, so I’m writing it here for future reference.

:set fde=getline(v:lnum)=~'^\\s*$'&&getline(v:lnum+1)=~'\\S'?'<1':1
:set fdm=expr

This first line sets the fold expression to one which handles paragraphs. The second line tells Vim to use the expression method for folding. There are a couple more fold expressions to be found in :help fold-expr. If I still find this painful in the future, it’s likely those lines will find their way into my ~/.vimrc.