After each n
command to search for the next match I would like vi to automatically position the line at the top of the screen.
The zt
command will redraw vim so that the current line is at the top of the screen. From :h zt
:
*z<CR>*
z<CR> Redraw, line [count] at top of window (default
cursor line). Put cursor at first non-blank in the
line.
*zt*
zt Like "z<CR>", but leave the cursor in the same
column. {not in Vi}
So you can accomplish this with the following in your .vimrc
:
nnoremap n nzt
nnoremap N Nzt
xnoremap n nzt
xnoremap N Nzt
Although personally, I prefer leaving it in the middle, so I have the following instead:
nnoremap n nzz
nnoremap N Nzz
Use :set so=999
Using this, cursor will always be at middle line. So after each n
command to search for the next match vim automatically position the cursor at middle line of the screen.
This is advantageous as we can see the context around the search.
For more details see :help scrolloff
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With