created 2001 · complexity basic · author Andrew Pimlott · version 5.7
It can be convenient to scroll down or up by one line, while not changing the cursor screen line.
You can scroll down in this way by typing
<C-e>j (Ctrl-e to scroll down to see more text at the bottom, and
j to move the cursor down so its screen line hasn't changed). An alternative is
1<C-d> (Ctrl-d to scroll down, with a count of
1 which sets the
'scroll' option to 1 so one line is scrolled).
A superior solution is to place the following in your vimrc. This script behaves better at the top and bottom of the buffer, and does not change the
'scroll' option. Press Ctrl-J to scroll down or Ctrl-K to scroll up, in normal and visual modes.
function! s:Saving_scroll(cmd) let save_scroll = &scroll execute 'normal! ' . a:cmd let &scroll = save_scroll endfunction nnoremap <C-J> :call <SID>Saving_scroll("1<C-V><C-D>")<CR> vnoremap <C-J> <Esc>:call <SID>Saving_scroll("gv1<C-V><C-D>")<CR> nnoremap <C-K> :call <SID>Saving_scroll("1<C-V><C-U>")<CR> vnoremap <C-K> <Esc>:call <SID>Saving_scroll("gv1<C-V><C-U>")<CR>
For PageDown and PageUp, see the comments in Page up/down and keep cursor position.
If you want to keep the cursor centered vertically, you can also set scrolloff to a large value (such as 50). This won't work if you want the cursor closer to the edge, but it has the benefit that it works with all movement commands.