how to activate vim folding markers?

kfmfe04 picture kfmfe04 · Dec 24, 2013 · Viewed 8.5k times · Source

I have inherited some c++ code with vim-based folding markers like this:

// CONSTRUCTORS/DESTRUCTORS /*{{{*/
Foo::Foo()
{
}
Foo::~Foo()
{
}
/*}}}*/

What do I need to put into my .vimrc to enable folding toggles like zm and space-bar?

With my current settings, when I hit space bar inside or zm, vim does nothing.

Answer

Aidan Miles picture Aidan Miles · Dec 24, 2013

The default keybindings for folding are za or zm (though zm I think only closes folds, while za toggles them), so you should add the following lines to your .vimrc:

set foldmethod=marker to enable folding triggered by markers (the {{{ things in your code)

nnoremap <space> za to enable space to trigger the fold in normal mode.

But! if you're not sure if you want to enable folding in other files, you could use autocmds, like so:

autocmd FileType vim,c++,txt setlocal foldmethod=markerand that will ensure that folding only works in vim, c++, and text files.

By the way, what you've posted is only one kind of folding mentioned by vim guru Steve Losh in this article. Read it to learn more about folding. It's super cool.