Skip to content

Instantly share code, notes, and snippets.

@druckdev
Created October 31, 2024 23:04
Show Gist options
  • Save druckdev/a95e8db9501458f65a08e44850d31fd2 to your computer and use it in GitHub Desktop.
Save druckdev/a95e8db9501458f65a08e44850d31fd2 to your computer and use it in GitHub Desktop.
vim: Matching inside/outside of the visual selection

Matching inside the visual selection

To search for word inside of the selection:

/\%Vwor\%Vd

\%V matches with zero with if inside the visual selection. The second \%V makes sure that the whole word is inside of the selection. Otherwise it would also match when only the beginning is inside.

Matching outside the visual selection

To search for word outside of the selection:

/\%V\@!wor\%V\@!d

The selection matcher from above is negated with \@!. Here again the same construct is needed at the end to make sure that not only the beginning but also the ending of the word are outside the selection.

This has one gotcha:

Since the pattern only states that the first and last character have to be outside the selection, word would still be matched if the selection spanned only the or. So to make sure that the full word is outside, one would need to add \%V\@! before every character of the word.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment