I want to have the very first search result centered on the screen. I know about :nnoremap n nzz mapping, but it only works for centering next matches, not the first.I thought that maybe I could use some AutoCmd, but I haven't found an appropriate event.
How can I automatically center first search result?
search;autocmd
You can use the following mapping:cnoremap <expr> <CR> getcmdtype() == '/' ? '<CR>zz' : '<CR>'Which can be explained like this:cnoremap        Create a mapping in the command line mode<expr>          The mapping will evaluate an expression (allowing to use conditions)<CR>            The key to remap (Enter)getcmdtype()    Return a character indicating which command line mode is used== '/'          Did getcmdtype() returned a / which means we are in search mode?'<CR>zz'        If we are in search mode execute zz after pressing <CR>'<CR>'          Else execute only <CR>Related help topics::h expr1 for the X ? Y : Z syntax:h :cnoremap:h :map-<expr>:h getcmdtype()EDIT Note that it would be interesting to create a mapping which would work with both forward and backward search i.e. when getcmdtype() return / or ?. Here is the mapping to do that:cnoremap <expr> <CR> (getcmdtype() == '?' \|\| getcmdtype() == '/') ? '<CR>zz' : '<CR>'Note the escaping of the | characters: if they are not escapes vim will understand cnoremap <expr> <CR> (getcmdtype() == '?' as the first command to execute followed by getcmdtype() == '/') ? '<CR>zz' : '<CR>' which doesn't make sense and create an error.EDIT 2 @Antony suggested a more readable solution, thanks to him:cnoremap <expr> <CR> getcmdtype() =~ '[/?]' ? '<CR>zz' : '<CR>'