Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to activate vim folding markers?

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.

like image 562
kfmfe04 Avatar asked Dec 24 '13 09:12

kfmfe04


People also ask

How does folding work in Vim?

Vim uses the same movement commands to define folds. Folding also works in visual mode. If you enter visual mode using v or V , then select a few lines of text using the movement keys, and type zf , Vim will create a fold comprising those lines. Another option is to specify a range in command mode.

How do I fold in Vim?

With the following in your vimrc, you can toggle folds open/closed by pressing F9. In addition, if you have :set foldmethod=manual , you can visually select some lines, then press F9 to create a fold. Here is an alternative procedure: In normal mode, press Space to toggle the current fold open/closed.

How do I expand all lines in Vim?

To expand the lines, put the cursor over the fold and hit spacebar (in vim terminology, this deletes the fold). (Side note: you may want to change the look of collapsed folds.

How do I save a fold in Vim?

The problem is that when you close Vim, your artfully folded code returns to its unfolded state. The solution is quite simple - when you are ready to save your folds run the :mkview command. This will save your folds in the current buffer to your viewdir ( :h viewdir ) depending on your environment.


2 Answers

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.

like image 195
aidan Avatar answered Sep 22 '22 08:09

aidan


If you only have a few files or if you just wish to control option(s) on a per file basis you may want to use modeline. My intro to folding came when I download a z-shell script and when I opened it, was surprised to find everything folded. Found something like this at the end of the file:

# vim:ts=4:sw=4:ai:foldmethod=marker:foldlevel=0:

Change commenting to match your code type, and insure there is a space before the word vim. As always a good place to start: :help modeline and :help folding. You may have to add set modeline to your .vimrc file if modeline was not set at build time.

like image 35
Friartek Avatar answered Sep 20 '22 08:09

Friartek