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?

lesnik
  • 451
  • 2
  • 4
  • 13

5 Answers5

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.

Rich
  • 31,891
  • 3
  • 72
  • 139
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
thakis
  • 261
  • 3
  • 3
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.

grodzik
  • 4,588
  • 19
  • 27
  • 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. – lesnik Sep 26 '18 at 09: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.

Naumann
  • 2,759
  • 1
  • 11
  • 16
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>
B Layer
  • 19,834
  • 2
  • 30
  • 57