Wiki

by yszheda

View project onGitHub

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 \w and \W character classes with the \zs and \ze match delimiters.
    • <: \W\zs\w
    • >: \w\ze\W
      • \w matches word characters, including letters, numbers, and the "_" symbol
      • \W matches 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* and g# 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