Just in case someone still wondering about this, now you can use autocmd.
autocmd CmdlineEnter /,\? :cnoremap <CR> <CR>zz
autocmd CmdlineLeave /,\? :cunmap <CR>
It's basically remap enter
when in search mode and unmap if leave search mode. It was inpired by example from :help 'incsearch'
and you can use it without incsearch
.
EDIT:
If you don't want to to remap <CR>
or enter when you use search as motion, you can do something like this:
augroup incsearch_center
autocmd CmdlineEnter /,\?
\ if empty(v:operator) |
\ cnoremap <CR> <CR>zz|
\ endif
autocmd CmdlineLeave /,\?
\ if empty(v:operator) |
\ cunmap <CR>|
\ endif
augroup END
v:operator
is basically to check the last operator given in Normal mode such as c
, d
, and so on. For more info :help v:operator
If you want to use the accepted answer, you can use something like this:
cnoremap <expr> <CR> getcmdtype() =~ '[/?]' && empty(v:operator) ? '<CR>zz' : '<CR>'