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

How do I keep each search match centered and unfolded as I jump with n?

Answer

nzzzv

Explanation

When you are stepping through many matches, plain n often lands with poor context and can hide the match inside a closed fold. Chaining nzzzv solves both issues in one repeatable keystroke: jump to the next hit, recenter the viewport, and open folds just enough to reveal the line. This is especially effective during code review, refactors, or log triage where you repeatedly inspect nearby context.

How it works

  • n jumps to the next match for the current / pattern
  • zz recenters the cursor line in the window
  • zv opens folds at the cursor position so the match is visible

Because this is a normal-mode sequence, it is easy to map to a key you already use for search navigation.

Example

1  function render() {
2    if (featureFlag) {
3      // ... many folded lines ...
4      renderWidget();
5    }
6  }

Search for render. With folds enabled, n may land on hidden lines. nzzzv lands on each result, centers it, and opens the fold containing that result so you can immediately inspect surrounding code.

Tips

  • Use the reverse variant Nzzzv for backward traversal
  • Works well with :set hlsearch because the next hit remains obvious
  • If you rely on this heavily, map n and N to include zzzv in your config

Next

How do I launch a GDB session in Vim with the built-in termdebug plugin?