1

In text editors like sublime text, atom and notepad++, when you select a word, all matches are highlighted automatically as shown in the following screenshot of my atom editor:

Automatically highlight the word permutation when selected

I use v and select text in vim but how can I obtain all highlight functionality?

3

TL;DR: Combine * and # with hlsearch

You could highlight by using match and <cword> via an autocommand on an event like CursorHold. But I’m going to show an alternative method.

  • * searches forward for the word under the cursor
  • # searches backward for the word under the cursor

Both have variants (g*, g#) that don’t restrict the searches to word-boundaries.

Use n and N to navigate.

By setting hlsearch, these searches will also highlight matches.

Why do this?

  1. It uses a vim search instead of arbitrary highlights, so you can actually navigate between matches
  2. It uses existing vim machinery and needs little-to-no setup
  3. The commands are good to learn anyway (and then you start to like gd, among others)

Disadvantage

This is does not work for “partial words”: if you visually-select part of a word and hit *, you won’t get the result you want.

One workaround is one of the many visual star plugins.

Another is to simply yank y and then search /<C-r>"<CR> (vim notation: type slash / to search, then Control-R quote to paste the selected text, then hit enter).

  • Why vim does has to be so hard? I hope all these learning commands and losing some features from other editors will be worth it. – as2d3 May 23 at 21:12
  • 1
    @AbhishekAgrawal I dont see it as losing a feature—fwiw, i dont feel like im missing anything. As far as worth it, any adjustment to workflow comes with cost. Switching to vim years ago paid off for me. – D. Ben Knoble May 23 at 21:23

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.