Tip 76: Stake the Boundaries of a Word
Return to the top: <<Practical Vim>>
-
/\v<{word}><CR> -
We can approximate the meaning of
<and>by combining the\wand\Wcharacter classes with the\zsand\zematch delimiters.-
<:\W\zs\w -
>:\w\ze\W-
\wmatches word characters, including letters, numbers, and the "_" symbol -
\Wmatches everything except for word characters.
-
-
- In a very magic search, the naked < and > characters are interpreted as word delimiters, but in magic, nomagic, and very nomagic searches we have to escape them.
References:
# If you open the file in vim, # then you can \"ayy@a the next line to execute the vim command: :h /\<
-
word delimiters are used automatically each time we trigger the
*or#commands.
References:
# If you open the file in vim, # then you can \"ayy@a the next line to execute the vim command: :h *
-
g*andg#perform the same searches without word delimiters.
Use Parentheses Without Capturing Their Contents
Example:
/\v(And|D)rew Neil
/\v%(And|D)rew Neil
replace all occurrences of FIRSTNAME LASTNAME with LASTNAME, FIRSTNAME
/\v(%(And|D)rew) (Neil) :%s//\2, \1/g