0

Doing a search with / can be useful, however, it becomes harder to find things if the term you are searching for is a part of other words. I have a large text document with notes i've made about the BASH command language, and if i hit /cat, in reference to the cat command, it just takes me to some word with "cat" as a part of it, like locate.

During a search, is there a way to use the white space surrounding, preceding, or fallowing a term in order to find a word or sequence or characters? I've tried using double and single quotes with no luck. EOF ($) works, but that won't help find words in the middle of a document.

2
  • 3
    Try /\<cat\> and see :help \< and also :help *
    – mattb
    Commented Jul 8, 2022 at 20:19
  • Note that in a pattern $ is end-of-line (EOL), not end-of-file (EOF). As a range, however, $ is EOF.
    – D. Ben Knoble
    Commented Jul 10, 2022 at 18:09

1 Answer 1

1

Like mattb proposes you could search for cat as a word:

/\<cat\>

The \< and \> token match the word boundaries.

Remark: If you have cat selected and hit * this what Vim will do.

But you can also do the same using the following pattern.

/\v<cat>

To limit the number of backslash to type, you can switch on the magic mode using the \v token.

In magic mode the special tokens like <, >, +, (, ), ... don't need to be escaped.

More information about the magic mode using: help /magic

2
  • those both work, yet you could explain what's happening "under the hood" in order to make the answer better.
    – user8919
    Commented Jul 9, 2022 at 17:39
  • @thinksinbinary see :help \<
    – mattb
    Commented Jul 10, 2022 at 6:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.