Highlighting a search term without moving the cursor

15

8

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?

Andrew Wood

Posted 2011-03-08T21:48:38.283

Reputation: 1 139

2Not exactly an answer, but I just use two backticks (``) to jump to where I was before. – Izkata – 2012-01-30T17:55:00.400

Answers

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.

lessthanideal

Posted 2011-03-08T21:48:38.283

Reputation: 670

I love this plugin! – Andrew Wood – 2012-08-02T17:18:32.350

15

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

Posted 2011-03-08T21:48:38.283

Reputation: 31 786

1You could avoid the calls to winsaveview(), winrestview() and the normal * by replacing let @/ = @/ with let @/ = expand("<cword>"). – garyjohn – 2011-03-09T16:05:01.230

@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 – 2011-03-09T22:59:23.987

13

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

user70801

Posted 2011-03-08T21:48:38.283

Reputation:

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

Posted 2011-03-08T21:48:38.283

Reputation: 29 085

1

In addition to:

 :set hlsearch

You may want to:

 : set noincsearch

therube

Posted 2011-03-08T21:48:38.283

Reputation: 1 296

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)

SergioAraujo

Posted 2011-03-08T21:48:38.283

Reputation: 211