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

后端 未结 10 1593
醉梦人生
醉梦人生 2021-01-30 03:01

The main scrolling commands in Vim are:

  1. Ctrl-B and Ctrl-F, as well as PageUp and PageDown scrol
相关标签:
10条回答
  • 2021-01-30 03:20

    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>
    

    0 讨论(0)
  • 2021-01-30 03:20

    Shameless plug, but I created a plugin here that you can use to easily adjust the distance, speed, and duration of the scrolling animation: https://github.com/terryma/vim-smooth-scroll

    0 讨论(0)
  • 2021-01-30 03:20

    This combines many of these answers, and this is what I use.

    noremap <expr> <C-u> repeat("\<C-y> :sleep 10m<CR>", winheight('%')/2)
    noremap <expr> <C-d> repeat("\<C-e> :sleep 10m<CR>", winheight('%')/2)
    
    0 讨论(0)
  • 2021-01-30 03:23

    This isn't exactly smooth scrolling, but it's how I handle not losing context when jumping pages.

    set so=7

    'scrolloff' 'so' number (default 0) global
    {not in Vi}
    Minimal number of screen lines to keep above and below the cursor. This will make some context visible around where you are working. If you set it to a very large value (999) the cursor line will always be in the middle of the window (except at the start or end of the file or when long lines wrap). For scrolling horizontally see 'sidescrolloff'. NOTE: This option is set to 0 when 'compatible' is set.

    0 讨论(0)
  • 2021-01-30 03:30
    N <CR-E>
    N <CR-Y>
    

    ...where 'N' is the number of single lines you want to scroll.

    Not smooth in literal sense, but you keep the keyboard.

    0 讨论(0)
  • 2021-01-30 03:31

    What I do is I set the keyboard repeat to very fast, about 120 chars / second, and the delay small. Then I map to 4j and to 4k I navigate up and down source code using j and k which moves the cursor up and down nice and quick, pretty smooth. But here's the good part, and this works on Linux, not Windows. For a number of years now, X11's keyboard input works in such a way that when you press and hold j it obviously starts putting out j characters. But when you then keep holding down j and then also press the ctrl key, X11 starts putting out c-j characters without you having to re-press the j key. Then when you let go of the ctrl key and still keep on pressing j, X11 continues to put j's again. So j makes the cursor start moving nice and smooth downwards, and you can periodically hit ctrl without letting go of j to give it a boost, a jolt.

    Also, I do what Devin does, and I set scrolloffset to 5.

    Lastly, I swap ctrl and cap lock. The default position of the ctrl key is completely retarded (no offense intended). It makes you have to rotate your left hand. I almost never use caps lock, so I swap them. Then my left pink finger can reach the ctrl key without any yoga moves.

    These things have worked for me for years. I only use vim, never gvim.

    0 讨论(0)
提交回复
热议问题