5

While I enter a regular expression on the vim command line, I would like to see what matches the partial expression I already wrote.

For example, while writing the command

:%s/regular

all words regular should be highlighted, and the text should be scrolled so that the first of them is visible.

I think it is good enough if it does the highlight only for single line matches, because a match could easily multiple pages intermittently, even if I want to match only one line.

Multiple pages means potentially gigabytes. Of course a single line can be multiple gigabytes itself. The point is: it may be slow.

Is there a way to do this, or anything similar?

8

This is done by setting just two options: set is hls (incremental search and highlight all matches).

Make sure you've also read the appropriate help topics, excerpted below: :h 'is'

While typing a search command, show where the pattern, as it was typed so far, matches. The matched string is highlighted. If the pattern is invalid or not found, nothing is shown. The screen will be updated often, this is only useful on fast terminals.

Note that the match will be shown, but the cursor will return to its original position when no match is found and when pressing Esc. You still need to finish the search command with Enter to move the cursor to the match …

and :h 'hls'.

When there is a previous search pattern, highlight all its matches. The type of highlighting used can be set with the l occasion in the highlight option. This uses the "Search" highlight group by default. Note that only the matching text is highlighted, any offsets are not applied …

When you get bored looking at the highlighted matches, you can turn it off with :nohlsearch. As soon as you use a search command, the highlighting comes back …

Since Patch 8.1.0271, incsearch applies to :substitute, :global, and :vglobal (also known as :s, :g, and :v).

In Neovim incsearch still does not affect any command, but there exists another option called :h 'inccommand'. Currently it highlights all matches only for :h :substitute. Besides this, it can also show a preview window with matched lines: :set inccommand=split.

  • No, I do not mean in search as with / or ?, I mean for :, as in the example. – Volker Siegel Nov 26 '19 at 6:42
  • @VolkerSiegel Please read :h 'is' carefully. – Matt Nov 26 '19 at 6:43
  • I may miss something, but I do not find what you mean in :h 'is'. I already used incsearch and hlsearch, and they work fine. I have a highlight of the matched strings of a :%s/// after the execution and a following undo, which is pretty useful. What I mean is having that highlight incrementally. Do you think I should already have that? (I may have broken it somehow.) – Volker Siegel Nov 26 '19 at 6:55
  • 1
    @VolkerSiegel It was added in 8.1.0271 – Matt Nov 26 '19 at 7:16
  • 1
    I suggest instead of linking to vimdoc.sf.net (which isn't maintained anymore) to rather link to vimhelp.org. This way, you actually get the correct and updated documentation. – Christian Brabandt Nov 27 '19 at 9:19
3

Prior to version 8.1.0271, in order to preview searches in this way you have to adjust your workflow slightly.

First, set up your regular expression by using a normal search, during which highlighting will be active:

/regular<CR>

Then, you can re-use the regular expression from your search command by leaving the field blank in your substitute command:

:%s//replacement

An alternative method — if you want to include the actual expression in your command and don't mind a couple of extra keystrokes — is to actually insert the pattern into your :substitute command by hitting Ctrl-R/. This inserts the contents of the search register into the command line. See :help c_CTRL-R for more details.

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.