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

How do I delete a fold definition at the cursor without deleting the underlying text?

Answer

zd

Explanation

zd removes the fold definition at the cursor position — the text inside the fold is not deleted. This command is available when foldmethod is set to manual or marker. It is useful when you want to unfold a region permanently, or reorganize your fold structure without touching the buffer content.

How it works

  • zd — delete the fold directly under the cursor
  • zD — delete the fold and all nested folds inside it (recursive)
  • Only works with foldmethod=manual or foldmethod=marker
  • For marker folds, zd removes the {{{ / }}} fold markers from the text
  • For manual folds, zd removes the fold from Vim's internal fold list without modifying the buffer

Compare this to zo (open fold) and zc (close fold), which are temporary view changes. zd is a permanent structural change.

Example

With foldmethod=manual and a fold around lines 5–12:

+-- 8 lines: function foo() {-------------

With cursor on that fold line, pressing zd removes the fold:

function foo() {
  ...
}

The text is untouched; only the fold definition is gone.

Tips

  • Use zE to eliminate all folds in the window at once
  • After zd on a marker fold, the {{{ and }}} comment markers are deleted from the file
  • Pair with zf{motion} to create new folds and zd to remove them as your code structure evolves
  • :set foldmethod=manual to switch to manual folding if you're currently using another method

Next

How do I sort lines numerically so that 10 sorts after 2 rather than before it?