范围与 Vim 键盘映射的高级用法 [英] Advanced Usage of Ranges with Vim Keymappings

查看:31
本文介绍了范围与 Vim 键盘映射的高级用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 vimrc 中有一个映射,它向下注释掉 c 代码区域:

I have a mapping in my vimrc that downwardly comments out regions of c code:

nmap comc :normal! I//<ESC>

由于'normal' ex 命令隐式地将诸如Ncomc"之类的输入转换为.,.+N-1 comc",我可以向下调整注释范围,无需多次击键,也无需离开正常模式.然而,这是 vim 范围可以做的一个非常有限的子集.如果我愿意冗长,我可以像这样实现向上的评论:

Since the 'normal' ex command implicitly converts input such as "Ncomc" to ".,.+N-1 comc", I can range comments downwardly without many keystrokes and without leaving normal mode. This is, however, a very limited subset of what vim ranges can do. If I'm willing to be verbose, I can achieve upward ranging comments like so:

.,.-5 normal comc 

在编辑文本时,我更喜欢输入诸如-6comc"之类的内容或制作使用向上范围的Comc"映射.我没能成功.

While editing text, I would much prefer to type something like "-6comc" or make a mapping of "Comc" that uses upward ranges. I'm haven't been able to do so successfully.

同样,范围操作支持评论直到达到搜索模式,例如:

Similarly, range operations support commenting until a search pattern is reached, e.g :

.,/int main/ comc

但是,我希望在没有所有打字的情况下这样做.

I would, however, like to do so without all that typing.

推荐答案

您请求的行为通常使用 :h map-operator 映射完成.有了这个注释,向下的 3 行将变成 comc2j,但是现在向上 3 行同样简单:comc2k.

The behavior you requested is normally done with :h map-operator mapping. With this commenting 3 lines down will turn into comc2j though, but 3 lines up is now just as easy: comc2k.

您也可以使用可视化模式而无需更改您的映射:V2kcomc.您必须添加具有相同 lhsrhsxnoremap,因为 nnoremap 仅适用于普通模式.(并且不要使用nmap.)

You can also use visual mode without changing your mapping at all: V2kcomc. You will have to add xnoremap with the identical lhs and rhs because nnoremap works only for normal mode. (And do not use nmap.)

第三个选项是将 - 映射到移动 {count} 行并将计数放回的东西:

Third option is mapping - to something that moves {count} lines up and puts count back:

nnoremap <expr> - (v:count ? ":\<C-u>\n" . (v:count-1) . 'k' . v:count : '')

.这假设您正在编写 6-comc,而不是 -6comc.

. This assumes you are writing 6-comc, not -6comc.

//顺便说一句,如果涉及到插件,我会建议 The NERD Commenter.

// By the way, I would suggest The NERD Commenter if it comes to the plugin.

这篇关于范围与 Vim 键盘映射的高级用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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