如何在 Vim 中使用带有参数的缩写?
在 Vim 中,您可以通过使用以下命令在插入模式下每次写入“FF”时更改某些代码:
:iab FF for ( int i = 0 ; i < n ; i++ )
但是有什么方法可以将其与参数一起使用吗?就像 C 的 #defines 一样,所以如果我写
FF(e, 10)
它就会变成:
for ( int e = 0 ; e < 10 ; e++ )
In Vim, you can make it so each time you write "FF" on insert mode changes to some code by using:
:iab FF for ( int i = 0 ; i < n ; i++ )
But is there any way to use this with arguments? Something like C's #defines, so if I write
FF(e, 10)
It becomes:
for ( int e = 0 ; e < 10 ; e++ )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看一下 SnipMate (一个 vim 插件)。您不会得到参数,但在扩展缩写时,它允许您通过选项卡浏览可修改区域。在
for
示例中,您将首先进入i
,可以将其编辑为e
,它会将其更改为 < code>e 在for
声明的所有区域。然后只需按 Tab 键切换到您要更改的下一个区域即可。来自文档:
以下是将
tab
和s-tab
重新映射到cd
和ca
的一个非常有用的更改,以防万一不想失去tab
的功能(在~/.vim/after/plugin/snipMate.vim
中):Take a look at SnipMate (a vim plugin). You won't get arguments, but upon expansion of an abbreviation, it allows you to tab through modifiable areas. In the
for
example, you'll be brought to thei
first, can edit it to bee
, and it will change it toe
in all areas of thefor
declaration. Then simply tab to the next area you'd like to change.From the docs:
The following is a nice helpful change to remap
tab
ands-tab
toc-d
andc-a
, in case you don't want to lose the functionality oftab
(in~/.vim/after/plugin/snipMate.vim
):您可以在缩写中包含函数定义,但它们不能带参数。这是 vimdocs 中的一个示例:
我想您可能可以解析函数中的缩写表达式,但我不确定您是否还可以在缩写中包含括号等字符。也许这里会给你一个想法。
编辑:您始终可以执行以下操作:
当然,它缺少参数自动完成功能,但可以让您立即开始输入它。
You can include function definitions in abbreviations, but they cannot take arguments. This is an example from the vimdocs:
I guess you could maybe parse the abbreviation expression in the function, but I'm not sure if you can also include characters like parenthesis in the abbreviation. Maybe something here will give you an idea.
Edit: You can always do something like this:
Which lacks the argument autocompletion of course but lets you start typing it immediately.
它对我有用:
在插入模式下
FF e 10
将是for (int e = 0; e < 10; ++e) {
>。It worked for me:
at insert mode
FF e 10<cr>
will befor (int e = 0; e < 10; ++e) {<cr>
.mu-template 支持交互式模板。有了它,您可以向用户询问某些内容,或者重用任何变量,如果您愿意的话,对其应用计算(可以检测到
i
已经在当前范围内使用),然后使用结果在文本中您将展开。mu-template support interactive templates. With it, you can either ask something to the user, or reuse any variable, apply computation on it if you which (detecting that
i
is already use in the current scope is doable), and use the result in the text you will expand.