vimtricks.wiki Concise Vim tricks, one at a time.

How do I create a fold from a visual selection?

Answer

zf (in visual mode)

Explanation

In visual mode, pressing zf creates a manual fold from the selected lines. This lets you collapse sections of code or text.

How it works

  • Select lines with V and motions
  • Press zf to create a fold
  • The selected lines are collapsed into one line
  • zo opens the fold, zc closes it

Example

Select a function body with Vip, press zf to fold it. The fold shows something like:

+-- 15 lines: function process() {----

Tips

  • :set foldmethod=manual must be set for this to work
  • zd deletes a fold (unfolds it permanently)
  • zf{motion} creates a fold without visual mode
  • zR opens all folds, zM closes all folds
  • za toggles the fold under the cursor

Next

How do you yank a single word into a named register?