Defining a shortcut only in vimdiff.

huangapple go评论66阅读模式
英文:

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定义自定义按键绑定。具体而言,我想定义两个快捷键glgr,分别用于执行: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&lt;CR&gt;
noremap gr :diffget REMOTE&lt;CR&gt;

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 &amp;diff | exe &#39;noremap &lt;buffer&gt; gl :diffget LOCAL&lt;CR&gt;&#39; | exe &#39;noremap &lt;buffer&gt; gr :diffget REMOTE&lt;CR&gt;&#39; | 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 &lt;expr&gt; callback:

nnoremap &lt;expr&gt; gl &amp;diff ? &#39;:diffget LOCAL&lt;CR&gt;&#39; : &#39;gl&#39;

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 and gr 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&lt;CR&gt;
nnoremap gr :diffget REMOTE&lt;CR&gt;

: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 &lt;expr&gt; mapping, like this:

nnoremap &lt;expr&gt; gl &amp;diff ? &#39;:diffget LOCAL&lt;CR&gt;&#39; : &#39;gl&#39;
nnoremap &lt;expr&gt; gr &amp;diff ? &#39;:diffget REMOTE&lt;CR&gt;&#39; : &#39;gr&#39;

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 &#39;diff&#39; option: if it is enabled, then gl does :diffget LOCAL&lt;CR&gt;, if it is not, then gl does gl.

huangapple
  • 本文由 发表于 2023年4月19日 16:05:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76052084.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定