44

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.

39

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.

  • 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. – Mohammad Reza Rezwani Jul 7 '14 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 '14 at 9:35
32

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

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.

3

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.