Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
2  
Not exactly an answer, but I just use two backticks (``) to jump to where I was before. –  Izkata Jan 30 '12 at 17:55

5 Answers 5

up vote 1 down vote accepted

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.

share|improve this answer
    
I love this plugin! –  Andrew Wood Aug 2 '12 at 17:18

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.

share|improve this answer

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 @/

share|improve this answer

In addition to:

 :set hlsearch

You may want to:

 : set noincsearch
share|improve this answer

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>
share|improve this answer
    
You could avoid the calls to winsaveview(), winrestview() and the normal * by replacing let @/ = @/ with let @/ = expand("<cword>"). –  garyjohn Mar 9 '11 at 16:05
    
@garyjohn: Yes, that is better. I was not sure there was something equivalent to the “word” selection done by *, but <cword> is it. –  Chris Johnsen Mar 9 '11 at 22:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.