在 Vim 中进行平滑滚动的最佳方法是什么? [英] What is the best way to do smooth scrolling in Vim?

查看:60
本文介绍了在 Vim 中进行平滑滚动的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Vim 中的主要滚动命令有:

The main scrolling commands in Vim are:

  1. Ctrl-BCtrl-F,以及 PageUpPageDown 滚动整页
  2. Ctrl-UCtrl-D 默认滚动半页
  3. Ctrl-YCtrl-E 滚动一行
  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

我每次都失去前两者的视觉上下文,所以我养成了敲后者的坏习惯(Ctrl-YCtrl-E) 重复.

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?

我根据任务同时使用 Vim 和 GVim,如果没有一个真正适用于两者的真正好的 hack,我很乐意分别定制它们.鼠标滚轮在 GVim 中运行良好,但我正在寻找基于键盘的解决方案.

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.

推荐答案

更新:我现在已经推送了这段代码,根据 :help write-plugin,到 Github 存储库.

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

这是我在 .vimrc 中的内容:

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

特点:

  • 在 Vim scroll 选项的基础上滚动.
  • 可定制的滚动速度(调整 sleep 命令的时间参数;我使用十毫秒).注意:就像降低视频的帧率一样,如果平滑滚动速度减慢太多,则会出现抖动滚动,而不是平滑滚动.但无论哪种方式最适合您.
  • 在正常或插入模式下工作.
  • 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 然后 CTRL-Y
  • ^ECTRL-V 然后 CTRL-E
  • ^YCTRL-V then CTRL-Y
  • ^ECTRL-V then CTRL-E

但是, 样式语法按字面意思输入为这些字符;map 命令智能地将它们转换为控制字符.

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

问题提到在 GVim 中使用鼠标滚动效果很好,但需要键盘解决方案.这对我来说意味着提问者可能对鼠标解决方案感兴趣,如果它在常规终端 Vim 中工作.

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.

基于这个问题,不过,似乎有些人不得不做一些更多的手动设置只需打开鼠标(我只是使用 set mouse=n):

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):

我的 .vimrc 有以下几行

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

这篇关于在 Vim 中进行平滑滚动的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆