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

How do I create a fold that spans the current paragraph in manual fold mode?

Answer

zfap

Explanation

Combining the fold-creation operator zf with the ap (around paragraph) text object gives you a quick way to collapse any paragraph into a single fold line. This is faster than counting line numbers or entering visual mode just to create a fold.

How it works

  • zf is the fold-creation operator — it works with any motion or text object
  • ap is the "around paragraph" text object, selecting the current paragraph including its trailing blank line
  • Together, zfap folds the entire paragraph the cursor is in
  • Requires set foldmethod=manual (or using expr/marker methods that accept manual folds)

Example

With the cursor inside a prose paragraph or a code block separated by blank lines:

Before:
  This is the first line of a long
  paragraph that goes on for several
  lines and would benefit from folding.

After zfap:
  +-- 3 lines: This is the first line... ---

The fold is created instantly without entering visual mode.

Tips

  • zfip (inner paragraph) excludes the trailing blank line — use when you want the blank line visible
  • zf{motion} works with any motion: zf5j folds 5 lines down, zf/end<CR> folds to the next occurrence of "end"
  • zd deletes the fold at the cursor; zD deletes recursively
  • za toggles the fold open/closed; zM closes all folds in the file
  • Store folds with :mkview and restore them with :loadview

Next

How do I create new files and directories directly inside Vim's built-in file browser without leaving Vim?