7

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?

9

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.

5

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.

  • 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 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.

2

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>

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.