如何在 vim 中退出 vimdiff 模式,特别是对于 Fugitive?
我使用带有 逃逸扩展 的 vim。它有一个 :Gdiff 命令,可以让您进入 vimdiff 模式,但是关闭/退出 vimdiff 模式的正确/快速方法是什么?
即,假设我正在 Git 存储库下编辑文件 FooBar.txt。我启动 :Gdiff,检查 vimdiff 中的更改,然后我想返回并继续编辑 FooBar.txt 或任何其他文件:)
更新 1: 我将给这些快速组合一个尝试下一个工作日:)
"vimdiff current vs git head (fugitive extension)
nnoremap <Leader>gd :Gdiff<cr>
"switch back to current file and closes fugitive buffer
nnoremap <Leader>gD :diffoff!<cr><c-w>h:bd<cr>
更新2:我当前的映射(仅关闭差异窗口!)
"vimdiff current vs git head (fugitive extension)
nnoremap <Leader>gd :Gdiff<cr>
"switch back to current file and closes fugitive buffer
nnoremap <Leader>gD <c-w>h<c-w>c
另外,请帮助我决定以下内容是否应该是一个答案:https://stackoverflow.com/a/15975201/275980
I am using vim with the fugitive extension. It has a :Gdiff command which brings you into vimdiff mode, but what is the right/quick way to close/quit vimdiff mode?
I.e., let's say I am editing the file FooBar.txt under Git repository. I fire up :Gdiff, review my changes in vimdiff, and then I want to get back and continue editing FooBar.txt or any other file :)
UPDATE1: I'm going to give these quick combos a try next working day :)
"vimdiff current vs git head (fugitive extension)
nnoremap <Leader>gd :Gdiff<cr>
"switch back to current file and closes fugitive buffer
nnoremap <Leader>gD :diffoff!<cr><c-w>h:bd<cr>
UPDATE2: My current mappings (closes diff window only!)
"vimdiff current vs git head (fugitive extension)
nnoremap <Leader>gd :Gdiff<cr>
"switch back to current file and closes fugitive buffer
nnoremap <Leader>gD <c-w>h<c-w>c
Also, please help me decide if the following should be an anwser: https://stackoverflow.com/a/15975201/275980
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
您可以执行windo set nodiff noscrollbind
,然后关闭第二个窗口。更新:有一个
diffoff
命令。使用windo diffoff
,而不是我在上一行中编写的内容。You can executewindo set nodiff noscrollbind
and then close the second window.Update: there is a
diffoff
command. Usewindo diffoff
, not what I wrote in previous line.根据:https://github.com/tpope/vim-fugitive/issues/36
According to: https://github.com/tpope/vim-fugitive/issues/36
我对
diffoff
没有什么运气,但我刚刚了解到,不带参数的:Gedit
会将您带回文件的工作目录版本,而不是之前的一些版本您正在审查的版本。由于
q
(不需要:q
)将关闭 diff 侧边栏,因此您可以执行q
,然后执行:Gedit 删除侧边栏,然后返回到文件的当前版本。
I had no luck with
diffoff
, but I just learned that:Gedit
with no argument will bring you back to the working-directory version of the file, as opposed to some earlier version you were reviewing.And as
q
(no need for:q
) will close the diff sidebar, you can doq
followed by:Gedit
to get rid of the sidebar and then go back to the current version of the file.上述解决方案都不适合我。最终改为这样做:
nnoremapD :Gedith :qk
None of the above solutions worked for me. Ended up doing this instead:
nnoremap <Leader>D :Gedit<CR><C-w>h :q<CR><C-w>k
这对我来说效果很好,结合了这里的一些现有想法:
This works fine for me, combining some of the existing ideas here:
如果您有多个窗口,则
的替代方法是移动到另一个 diff 窗口并执行c
,这会关闭只有一扇窗户。如果您关闭了错误的 diff 窗口,请执行
:Gedit
请小心,不要将< 混淆/代码>
c
与An alternative to
<C-W><C-O>
, if you have multiple windows, would be move to the other diff window and do<C-W>c
, which close only one window.If you close the wrong diff window do a
:Gedit
Be careful and don't confuse
<C-W>c
with<C-W><C-C>
我为此找到了一个简单的解决方案。您可以在这里查看:https://gist.github.com/radmen/5048080
I've found a simple solution for this. You can check it here: https://gist.github.com/radmen/5048080
在此处检查
vimdiff
在diffthis
和diffoff
之间切换在此页面。
代码:
Check the
vimdiff
toggling betweendiffthis
anddiffoff
hereat this page.
The code:
方法 1:
:windo diffthis
:windo diffoff
方法 2:
我建议仅使用最简单的命令:
:q< CR>
当你想快速完成时,添加映射:
它对
我来说效果很好。只需按
,q
即可退出 vimdiff,因为通常您的光标位于旧文件中。Method 1:
:windo diffthis
:windo diffoff
Method 2:
I recommend just using the most simple command:
:q<CR>
when you want to do it quickly, add the mapping:
and
It works well for me. Exit vimdiff just by
,q
, because normally your cursor in the old file.这就是我使用 :Gdiff 后必须离开 vimdiff 窗口的内容
this is what I have to leave the vimdiff windows after using :Gdiff
noremapdo :diffoff \| windows if & diff \|隐藏\| endif
完全 diff 模式并关闭其他 diff 窗口。 (注意:逃亡者将自动删除其隐藏缓冲区。)
noremap <leader>do :diffoff \| windo if &diff \| hide \| endif<cr>
Quite diff mode and close other diff windows. (Note: fugitive will auto delete its hidden buffers.)
我的函数可以在 diff 窗口和文件窗口中运行。但可能无法在打开多个差异的情况下自行处理。为此,您需要使用
fugitive#buffer(n).path()
进行扫描和匹配。添加键绑定:
My function will work both from diff window and file window. But probably won't handle itself with multiple diffs opened. For that you'll need to use
fugitive#buffer(n).path()
to scan and match.Add a key binding:
还有另一种方式。我在 fugitive.vim 中的内容 - 首先在 diff 启动时保存一些信息 (s:gitbufname):
然后将缓冲区切换窗口保留到保存的缓冲区并恢复:
Yet another way. What I have in fugitive.vim - first save some info (s:gitbufname) when diff starts:
and later when leaving the buffer switch window to the saved buffer and restore:
基于 https://stackoverflow.com/a/15113951/10999673 使用下面的代码:
但它给了我一个错误“E93:多于一个匹配...”在三向差异中,所以我改为使用 https:// stackoverflow.com/a/4867969/10999673 终于有了这个:
我刚刚调整、复制代码并将其粘贴到我的 .vimrc 中
Was using the code below based on https://stackoverflow.com/a/15113951/10999673 :
but it gave me an error "E93: more than one match for ..." in a 3 way diff, so i instead used the answer from https://stackoverflow.com/a/4867969/10999673 and finally have this:
I just tweaked, copied and pasted the code into my .vimrc
合并到满意程度后运行
:Gwrite
除了更新 git 缓存以将文件标记为已合并之外,还将关闭其他两个差异窗格。Running
:Gwrite
after merging to your satisfaction will close the other two diff panes in addition to updating the git cache to mark the file as merged.我采用以下方法:
无论您是否站在差异视图中,这都有效。它首先运行 :Git ,它将带你到逃亡的 git 缓冲区(如果你站在 diff 视图中)。然后它会使用 Cw Co 关闭所有其他缓冲区。最后,我们使用“:bd”卸载逃逸的 git 缓冲区,这将带您回到之前的位置,类似于当所有其他缓冲区都已关闭时站在 git 缓冲区中键入 gq 。
理想情况下,我希望它覆盖逃亡者的 gq 并使用快捷方式 gq 而不是 gx,但是我不知道如何做到这一点。我还希望它仅在处于 diff 缓冲区或 git 缓冲区之一时才起作用。
编辑:刚刚发现,如果您已经打开了多个缓冲区并且您想打开逃亡并执行一些 vim 操作,则此方法效果不佳。因此,更新了我的“打开逃亡者”快捷方式以在新选项卡中打开,所以现在我的关闭热键将我带回上一个选项卡,其中包含所有缓冲区,就像打开逃亡者之前一样。
编辑2:
现在,我在新选项卡中打开 git fugitive 窗口,我完全放弃了关闭热键,而是为关闭选项卡创建了绑定。
I went with the following:
This works whether you're standing in the diff view or not. It first runs :Git which takes you to the fugitive git buffer (in case you we're standing in the diff view). Then it closes all other buffers with C-w C-o. Finally we unload the fugitive git buffer with ":bd" which will take you back to where you were before, similar to typing gq when standing in the git buffer when all other buffers have been closed.
Ideally I want this to override the fugitive's gq and make the shortcut gq instead of gx, however I couldn't figure out how to do that. I also wanted this to work only when standing in either one of the diff buffers or the git buffer.
EDIT: Just found out that this works poorly if you have several buffers already open and you want to open fugitive and do some vim stuff. So updated my "open fugitive" shortcut to open in a new tab, so now my closing hotkey takes me back to the previous tab with all my buffers as they were prior to opening fugitive.
EDIT 2:
Now that I open the git fugitive window in a new tab I completely ditched my closing hotkey all together and instead made a binding for closing tabs.