Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put the search results at the top of the screen in vi

Tags:

vim

vi

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.

like image 465
KenH Avatar asked Aug 24 '17 16:08

KenH


2 Answers

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
like image 175
DJMcMayhem Avatar answered Oct 13 '22 01:10

DJMcMayhem


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

like image 20
arun pal Avatar answered Oct 13 '22 01:10

arun pal