In vim, is there a way to cancel a search *without* resetting the cursor position?
Asked Answered
vim
P

5

6

In vim, I've got the hlsearch and incsearch options turned on. When I start a search, it immediately goes to the first match. If I press Enter to accept the search, the result is highlighted and the cursor is positioned at the first match. If I press Esc to cancel, the cursor position is reset to wherever I was when I started the search.

Is there a way in this case to make Esc cancel the search but leave the cursor position on the first match? That way I'd end up in the right spot but without anything highlighted.

Thanks!

Pigfish answered 24/7, 2013 at 1:8 Comment(3)
One day I turned off hlsearch and I never went back. You might think you want it more than you actually do. You can always turn it back on temporarily if you really need to highlight something.Saddlebacked
There's a problem with your idea: if you cancel the search it means that you don't want to perform it. In that case there's no reason for the cursor to move to the first match: no search, no move.Bolling
A common mapping is to use <c-l> which redraws the screen to also clear highlighting: nnoremap <silent> <C-l> :noh<CR><C-l>. If your terminal can handle it mapping <esc> is also nice: nnoremap <silent> <esc> <esc>:noh<cr>Tap
R
3

I added this to my .vimrc:

:command C let @/=""

I like search highlights, I just don't like them to stick around, so when I'm done with it, I can just type :C and they go away!

Also I changed my highlights to underlines instead of selection-esq styled which is easier on my eyes, but still easy to pick out multiple matches on a page without having to blindly n over and over. Best of both worlds imo.

Rabbi answered 1/8, 2013 at 19:43 Comment(0)
S
2

You could use :nohl after hitting <Enter> to cancel the highlighting.

You could make it easy to switch between highlight mode and non-highlight mode by using a mapping:

" put this in your vimrc
nnoremap <Space> :set hls!<CR>

Alternately, you could use a mapping to clear out the current search term.

" put this in your vimrc
nnoremap <Space> :let @/=""<CR>
Sava answered 24/7, 2013 at 1:12 Comment(2)
Right, but doing this every time I search is a huge pain. I use search like this to move around a file -- goal is to make it as quick as possible.Pigfish
Devin Lane: You could use a mapping to make it easier.Sava
H
2

You can use :nohlsearch to temporarily remove the highlighting; from :h :nohlsearch:

:noh[lsearch]

Stop the highlighting for the hlsearch option. It is automatically turned back on when using a search command, or setting the hlsearch option.

You could easily map this to whatever you want, I happen to use <leader>h:

nmap <silent> <leader>h :nohlsearch<CR>

Of course, you must first hit Enter to exit search mode before you can hit the mapping, but it quickly becomes habitual to hit the mapping keys afterwards.

Hypocycloid answered 24/7, 2013 at 1:44 Comment(0)
L
1

No, it is not possible without pressing some extra keys. To leave cursor in the new position it is required to press Enter. Esc will move cursor back to the previous place.

To turn off possible bothering highlighting until next search the following mapping can be added to ~/.vimrc for Ctrl + L shortcut:

" nohlsearch, disable highlighting until next search
nnoremap <c-l> :noh<cr><c-l>

Do not remap Esc for turning off highlighting since it may bring glitches to Vim. See vimrc - Why does this <Esc> normal mode mapping affect startup? - Vi and Vim Stack Exchange for details.

Lengthen answered 2/4 at 15:20 Comment(0)
G
0

I making an assumption that you really want a quick way to toggle between highlighting the search pattern and not highlighting. I say this because pressing enter is one way to quit the search that keeps the cursor at the current position whereas escape quits in a way the cursor is reset to where it was before the search.

So to toggle highlighting (without using <leader>) you could put the following in your .vimrc file (I have a bunch of toggle Boolean values in my .vimrc file that all begin with \t):

nmap \th :set invhls<CR>

Then all you have to do to go from highlighting to not highlighting is:

\th

Other toggle mappings I use are for Booleans spell, number, ignorecase, paste and wrap.

Once you are more comfortable with vim you can start using <leader>.

Glede answered 24/7, 2013 at 2:35 Comment(9)
I'd rather not have to press extra keys to turn off a mode I didn't want to turn on in the first place (the search), but thanks for pointing out <leader> -- wasn't aware that existed.Pigfish
I’m not really sure what you mean “without using <leader>”, one could easily define <leader> to be \ , and then make your mapping as map <leader>th and it would be exactly the same as what you have…Hypocycloid
Do you just want to turn off hlsearch? You said you had it on, but it sounds like you do not like it. If you like it on when you are searching, but like it off after the search is complete, the best solution is a quick way to toggle the option. Am I missing something?Glede
@Andrew Marshall, I find the use of <leader> confusing to people learning vim. So I try to avoid its use in answers.Glede
@Glede I’d argue that you’re just misleading people into thinking it’s this mystical and complicated thing that one should avoid. Plenty of explanation is just a few keystrokes away at :h <leader>.Hypocycloid
@Glede Here's the thing: If I'm actually searching for something, then I want to see the highlight as I step through the results. But if the search immediately gets me to where I want, or close enough, I want to immediately switch to normal/edit mode with the cursor on the search term without the distracting highlight. If there was a way to make canceling the search just not reset the cursor position that'd be perfect.Pigfish
@Devin Lane I guess you want to see all the matches highlighted while searching. You should get the current search highlighted if nohls and incsearch are set. Is this correct?Glede
@Glede I don't get the search highlighted if I have nohls set.Pigfish
I get a different color highlight (in my case white instead of red) if I have incsearch and nohls turned on. Besides the color, the only other difference is only one pattern is highlighted (not all on the page). I cannot think of a setting that will get you seeing what I am seeing.Glede

© 2022 - 2024 — McMap. All rights reserved.