Vim - zt will redraw the window with the cursor at the top. Can you redraw with it say, 4 lines down?

655 views Asked by At

Reading the manual with ':help z' shows a variety of commands which can redraw the window, e.g. z.to redraw with the cursor in the centre or zb to redraw with it at the bottom of the screen.

zt is nice but I would find it much more comfortable to be able to redraw with the cursor 25% of the way down the page (so that what I was just writing is still visible). Is there an easier way to accomplish this other than macro-ing it?

3

There are 3 answers

0
romainl On BEST ANSWER

You can use the 'scrolloff' option:

set scrolloff=4

See :help 'scrolloff'.

1
echristopherson On

Here's the mapping (assigned to <Leader>zt) I use to put a number of lines above the cursor line at the top of the window:

nnoremap <silent> <Leader>zt :<C-u>exec 'normal! ' . v:count . 'kzt' . v:count . 'j'<CR>

If you don't supply a prefix, it will put the line above the cursor at the top of the window.

0
Yosh On

Regarding @echristopherson 's answer, this mapping should work, too:

nnoremap zt zt3<C-y>

and for the default behaviour:

nnoremap zT zt " 3<C-y> is not applied because of the n`nore` map.