17

Using Vim, I sometimes find myself staring at a section of source code for a while and suddenly want some variable on the screen to pop out. That's easy: /<var> which highlights them all. My issue is that it more often than not the search shifts my window so I'm not looking at the source code from the same place. Even if it's only shifted a few lines, it's still throws me off since I need to take a few seconds to figure out where things have moved to.

Is it possible to highlight a search term without moving the cursor to the next match?

Kazark
  • 3,509
Andrew Wood
  • 1,309

6 Answers6

16

skyblue’s answer shows the core of the idea of how to use the normal search highlighting to accomplish what you want.

I extended that technique by making a keybinding that works similarly to the * command, but without actually moving to the next match.

My thanks goes to garyjohn for pointing out the expand() equivalent to *’s word selection (to avoid using * itself and restoring the view). This simplifies the code enough so that it can go directly in the mapping (obviates using a function). I have also added a mapping that matches partial words (like g*).

:" The leader defaults to backslash, so (by default) this
:" maps \* and \g* (see :help Leader).
:" These work like * and g*, but do not move the cursor and always set hls.
:map <Leader>* :let @/ = '\<'.expand('<cword>').'\>'\|set hlsearch<C-M>
:map <Leader>g* :let @/ = expand('<cword>')\|set hlsearch<C-M>
Chris Johnsen
  • 42,029
12

You could also set the search register to the variable you want to match.

First, make sure search highlighting is turned on:

:set hlsearch

Then, you can set the search register with:

:let @/="variable"

This will highlight all occurrences of variable without jumping to the next match.

For more information, see :help @/

7

One way is to use the :match command, e.g.,

:match Question '^R^W'

That will highlight the word under the cursor using the Question highlight group. I often use the Question group because on my terminal it's a nice green. To see the available highlight groups, execute

:hi

The ^R and ^W are Ctrl-R and Ctrl-W, respectively, and are replaced by the word under the cursor. See

:help c_CTRL-R_CTRL-W

for more on that. See

:help :match

for more on the :match command. To clear that highlighting, simply execute

:match

with no arguments.

garyjohn
  • 36,494
1

In addition to:

 :set hlsearch

You may want to:

 : set noincsearch
therube
  • 1,476
1

http://vim.wikia.com/wiki/VimTip1572

If you used this plugin, you can simply enable the functionality

\m

then use a NumPad key to assign a colour to that word everywhere without moving your cursor.

1

You can use many different highlights at once for many different words, or more complex search patterns if you wish.

0

Similar to * we have:

[I ............................. it show where the word under the cursor appears

It is worth having a look at :h [I. there we find some interesting things related to your needs.

There is a similar question on stackoverflow where I put my answer (a bit more complete)