2

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
    Remapping another key to nzz so that you still have n if you want to? Is there a reason why you don't want to remap to nzz? – statox Sep 20 '17 at 9:22
  • nnoremap n nzz doesn't work during replacement, which mapping do I have to use for this? – oarfish Sep 21 '17 at 10:45
  • From your question, it wasn't clear that you wanted to have this mapping while using a substitute command. I don't think that is possible: in the mapping modes listed in :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 keys y, n, a etc are hard code in the substitue command. You should edit your question to include this specific case. – statox Sep 21 '17 at 11:33
  • 1
    Yes, I only realized that's what I wanted afterwards. Will edit. – oarfish Sep 21 '17 at 12:01
3

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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