英文:
Defining a shortcut only in vimdiff
问题
I have been using vimdiff in its vimdiff1
layout, where it opens side by side the two buffers of LOCAL and REMOTE. The reason I preferred this layout over the other standard layout is the two shortcuts do
and dp
for getting and putting diff chunks respectively.
现在,我一直在使用vimdiff1
布局,它会将LOCAL和REMOTE的两个缓冲区并排打开。我之所以喜欢这种布局,是因为它提供了两个快捷键:do
用于获取差异块,dp
用于放置差异块。
Yet, recently I came across the scenario in which I often had to take a mixture of the two chunks, for which manual editing to the MERGED file has been easier.
然而,最近我遇到了一个情况,我经常需要混合这两个块,对于这种情况,手动编辑MERGED文件更容易。
Now, I would like to switch to a LOCAL,MERGED,REMOTE
layout, and I was wondering if it's possible to define custom keybindings only for vimdiff.
Namely, I'd like to define two gl
and gr
shortcuts for :diffget LOCAL
and :diffget REMOTE
.
现在,我想切换到LOCAL,MERGED,REMOTE
布局,我想知道是否可以为vimdiff定义自定义按键绑定。具体而言,我想定义两个快捷键gl
和gr
,分别用于执行:diffget LOCAL
和:diffget REMOTE
。
Is there a way to define a keybinding only for the vimdiff 'mode'?
Or alternatively, are there already standard keybindings which I'm not aware of?
For reference: https://git-scm.com/docs/vimdiff/en
是否有办法仅为vimdiff的“模式”定义按键绑定?
或者,是否已经存在标准的按键绑定,我不知道?
供参考:https://git-scm.com/docs/vimdiff/en
Of course, I could set these to be global keybindings with:
noremap gl :diffget LOCAL<CR>
noremap gr :diffget REMOTE<CR>
但是这将会将它们应用到正常模式的全局按键绑定中。
英文:
I have been using vimdiff in its vimdiff1
layout, where it opens side by side the two buffer of LOCAL and REMOTE. The reason I preferred this layout over the other standard layout is the two shortcuts do
and dp
for getting and putting diff chuncks respectively.
------------------------------------------
| | |
| | |
| | |
| LOCAL | REMOTE |
| | |
| | |
| | |
------------------------------------------
Yet, recently I came across the scenario in which I often had do take a mixture of the two chuncks, for which manual editing to the MERGED file has been easier.
Now, I would like to switch to a LOCAL,MERGED,REMOTE
layout, and I was wondering if it's possible to define custom keybinding only for vimdiff.
Namely, I'd like to define two gl
and gr
shorcuts for :diffget LOCAL
and :diffget REMOTE
.
------------------------------------------
| | | |
| | | |
| LOCAL | MERGED | REMOTE |
| | | |
| | | |
------------------------------------------
Is there a way to define a keybinding only for the vimdiff 'mode'?
Or alternatively, are there already standands keybindings which I'm not aware of?
For reference: https://git-scm.com/docs/vimdiff/en
Of course, I could set these to be global keybindings with:
noremap gl :diffget LOCAL<CR>
noremap gr :diffget REMOTE<CR>
But this would apply them globally to the normal mode.
答案1
得分: 2
There is no special map mode for the diff window (that would also make no sense). So you usually have to work with map-local
to achieve your goal of mappings for a special buffer:
au BufRead * if &diff | exe 'noremap <buffer> gl :diffget LOCAL<CR>' | exe 'noremap <buffer> gr :diffget REMOTE<CR>' | endif
This, however, leaves the mapping on your buffer if diff mode is exited. Diff is not a special buffer, but a special mode. For that case, it is easier to use the <expr>
callback:
nnoremap <expr> gl &diff ? ':diffget LOCAL<CR>' : 'gl'
Which is evaluated every time you press the mapping.
Why do I even talk about the first solution? Well, it is the usual way I was heading down, so I guess it isn't bad to state why it isn't the best idea in this case.
英文:
Well there is no special map mode for the diff window (that would also make no sense). So you usually have to work with map-local
to achieve your Goal of mappings for a special buffer:
au BufRead * if &diff | exe 'noremap <buffer> gl :diffget LOCAL<CR>' | exe 'noremap <buffer> gr :diffget REMOTE<CR>' | endif
This however leaves the mapping on your buffer if diff mode is exited. Diff is not a special buffer, but a special mode. For that case it is easier to use the <expr>
callback:
nnoremap <expr> gl &diff ? ':diffget LOCAL<CR>' : 'gl'
Which is evaluated every time you press the mapping.
Why do I even talk about the first solution? Well it is the usual way I was heading down, so I guess it isn't bad to state why it isn't the best idea in this case.
答案2
得分: 2
gl
默认情况下在正常模式下不起作用,gr
是一个非常狭窄的命令,你可能从未使用过或听说过,所以将它们映射到全局是相当安全的。- 由于这些映射是为特定上下文而设计的,你不会在该上下文之外意外使用它们。
所以我认为你可以安全地将它们添加到你的 vimrc
中,但需要稍作修改:
nnoremap gl :diffget LOCAL<CR>
nnoremap gr :diffget REMOTE<CR>
:noremap
在正常模式、可视模式和操作等待模式下创建一个非递归映射,如果该场景未得到正确处理,这并不是一个好主意(它只对自定义动作有用)。 :nnoremap
限制了你的映射到正常模式,这更符合你尝试做的事情。
也就是说,如果你真的只想在 diff 模式下拥有这些映射,可以使用 :help <expr>
映射来实现,像这样:
nnoremap <expr> gl &diff ? ':diffget LOCAL<CR>' : 'gl'
nnoremap <expr> gr &diff ? ':diffget REMOTE<CR>' : 'gr'
在这里,映射保持全局,但其右侧在运行时进行评估,在不同的上下文中产生不同的宏。这里,检查是在 :help 'diff'
选项上进行的:如果启用了它,那么 gl
就执行 :diffget LOCAL<CR>
,如果没有启用,则 gl
执行 gl
。
英文:
> But this would apply them globally to the normal mode.
gl
does nothing in normal mode by default andgr
is a very niche command that you have probably never used or heard of, so they are quite safe to map globally.- Since those mappings are meant for a specific context, you wouldn't accidentally use them outside of that context anyway.
So I think you can safely add them to your vimrc
, but with a slight modification:
nnoremap gl :diffget LOCAL<CR>
nnoremap gr :diffget REMOTE<CR>
:noremap
creates a non-recursive mapping for normal mode, visual mode, and operator-pending mode, which is not exactly a good idea if that scenario is not handled properly (it's only useful for custom motions). :nnoremap
restricts your mappings to normal mode, which is more in line with what you are trying to do.
That said, if you really want to have those mappings only for diffs, it could be done with a :help <expr>
mapping, like this:
nnoremap <expr> gl &diff ? ':diffget LOCAL<CR>' : 'gl'
nnoremap <expr> gr &diff ? ':diffget REMOTE<CR>' : 'gr'
where the mapping is kept global but its right-hand side is evaluated at runtime, yielding different macros in different contexts. Here, the check is done on the :help 'diff'
option: if it is enabled, then gl
does :diffget LOCAL<CR>
, if it is not, then gl
does gl
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论