How do I search for patterns only at specific line or column positions in Vim?
Answer
/\%5l\%10cpattern
Explanation
Vim provides position-matching atoms that constrain where a pattern can match based on line numbers, column positions, or virtual columns. These atoms let you search within precise coordinates of your file — invaluable for structured data, fixed-width formats, or column-aligned text.
How it works
\%23l— matches only on line 23\%>10l— matches only after line 10\%<50l— matches only before line 50\%10c— matches only at column 10\%>5c— matches only after column 5\%V— matches only inside the Visual area
Example
Find TODO only between lines 10 and 50:
/\%>10l\%<50lTODO
Highlight column 80 (to check line length):
/\%80v.
Only characters at exactly column 80 are highlighted
Tips
\%Vis especially useful for substitutions within a visual selection- Use
\%>79v.to highlight all characters beyond column 79 - Combine with
:match::match ErrorMsg /\%>80v.\+/for persistent column warning \%'mmatches at the position of mark m