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.
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.
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.
indent
The accepted answer, by @Anthon, describes how to set folding method to indent
; i.e. folding are defined by the level of indentations.
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:
foldlevel
by one.foldlevel
by one.foldlevel
to zero -- all folds will be open.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.
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.
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
.
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'
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
!
za
) to toggle the current fold (close it if it's open and open it if it's closed). You can still map
:
. You type theset
commands after the prompt you get when typing:
. Thezc
you type when you can freely move the cursor, just like you would usezt
to get the text the cursor is on to the top of the current screen. – Anthon Jul 07 '14 at 09:35