How to enable and use code folding in Vim?

How can I enable and do code folding in Vim?

Do I have to change anything in ~/.vimrc?

I type z+a and z+c and z+o and nothing happens.

Here is an article about folding: Code folding in Vim.

No you don’t have to put the command from the page you linked to in your ~/.vimrc, you can just type them after issuing : in vim to get the command prompt.

However if you put the lines:

set foldmethod=indent   
set foldnestmax=10
set nofoldenable
set foldlevel=2

as indicated in the link you gave, in your ~/.vimrc, you don’t have to type them every time you want to use folding in a file. The set nofoldenable makes sure that when opening, files are “normal”, i.e. not folded.

Answered By: Anthon

You can enable folding in current session like @Anthon’s answer. But if you want make it permanent, you must setting at least this line in .vimrc to folding work:

set foldmethod=indent

indent is kind of folding, you can see more from :help foldmethod

'foldmethod' 'fdm'      string (default: "manual")
                        local to window
                        {not in Vi}
                        {not available when compiled without the +folding
        The kind of folding used for the current window.  Possible values:
        fold-manual     manual      Folds are created manually.
        fold-indent     indent      Lines with equal indent form a fold.
        fold-expr       expr        'foldexpr' gives the fold level of a line.
        fold-marker     marker      Markers are used to specify folds.
        fold-syntax     syntax      Syntax highlighting items specify folds.
        fold-diff       diff        Fold text that is not changed.

Now, everytime you open a file with vim, you can see the code is folded by the method you was set. Then you can use za, zc, zo.

Answered By: cuonglm

You don’t have to use it systematically: I usually manually select folds by the motion or section. For example, folding a paragraph is zfip and folding the next 20 lines is zf20j. Use za to toggle and zd to remove.

This requires a little more work but allows your folding to reflect the task at hand.

Answered By: Marmaduke

Fold by default

Vim’s default folding method is manual meaning that the folds are created manually; otherwise, there is no fold to be closed or opened using za, zo, or zc as you described. But, you can create a fold by zf{motion} in Normal mode or zf in Visual mode; e.g. zfj creates a fold for current line and the next following one in Normal mode.

Fold by indent

The accepted answer, by @Anthon, describes how to set folding method to indent; i.e. folding are defined by the level of indentations.

Fold by syntax

In a more convenient way, folds can be created automatically based on the language syntax of the current buffer. If you are using a programming language, let’s call it L, and you have folding definition of L (e.g. you have installed a Vim plugin in which the folding information of L is defined; such as c.vim for C/C++, or python-mode for Python), you just need to set folding method to syntax:

set foldmethod=syntax

That’s it. The most useful commands for working with folds are:

  • zo opens a fold at the cursor.
  • zShift+o opens all folds at the cursor.
  • zc closes a fold at the cursor.
  • zm increases the foldlevel by one.
  • zShift+m closes all open folds.
  • zr decreases the foldlevel by one.
  • zShift+r decreases the foldlevel to zero — all folds will be open.
Answered By: cartoonist

If anyone wants to change default fold method and stop auto-folding when new file opens, Add this code in your vim config file below:

setlocal foldmethod=syntax         
setlocal foldlevelstart=99

setlocal foldmethod is to change the default Manual fold method and foldlevelstart to stop auto-folding when the files are opening.
You can change the value to customize the auto-folding level. check the documentation here:’foldlevelstart’

Answered By: Mejan

The answers above are wonderful. Thanks for helping me!
Why not add this one:

nnoremap <space> @=((foldclosed(line('.')) < 0) ? 'zc' : 'zo')<CR>

Much more convenient way: using space instead of z + c and z + o!

Answered By: Daniel Hu
Categories: Answers Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.