How do I limit a search pattern to a specific line range without using :global?
/\%>10l\%<20lTODO
Vim regex supports position atoms that can constrain where a match is allowed to occur.
/\%>10l\%<20lTODO
Vim regex supports position atoms that can constrain where a match is allowed to occur.
:smagic/\vfoo.+/bar/
If you prefer very-magic regex syntax but don't want to change global settings, :smagic is a targeted solution.
/\vfoo\zsbar\zebaz
When you need to target only a slice of a larger pattern, \zs and \ze are usually cleaner than building lookarounds.
/\S\zs\s\+$
A plain trailing-whitespace search like /\s\+$ also matches fully blank lines, which is noisy when you only want accidental spaces after real content.
:filter /pattern/ ls
When you have many open buffers, plain :ls output gets noisy fast.
/\%>80v\S
When you enforce line-length limits, visual guides show where the boundary is, but they do not help you jump directly to violations.
:%s/#\zs\d\+/\=printf('%04d', submatch(0))/g
For log files, changelogs, or issue references, you sometimes need fixed-width numeric IDs without touching surrounding syntax.
editing #editing #ex-commands #substitute #regex #text-processing
:s/\v(\S+)\s*=\s*(.*)/\=printf('%-20s = %s', submatch(1), submatch(2))/
When a line contains uneven key = value spacing, quick manual fixes are easy to get wrong.
:let @/='\V'.escape(@", '\\')
When you yank text that contains regex characters like .
:%s/\d\+/\=printf('%04d', submatch(0))/g
When you need fixed-width numeric fields, manually editing each number is slow and error-prone.
:s/\v\d+/\=printf('%04d', submatch(0))/g
Substitution expressions let Vim compute each replacement dynamically, which is ideal when plain capture groups are too limited.
/foo\zsbar\zebaz<CR>
Sometimes you need Vim to match a long structural pattern but only treat one piece of it as the actual match.
\%(pattern\)\@=
Vim's lookahead assertion \@= confirms that the current position is followed by a pattern — without including those characters in the match.
:%s/^\(.\+\)\n\1$/\1/
This substitute command detects pairs of identical adjacent lines and collapses them into one, using a back-reference to match the repeated content.
[[:alpha:]]
Vim's regex engine supports POSIX character classes inside bracket expressions, giving you locale-aware, readable alternatives to manual character ranges like [
\_.{-}
The \ modifier in Vim extends any character class or atom to also match newlines.
:s/\v(\w+)\s+(\w+)/\2 \1/
By using capture groups in a substitute command with very magic mode (\v), you can swap two adjacent words in a single operation.
\(pattern\)\@=
Vim's regex engine supports zero-width positive lookahead via the \@= operator.
s/pattern/\r/
In Vim substitutions, \r in the replacement string inserts a line break, creating a new line.
\_.
In Vim's regular expressions, .