96

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.

0

6 Answers 6

102

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.
77

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.

2
  • I have a code which is written by C, So the courser in on the one of its functions. I press <kbd>esc</kbd> to go to the command so, after : , I write zc and nothing happen. Jul 7, 2014 at 9:26
  • 1
    @alex the zc you should not type after :. You type the set commands after the prompt you get when typing :. The zc you type when you can freely move the cursor, just like you would use zt to get the text the cursor is on to the top of the current screen.
    – Anthon
    Jul 7, 2014 at 9:35
18

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.

5

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
                        feature}
        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.

2
  • There is no need to use the vimrc, I never did that as I use folding only occasionally
    – Anthon
    Jul 7, 2014 at 9:18
  • @Anthon: Oh, of course. I will add more information in my answer.
    – cuonglm
    Jul 7, 2014 at 9:20
3

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: https://neovim.io/doc/user/options.html#'foldlevelstart'

3

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!

2
  • Not sure why you were down-voted, this was useful for me thanks!
    – chutsu
    Mar 5 at 15:01
  • 2
    This is more complicated than it needs to be. There is already a mapping (za) to toggle the current fold (close it if it's open and open it if it's closed). You can still map <space> to za if you want, of course.
    – dhakimian
    Mar 23 at 20:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .