DEV Community

Jonas Schumacher
Jonas Schumacher

Posted on • Edited on

Search for word boundaries in Vim & VSCode Vim

So you know that / will bring up search in Vim and VSCode Vim. That's good!

But then you named a variable is and now want to find every occurence of that word - but only where it stands alone. So no places where it occurs as part of your isLargePizza() function for example.

Luckily, that's doable: \< matches the beginning of a word and \> the end.

So hitting ...

/\<is\>
Enter fullscreen mode Exit fullscreen mode

... will to the trick!

Edit:

See this comment by @pbnj for additional tips relating to word boundary search.

Oldest comments (3)

Collapse
 
pbnj profile image
Peter Benjamin (they/them) • Edited

Nice.

See :help # and :help * for additional related features.

For example:

  • Place the cursor on a word and press #, vim will perform the same bounded word search going backward. Equivalent to ?\<word\>.
  • Place the cursor on a word and press *, vim will perform the same bounded word search going forward. Equivalent to /\<word\>.

This can be combined with other ways of searching in vim. For example: :vimgrep /\<word\>/ % will perform bounded search for all instances of word and load the results in :help quickfix-window. From there, you can navigate through the results with :copen, :cnext, :cprev, to open the list, go to next result, or go to previous result respectively.

Collapse
 
jonasmerlin profile image
Jonas Schumacher

These are great tips, thanks!

Collapse
 
jonasmerlin profile image
Jonas Schumacher

I added a reference to your comment to the end of the post.