Avatar Hund

Linux, plaintext & minimalism

April 15, 2019

Getting started with folding in Vim

Folding in Vim means that you visually (and locally) fold parts of the document. Just the way you (hopefully) fold a towel before putting it in the closet to save space.

Visually hiding parts of the documents makes it easier to manage the documents, especially if they’re large.

A screenshot of the Vim editor demoing the folding feature
Here's an example of two identical lines with the second line folded.

As the title suggests, this is only to get you started. There’s a lot more to this subject than what this post will cover. I will only cover the basics to get you sold on one of the greatest features in Vim. :)


By default Vim doesn’t save your folds. Getting this part to work required a bit of trial and error with various settings, but I eventually got it to work in a sane way thanks to the help of the web.

Add this part to you configuration file ~/.vimrc and Vim will automatically save and restore your folds just the way you left them:

augroup AutoSaveFolds
  autocmd BufWinLeave,BufLeave,BufWritePost ?* nested silent! mkview!
  autocmd BufWinEnter ?* silent! loadview
augroup end

Save and source the settings with source ~/.vimrc (or just restart Vim) for the changes to apply.

The keybindings

Folding is easy, just remember that you need to be in visual mode to be able to create folds.

zf Create fold
zd Delete fold
zo Open fold
zc Close fold
zR Open all folds
zM Close all folds
zE Delete all folds

That’s all for this post. If you want to read more about folding, different folding methods and what not, I then suggest you reading the help topic about it in Vim with :help folding.

Tags: Vim