Vim Tips Wiki
(referenced other tips mentioned in the comments section in the text body (will actually merge them next time))
Kba (talk | contribs)
(changed description of VimTip239: *does* work in terminal, wikified keyboard shortcuts)
Line 5: Line 5:
 
|created=June 9, 2008
 
|created=June 9, 2008
 
|complexity=basic
 
|complexity=basic
|author=kb
+
|author=kba
 
|version=7.0
 
|version=7.0
 
|subpage=/200806
 
|subpage=/200806
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 <c-j> and <c-k> to scroll one line up and down keeping the cursor in the same position on screen)
+
*[[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)
*[[VimTip400|400 Fast scroll mappings]] (does the same as [[VimTip105]] but maps the keys to <Alt-h/j/k/l/>, also mappings for insert and visual mode. Mappings might clash with GUI Shortcuts, e.g. <A-h> opens help menu)
+
*[[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)
*[[VimTip239|239 Scroll using arrow keys like in a web browser]] (map <Shift-up> and <Shift-Down> to <c-e> and <c-y>. this probably works only in GUI unless mapped to another key combo)
+
*[[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)
   
 
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.

Revision as of 09:55, 13 September 2008

Tip 1571 Printable Monobook Previous Next

created June 9, 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.).

Comments