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\%>20lis a zero-width atom that only allows matches after line 20\%<40lonly allows matches before line 40TODOis 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
\cor\Cfor case control, for example/\c\%>80l\%<120ldeploy - Use this with substitution ranges too when you need bounded edits instead of full-file changes