Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to do smooth scrolling in Vim?

The main scrolling commands in Vim are:

  1. Ctrl-B and Ctrl-F, as well as PageUp and PageDown scroll by full page
  2. Ctrl-U and Ctrl-D scroll half a page by default
  3. Ctrl-Y and Ctrl-E scroll one line

I lose visual context every time for the former two, so I have developed the bad habit of hitting the latter (Ctrl-Y and Ctrl-E) repetitively.

Since there is currently no first party support for smooth scrolling, what are the least objectionable workarounds/plugins?

I use both Vim and GVim depending on the task, and am happy to customize them separately if there is no one really good hack that works for both. The mouse scroll wheel works nicely in GVim, but I'm looking for keyboard based solutions.

like image 922
Andrew Wagner Avatar asked Oct 31 '10 19:10

Andrew Wagner


2 Answers

Update: I have now pushed this code, refactored somewhat according to the guidelines at :help write-plugin, to a Github repo.

Using the Keyboard

Here is what I have in my .vimrc:

function SmoothScroll(up)     if a:up         let scrollaction="^Y"     else         let scrollaction="^E"     endif     exec "normal " . scrollaction     redraw     let counter=1     while counter<&scroll         let counter+=1         sleep 10m         redraw         exec "normal " . scrollaction     endwhile endfunction  nnoremap <C-U> :call SmoothScroll(1)<Enter> nnoremap <C-D> :call SmoothScroll(0)<Enter> inoremap <C-U> <Esc>:call SmoothScroll(1)<Enter>i inoremap <C-D> <Esc>:call SmoothScroll(0)<Enter>i 

Features:

  • Scroll on the base of the Vim scroll option.
  • Customizable scrolling speed (adjust time argument of the sleep command; I use ten milliseconds). Note: just like slowing down the frame rate on a video, if you slow down the smooth scroll too much it will be jerky scroll, not smooth scroll. But whatever works best for you.
  • Works in normal or insert mode.

Note: all you copy-and-pasters, remember that the ^ character indicates a control character; copy-paste will produce invalid results and these must be entered manually!

  • ^YCTRL-V then CTRL-Y
  • ^ECTRL-V then CTRL-E

However, the <C-U> and <Enter> style syntaxes are literally typed as those characters; the map command intelligently converts them to control characters.

Using the Mouse

The question mentions that scrolling with the mouse works well in GVim, but a keyboard solution is desired. This implies to me that the asker may be interested in a mouse solution if it works in regular terminal Vim.

For me, turning mouse support on allows smooth scrolling through the mouse wheel. Also, for me, smooth scrolling is most important when I am looking around (i.e. in normal mode), not when I am editing (in insert mode), and if I am not actively editing, the need for my hands to stay on the keyboard at all times is removed, so this works well.

On the basis of this question, though, it would seem that some people have to do some more manual setup beyond simply turning the mouse on (I just use set mouse=n):


My .vimrc has the following lines

 set mouse=a  map <ScrollWheelUp> <C-Y>  map <ScrollWheelDown> <C-E> 

like image 128
Keith Pinson Avatar answered Sep 21 '22 21:09

Keith Pinson


There is a simple remap hack in vim's tips.txt:

Smooth scrolling                    *scroll-smooth*  If you like the scrolling to go a bit smoother, you can use these mappings:      :map <C-U> <C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y>     :map <C-D> <C-E><C-E><C-E><C-E><C-E><C-E><C-E><C-E><C-E><C-E><C-E><C-E><C-E><C-E><C-E><C-E> 
like image 39
Andrew Wagner Avatar answered Sep 19 '22 21:09

Andrew Wagner