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 markerszv— "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 zRreopens all folds if you need the full view backzoopens one fold level at the cursor;zOopens recursively- Combine with
zz(center cursor) for a centered focused view:zMzvzz