Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create folds with foldmethod indent in vim?

Tags:

vim

I am trying to learn how to use folds in vim. Folding manually works great but I would like to know how to fold via indents. I expect it to work like this:

+------
   +--- | //Place marker here and press 'zf'
   +---
+------

Will result in:

+------
<fold>
+------

But I just get

E350: Cannot create fold with current 'foldmethod'

Any ideas why I get this error?

Also, is there a way to create a fold in between curly braces?

like image 721
Edvin Avatar asked Jul 29 '16 14:07

Edvin


People also ask

How do I create a fold 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.

How do I expand all lines in Vim?

With this mapping, to create a fold, you can hit v to enter visual mode (or Shift-v to enter line-oriented visual mode), select some lines with the arrow keys, and hit spacebar to collapse the selected lines. To expand the lines, put the cursor over the fold and hit spacebar (in vim terminology, this deletes the fold).

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.


1 Answers

If you want an automatic fold based on the indentation you need to set the foldmethod option to indent value

:set foldmethod=indent

And that's the reason probably why you get that error. I suppose it is set like above.

zf{motion} or {Visual}zf Operator to create a fold. This only works when 'foldmethod' is "manual" or "marker". The new fold will be closed for the "manual" method.

you can check your foldmethod to confirm that by :set foldmethod?

whenever you are in a range of lines with the same indentation you need only to click on za so that block will be folded.

If you want to create manually your folds you need to set this time the foldmethod to manual.

Put your cursor on one of the curly braces and type zf% that will fold all the block between {}

like image 139
Meninx - メネンックス Avatar answered Nov 03 '22 19:11

Meninx - メネンックス