简体   繁体   English

Vim:如何滚动光标线呢?

[英]Vim: How to scrollbind the cursor line too?

Two windows in a split windows view of Vim can be bound to each other for scrolling by setting :set scrollbind for both of them. Vim的分割窗口视图中的两个窗口可以通过设置相互绑定进行滚动 :set scrollbind为它们:set scrollbind After this is done, if I scroll down the cursor in one windows, the other windows scrolls down along with it. 完成此操作后,如果我在一个窗口中向下滚动光标,其他窗口将随之向下滚动。

But, the cursors in the two windows are not bound to each other. 但是,两个窗口中的游标并没有相互绑定。 I have used :set cursorline to highlight the current line on which the cursor is present. 我使用过:set cursorline来突出显示光标所在的当前行。 How do I make the highlighted cursorline to scroll simultaneously in the other window too? 如何使突出显示的光标线同时在另一个窗口中滚动?

I don't use this myself, and I may be missing the point, but 我自己不使用它,我可能会忽略这一点,但是

:set cursorbind

seems to do exactly what you want. 似乎完全按照你的意愿行事。

I just encountered the same situation (with only two windows to compare), and what I did was: 我刚遇到同样的情况(只有两个窗口要比较),我做的是:

:set cursorbind
:set scrollbind
:set cursorline

and then 然后

:map <Down> j<C-w><C-w><C-w><C-w>
:map <Up> k<C-w><C-w><C-w><C-w>

This causes every press on the up or down arrows to switch back and forth between the windows once, which updates the cursor position on the other window (You can :redraw!<CR> instead of the ugly window switching but that looks bad when moving too fast). 这会导致向上或向下箭头上的每次按下在窗口之间来回切换一次,这会更新另一个窗口上的光标位置(您可以:redraw!<CR>而不是丑陋的窗口切换,但在移动时看起来很糟糕太快)。 Of course, there are many ways to move between lines without the arrows, so this is far from perfect. 当然,有许多方法可以在没有箭头的线条之间移动,所以这远非完美。

However, since this is not a main mode of work for me, useful primarily for comparing files on which diff is too complicated to look at for whatever reason, I find this setting to be practically good enough. 但是,由于这对我来说不是主要的工作模式,主要用于比较diff太大而无法查看的文件,我觉得这个设置实际上已经足够好了。

I came up with this solution when I wanted to do a "cross-window column tracking" in a vimdiff session (in additional to "cross-window row tracking"). 当我想在vimdiff会话中进行“跨窗口列跟踪”时(除了“跨窗口行跟踪”之外),我想出了这个解决方案。 Probably this would solve your problem as well. 也许这也可以解决你的问题。

First enable cursorcolumn , cursorbind , cursorline and scrollbind on each window. 首先允许cursorcolumncursorbindcursorlinescrollbind每个窗口上。

:windo set cursorcolumn | set cursorbind | set cursorline | set scrollbind

Then map the hjkl motion keys to update the cursor position for each movement. 然后map hjkl运动键以更新每个运动的光标位置。

:nmap h h:let curwin=winnr()<CR>:keepjumps windo redraw<CR>:execute curwin . "wincmd w"<CR>
:nnoremap j j:let curwin=winnr()<CR>:keepjumps windo redraw<CR>j:execute curwin . "wincmd w"<CR>
:nmap k k:let curwin=winnr()<CR>:keepjumps windo redraw<CR>:execute curwin . "wincmd w"<CR>
:nmap l l:let curwin=winnr()<CR>:keepjumps windo redraw<CR>:execute curwin . "wincmd w"<CR>

I found this page in the vim documentation thought you might find it helpful: http://vimdoc.sourceforge.net/htmldoc/scroll.html#scroll-binding 我在vim文档中发现这个页面以为您可能会觉得它有用: http//vimdoc.sourceforge.net/htmldoc/scroll.html#scroll-binding

Sometimes I find that straight from the horses mouth is the best. 有时我发现从马口直接是最好的。

I made a tiny change to cychoi 's answer . 我对cychoi的答案做了一点微小的改动。 Remap h,j,k,l in the diff mode and remove a 'j' hit in cychoi 's original mapping: 在diff模式下重新映射h,j,k,l并删除cychoi原始映射中的'j'命中:

if &diff
  au VimEnter * windo set cursorcolumn | set cursorbind | set cursorline | set scrollbind
  nmap h h:let curwin=winnr()<CR>:keepjumps windo redraw<CR>:execute curwin . "wincmd w"<CR>
  nmap j j:let curwin=winnr()<CR>:keepjumps windo redraw<CR>:execute curwin . "wincmd w"<CR>
  nmap k k:let curwin=winnr()<CR>:keepjumps windo redraw<CR>:execute curwin . "wincmd w"<CR>
  nmap l l:let curwin=winnr()<CR>:keepjumps windo redraw<CR>:execute curwin . "wincmd w"<CR>
endif

I think this setting is good for CSV line-by-line diff. 我认为这个设置适用于CSV逐行差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM