Vim Tips Wiki

Combining move and scroll

1,612pages on
this wiki
Revision as of 05:14, July 13, 2012 by JohnBot (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Tip 105 Printable Monobook Previous Next

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
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.

Around Wikia's network

Random Wiki