13

I am searching some pattern, navigate between search results, and at the same time I want some other pattern to be highlighted. For example, I jump between invocations of some function in my project, usually there is a variable named "session" around and I want this word to be highlighted.

What I do is:

:match StatusLineTerm /session/ 

where StatusLineTerm is just a name of existing highlight group having the color I like.

The question is: how to create some key combination that would call this match command using the word under cursor as a match pattern? Something like nnoremap <F5> :match StatusLineTerm /.expand(<cword>)/, but actually working?

0

5 Answers 5

10

For a quick solution, try this:

:nnoremap <F5> :match StatusLineTerm /<C-R><C-W>/<CR> 

This uses Ctrl-RCtrl-W to insert the word under the cursor into the command line. See :help c_CTRL-R_CTRL-W.

7

Here's how you do this automatically after keeping your cursor still for a short time:

:au CursorHold * :exec 'match Search /\V\<' . expand('<cword>') . '\>/' 

By default, this will highlight the word under the cursor after 4s of inactivity. Use :set updatetime=100 to make it happen after 0.1s instead.

If you want to do this in a script instead of as a one-off, put it in an autogroup so that you don't add a new CursorHold autocommand every time the script runs:

augroup highlight_current_word au! au CursorHold * :exec 'match Search /\V\<' . expand('<cword>') . '\>/' augroup END 
3
  • 2
    Welcome to Vi and Vim! You might want to show this with an autogroup, since that is best practice. Commented Jun 1, 2020 at 23:27
  • I definitely would not want to do it, but thanks a lot, it's cool trick to know about! Commented Jun 2, 2020 at 6:52
  • @D.BenKnoble Good idea, done. Commented Jun 2, 2020 at 23:13
6

I use a plugin for that: interesting words. It's not big, since it's only feature is to highligh words with defined or random colors, advantage is that you can have more than one color/highligh under single key. You can easly clear all the highlights done with that plugin if needed as well.

1
  • Not exactly what I want (f.e. I no NOT want to navigate between the interesting words), but it's something to investigate or may be use. Commented Sep 26, 2018 at 9:02
5

I use the mark.vim-plugin for this. It can handle many interesting words at the same time and allows to jump between highlighted words.

3

You indicate that you were trying to get the mapping to work with <cword>. Though the answer you accepted is just fine I'm surprised no one answered with a corrected use of that.

Normally the string on the RHS of a mapping is executed literally as an Ex command. No pre- expansion/evaluation/processing of the string occurs. So :match in

:nnoremap <F5> :match StatusLineTerm /.expand(<cword>)./ 

(along the lines of your attempt) will try to literally match the string '.expand(<cword>).'

If we want the string or some portion of it to be expanded/evaluated we have to do it ourselves by passing it to the :exec command as an expression:

:exec 'match StatusLineTerm /' . expand('<cword>') . '/' 

Note that we surround with quotes any parts that we want to use literally and append them with .. The rest is evaluated. (Also note that <cword> is a special string and needs to be quoted before being passed to expand().)

So, the mapping you were originally going for is:

:nnoremap <F5> :exec 'match StatusLineTerm /' . expand('<cword>') . '/'<CR> 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.