I would like Vim to place my cursor vertically in the middle of screen after search. I have achieved that for *
, #
, n
and N
commands with the following lines in my vimrc
:
nmap * *zz
nmap # #zz
nmap n nzz
nmap N Nzz
My question is: How to map /
and ?
the same way? In other words, I would like to position the cursor in the same way after some text has been found using the following commands:
/some-text-to-find-forward
?some-text-to-find-backward
Edit: Threw away my initial answer as it was too much of a kludge. Here's a much better solution.
function! CenterSearch()
let cmdtype = getcmdtype()
if cmdtype == '/' || cmdtype == '?'
return "\<enter>zz"
endif
return "\<enter>"
endfunction
cnoremap <silent> <expr> <enter> CenterSearch()
The way this works is to remap Enter in command-line-mode to a custom expression.
The function performs the current search followed by zz if the command-line is currently in a search. Otherwise it just executes whatever command was being done.