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

How do I restrict a search to a specific line-number window without selecting text first?

Answer

/\%>20l\%<40lTODO

Explanation

Vim's search engine can constrain matches by line number, which is useful when you want to scan a known section without touching the rest of the buffer. This avoids temporary folds, marks, or visual selections just to narrow scope. It is especially helpful during large-file triage where patterns are noisy outside a target region.

How it works

/\%>20l\%<40lTODO
  • / starts a forward search
  • \%>20l is a zero-width atom that only allows matches after line 20
  • \%<40l only allows matches before line 40
  • TODO is the actual text pattern you want

Together, the search only matches TODO on lines 21 through 39. Swap the numbers and pattern to fit your case.

Example

Imagine a 200-line config where deployment notes live in the middle block. You can jump through only that zone with:

/\%>80l\%<120ldeploy

This skips earlier/later deploy mentions and keeps n/N navigation focused on the bounded region.

Tips

  • Combine with \c or \C for case control, for example /\c\%>80l\%<120ldeploy
  • Use this with substitution ranges too when you need bounded edits instead of full-file changes

Next

How do I lazy-load cfilter from vimrc without interrupting startup when it is unavailable?