When searching for /a_string
and getting several matches, I can cycle through them with n
, but I'd like the editor window centered on each matching line so I don't have to move my eyes around all the time saccading for the match.
The same would be useful during search-replace with confirmation, where I want to cycle through each match and confirm or avoid replacement.
Is there any such option besides remapping n
to nzz
?
1 Answer
I don't believe there is a builtin setting to do what you want only for search. If you :set scrolloff=999
(or other arbitrarily high number) you will get the effect you are looking for but it doesn't turn off. In other words, the cursor line will be midway between the top and bottom of the window whether you're searching, editing, moving around in Normal mode, etc.
From :help scrolloff
:
Minimal number of screen lines to keep above and below the cursor. This will make some context visible around where you are working. If you set it to a very large value (999) the cursor line will always be in the middle of the window (except at the start or end of the file or when long lines wrap).
Try it out. Some people keep this on all the time. Maybe you'll like it, too. We might also be able to figure out a mapping/script that enables it just while searching, too, but if you don't want to mess around with a mapping with zz
I imagine you don't want to mess around with a mapping for this either.
Update: This is totally superfluous given that zz
should work fine but since I scripted it for kicks and someone might find something useful I give you...
Put something like this function and mapping in your vimrc file:
function! CenteredFindNext(forward)
" save the current value for later restore
let s:so_curr=&scrolloff
set scrolloff=999
try
if a:forward
silent normal! n
else
silent normal! N
endif
finally
" restore no matter what
let &scrolloff=s:so_curr
endtry
endfunction
:nnoremap <silent>n :call CenteredFindNext(1)<CR>
:nnoremap <silent>N :call CenteredFindNext(0)<CR>
nzz
so that you still haven
if you want to? Is there a reason why you don't want to remap tonzz
?nnoremap n nzz
doesn't work during replacement, which mapping do I have to use for this?:h :map-modes
I don't see something really related. I think (but I'd need to check the source code to be sure) that the keysy
,n
,a
etc are hard code in the substitue command. You should edit your question to include this specific case.