Wikia

Vim Tips Wiki

Changes: Combining move and scroll

Edit

Back to page

(Reword and merge in comments)
(Change <tt> to <code>, perhaps also minor tweak.)
 
Line 3: Line 3:
 
|previous=104
 
|previous=104
 
|next=106
 
|next=106
|created=September 3, 2001
+
|created=2001
 
|complexity=basic
 
|complexity=basic
 
|author=Andrew Pimlott
 
|author=Andrew Pimlott
Line 13: Line 13:
 
It can be convenient to scroll down or up by one line, while not changing the cursor screen line.
 
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 <tt><C-e>j</tt> (Ctrl-e to scroll down to see more text at the bottom, and <tt>j</tt> to move the cursor down so its screen line hasn't changed). An alternative is <tt>1<C-d></tt> (Ctrl-d to scroll down, with a count of <tt>1</tt> which sets the <tt>'scroll'</tt> option to 1 so one line is scrolled).
+
You can scroll down in this way by typing <code><C-e>j</code> (Ctrl-e to scroll down to see more text at the bottom, and <code>j</code> to move the cursor down so its screen line hasn't changed). An alternative is <code>1<C-d></code> (Ctrl-d to scroll down, with a count of <code>1</code> which sets the <code>'scroll'</code> 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 <tt>'scroll'</tt> option. Press Ctrl-J to scroll down or Ctrl-K to scroll up, in normal and visual modes.
+
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 <code>'scroll'</code> option. Press Ctrl-J to scroll down or Ctrl-K to scroll up, in normal and visual modes.
   
 
<pre>
 
<pre>

Latest revision as of 05:14, July 13, 2012

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

CommentsEdit

For PageDown and PageUp, see the comments in Page up/down and keep cursor position.


Around Wikia's network

Random Wiki