0

Sometimes I just want to see whether a regex is matching the right stuff. If I could cause Vim to visually select the matching text, it would be one way to do this. Is there a method for doing this?

1
4

Vim will use a colour for search matches if you have the 'hlsearch' option switched on:

:set hlsearch

You can then just search for your regular expression and see if it is highlighted:

/regex

Note that, if you also have 'incsearch' set, Vim will begin highlighting matches as soon as you start typing in your regular expression: with that option disabled the highlighting won't occur until you execute the search by pressing Enter.

The Search highlighting group is used for highlighting. You can check how this group is configured with the command:

:highlight Search

When you are done, you can remove the highlighting with the command:

:nohlsearch

Note the difference between this and :set nohlsearch, which turns off search highlighting entirely: the :nohlsearch command leaves the hlsearch option on, and will only turn off highlighting until the next time you search.

See :help 'hlsearch' for more details.

2
  • 1
    for visually selecting a regex, the gn command has been added – Christian Brabandt Jan 15 '18 at 17:17
  • @ChristianBrabandt As far as I can tell from the question and later comment, by "visually select" the OP means "highlight", rather than "make a visual-mode selection". (The OP has already rejected an answer suggesting gn, and I don't understand what is meant by the first sentence of the question if the OP really means "make a visual-mode selection"). – Rich Jan 15 '18 at 17:36
3

This can be achieved with gn.

See :h gn for details.

2
  • I know how to not match. The problem is how to activate the visual selection. – Tyler Durden Jan 14 '18 at 5:39
  • 1
    Not sure what you mean. gn activates the visual selection here (for the next match) – Naumann Jan 14 '18 at 6:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.