Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: search and highlight but do not jump

Tags:

vim

search

The super star (*) key in Vim will search for the word under the cursor and jump forward to the next match. The user can jump to the next matches with the n key. If hlsearch is enabled, it will also highlight the matches.

I want to be able to press * and get the highlighted matches and be able to navigate the matches using the n key. However, I do not want Vim to jump to the next match when * is pressed, it should remain on the current word. Is there a way to do this?

like image 973
Ashwin Nanjappa Avatar asked Nov 23 '10 13:11

Ashwin Nanjappa


People also ask

How do I keep Vim searching?

Press n to search for the next occurrence or uppercase N to search in the opposite direction. The basic steps to perform a search in Vim are as follows: Press / . Type the search pattern.

How do I remove highlighted text in Vim?

By default, Ctrl L in Vim clears and redraws the screen.


1 Answers

I would map:

nnoremap * *`` 

Works exactly like you want, except that it adds a jump in the jump list. To prevent that you need:

nnoremap * :keepjumps normal! mi*`i<CR> 
like image 108
mb14 Avatar answered Oct 19 '22 11:10

mb14