Tip 77Stake the Boundaries of a Word

When defining a pattern, specifying where a word begins and ends can be useful. Vim’s word-delimiter items let us do that.

Some words, especially short ones, have a habit of showing up inside other words. For example, “the” appears inside “these,” “they,” “their,” and many other words besides. So if we search the following excerpt by running /the<CR>, we’ll find more matches than we may have bargained for:

 the problem with these new recruits is that
 they don't keep their boots clean.

If we specifically want to match “the” as a word rather than a fragment, we can use word boundary delimiters. In very magic searches, these are represented by the < and > symbols. So if we amended our search to /\v<the> ...

Get Practical Vim, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.