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

How do I close all folds and then open just the one containing the cursor to focus on it?

Answer

zMzv

Explanation

The zMzv sequence collapses every fold in the file and then re-opens only the one containing your cursor. This gives you a clean, focused view of the section you are working in while hiding everything else.

How it works

  • zM — close all folds in the file recursively, collapsing everything to the top-level fold markers
  • zv — "view" the cursor line: open the minimum number of folds needed so the cursor is visible, without opening any sibling or parent folds unnecessarily

The combination works because zM first reduces the file to a compact outline, and zv then selectively exposes exactly where you are — nothing more.

Example

Given a file with three top-level functions folded:

+-- 15 lines: function authenticate() --------
+-- 22 lines: function processPayment() ------
+-- 18 lines: function sendReceipt() ---------

With the cursor inside processPayment, pressing zMzv results in:

+-- 15 lines: function authenticate() --------
function processPayment() {
    // ... full body visible ...
}
+-- 18 lines: function sendReceipt() ---------

Tips

  • Map this to a key for quick refocusing: nnoremap <leader>z zMzv
  • zR reopens all folds if you need the full view back
  • zo opens one fold level at the cursor; zO opens recursively
  • Combine with zz (center cursor) for a centered focused view: zMzvzz

Next

How do I make Neovim restore the scroll position when navigating back through the jump list?