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>
    let g:lessmode = 0
    let onoff = 'off'
    unmap d
    unmap j
    unmap u
    unmap k
  echohl Label | echo "Less mode" onoff | echohl None
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[edit | edit source]

Community content is available under CC-BY-SA unless otherwise noted.