Wikia

Vim Tips Wiki

Changes: Quickly switch between pager-like and editor-like scroll

Edit

Back to page

(changed description of VimTip239: *does* work in terminal, wikified keyboard shortcuts)
(Change <tt> to <code>, perhaps also minor tweak.)
 
Line 3: Line 3:
 
|previous=1570
 
|previous=1570
 
|next=1572
 
|next=1572
|created=June 9, 2008
+
|created=2008
 
|complexity=basic
 
|complexity=basic
 
|author=kba
 
|author=kba
Line 11: Line 11:
 
|category2=
 
|category2=
 
}}
 
}}
When reading text files, it can be convenient to switch to a pager-like scroll behavior (as with the '''less''' utility), where <tt>j</tt> and <tt>k</tt> scroll the document by one line, while keeping the cursor on the same buffer line. Of course you can scroll with Ctrl-e and Ctrl-y, but they require the control key.
+
When reading text files, it can be convenient to switch to a pager-like scroll behavior (as with the '''less''' utility), where <code>j</code> and <code>k</code> scroll the document by one line, while keeping the cursor on the same buffer line. Of course you can scroll with Ctrl-e and Ctrl-y, but they require the control key.
   
With the following in your [[vimrc]], you can press F5 to toggle scrolling on or off. When on, press <tt>j</tt> or <tt>k</tt> to scroll by one line, or <tt>d</tt> or <tt>u</tt> to scroll by half a page.
+
With the following in your [[vimrc]], you can press F5 to toggle scrolling on or off. When on, press <code>j</code> or <code>k</code> to scroll by one line, or <code>d</code> or <code>u</code> to scroll by half a page.
   
 
<pre>
 
<pre>
Line 46: Line 46:
   
 
If you prefer modeless solutions, check out
 
If you prefer modeless solutions, check out
*[[VimTip105|105 Combining move and scroll]] (which maps <tt>c-j></tt> and <tt><c-k></tt> to scroll one line up and down keeping the cursor in the same position on screen)
+
*[[VimTip105|105 Combining move and scroll]] (which maps <code>c-j></code> and <code><c-k></code> to scroll one line up and down keeping the cursor in the same position on screen)
*[[VimTip400|400 Fast scroll mappings]] (does the same as [[VimTip105]] but maps the keys to <tt><Alt-h/j/k/l/></tt>, also mappings for insert and visual mode. Mappings might clash with GUI Shortcuts, e.g. <tt><A-h></tt> opens help menu)
+
*[[VimTip400|400 Fast scroll mappings]] (does the same as [[VimTip105]] but maps the keys to <code><Alt-h/j/k/l/></code>, also mappings for insert and visual mode. Mappings might clash with GUI Shortcuts, e.g. <code><A-h></code> opens help menu)
*[[VimTip239|239 Scroll using arrow keys like in a web browser]] (map <tt><Shift-up></tt> and <tt><Shift-Down></tt> to <tt><c-e></tt> and <tt><c-y></tt>. This might not work on all terminals, though it does in xterm, konsole and rxvt-unicode on Linux)
+
*[[VimTip239|239 Scroll using arrow keys like in a web browser]] (map <code><Shift-up></code> and <code><Shift-Down></code> to <code><c-e></code> and <code><c-y></code>. This might not work on all terminals, though it does in xterm, konsole and rxvt-unicode on Linux)
   
 
There's also a tip ([[VimTip320|320 Page up/down and keep cursor position]]) to make vim behave like Borland IDE when using PageUp/PageDown, so that the cursor stays on the same column.
 
There's also a tip ([[VimTip320|320 Page up/down and keep cursor position]]) to make vim behave like Borland IDE when using PageUp/PageDown, so that the cursor stays on the same column.

Latest revision as of 06:34, July 13, 2012

Tip 1571 Printable Monobook Previous Next

created 2008 · complexity basic · author kba · version 7.0


When reading text files, it can be convenient to switch to a pager-like scroll behavior (as with the less utility), where j and k scroll the document by one line, while keeping the cursor on the same buffer line. Of course you can scroll with Ctrl-e and Ctrl-y, but they require the control key.

With the following in your vimrc, you can press F5 to toggle scrolling on or off. When on, press j or k to scroll by one line, or d or u to scroll by half a page.

function! LessMode()
  if g:lessmode == 0
    let g:lessmode = 1
    let onoff = 'on'
    " Scroll half a page down
    noremap <script> d <C-D>
    " Scroll one line down
    noremap <script> j <C-E>
    " Scroll half a page up
    noremap <script> u <C-U>
    " Scroll one line up
    noremap <script> k <C-Y>
  else
    let g:lessmode = 0
    let onoff = 'off'
    unmap d
    unmap j
    unmap u
    unmap k
  endif
  echohl Label | echo "Less mode" onoff | echohl None
endfunction
let g:lessmode = 0
nnoremap <F5> :call LessMode()<CR>
inoremap <F5> <Esc>:call LessMode()<CR>

Plugins like LessMode are available, but they may be more complex than you require.

If you prefer modeless solutions, check out

There's also a tip (320 Page up/down and keep cursor position) to make vim behave like Borland IDE when using PageUp/PageDown, so that the cursor stays on the same column.

Besides, if you don't like scrolling manually, make vim do it automatically for you as described in 1198 Automatic scrolling of text.

In conclusion: If you prefer the modal approach, then go with the LessMode() function, it's fast and doesn't require key combinations. If you prefer to stay in the mode you're in, use VimTip400, it should work on most GUIs and Terminals and does not require scroll/cusor movement combinations which might not work as expected in some cases (start of line, end of line, start of document etc.).

CommentsEdit

Around Wikia's network

Random Wiki