如何在 Vim 中使用带有参数的缩写?

发布于 2024-09-06 18:17:01 字数 284 浏览 3 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一笑百媚生 2024-09-13 18:17:01

看一下 SnipMate (一个 vim 插件)。您不会得到参数,但在扩展缩写时,它允许您通过选项卡浏览可修改区域。在 for 示例中,您将首先进入 i,可以将其编辑为 e,它会将其更改为 < code>e 在 for 声明的所有区域。然后只需按 Tab 键切换到您要更改的下一个区域即可。

来自文档:

snipMate.vim 旨在成为一个不显眼、简洁的 vim 脚本,在 Vim 中实现 TextMate 的一些片段功能。代码片段是一段经常键入的文本,您可以使用触发词后跟 .

将其插入到文档中。

例如,在使用默认安装的 snipMate.vim 的 C 文件中,如果您在插入模式下键入“for”,它将扩展 C 中的典型 for 循环:

for (i = 0; i < count; i++) {

}

要转到循环中的下一项,只需转到它即可;如果存在重复的代码,例如本示例中的“i”变量,您只需在其突出显示后开始输入即可,代码片段中指定的所有匹配项都将被更新。

以下是将 tabs-tab 重新映射到 cdca 的一个非常有用的更改,以防万一不想失去 tab 的功能(在 ~/.vim/after/plugin/snipMate.vim 中):

"""ino <silent> <tab> <c-r>=TriggerSnippet()<cr>
"""snor <silent> <tab> <esc>i<right><c-r>=TriggerSnippet()<cr>
"""ino <silent> <s-tab> <c-r>=BackwardsSnippet()<cr>
"""snor <silent> <s-tab> <esc>i<right><c-r>=BackwardsSnippet()<cr>
"""ino <silent> <c-r><tab> <c-r>=ShowAvailableSnips()<cr>
ino <silent> <c-d> <c-r>=TriggerSnippet()<cr>
snor <silent> <c-d> <esc>i<right><c-r>=TriggerSnippet()<cr>
ino <silent> <c-a> <c-r>=BackwardsSnippet()<cr>
snor <silent> <c-a> <esc>i<right><c-r>=BackwardsSnippet()<cr>
ino <silent> <c-r><tab> <c-r>=ShowAvailableSnips()<cr>

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 the i first, can edit it to be e, and it will change it to e in all areas of the for declaration. Then simply tab to the next area you'd like to change.

From the docs:

snipMate.vim aims to be an unobtrusive, concise vim script that implements some of TextMate's snippets features in Vim. A snippet is a piece of often-typed text that you can insert into your document using a trigger word followed by a .

For instance, in a C file using the default installation of snipMate.vim, if you type "for" in insert mode, it will expand a typical for loop in C:

for (i = 0; i < count; i++) {

}

To go to the next item in the loop, simply over to it; if there is repeated code, such as the "i" variable in this example, you can simply start typing once it's highlighted and all the matches specified in the snippet will be updated.

The following is a nice helpful change to remap tab and s-tab to c-d and c-a, in case you don't want to lose the functionality of tab (in ~/.vim/after/plugin/snipMate.vim):

"""ino <silent> <tab> <c-r>=TriggerSnippet()<cr>
"""snor <silent> <tab> <esc>i<right><c-r>=TriggerSnippet()<cr>
"""ino <silent> <s-tab> <c-r>=BackwardsSnippet()<cr>
"""snor <silent> <s-tab> <esc>i<right><c-r>=BackwardsSnippet()<cr>
"""ino <silent> <c-r><tab> <c-r>=ShowAvailableSnips()<cr>
ino <silent> <c-d> <c-r>=TriggerSnippet()<cr>
snor <silent> <c-d> <esc>i<right><c-r>=TriggerSnippet()<cr>
ino <silent> <c-a> <c-r>=BackwardsSnippet()<cr>
snor <silent> <c-a> <esc>i<right><c-r>=BackwardsSnippet()<cr>
ino <silent> <c-r><tab> <c-r>=ShowAvailableSnips()<cr>
惟欲睡 2024-09-13 18:17:01

您可以在缩写中包含函数定义,但它们不能带参数。这是 vimdocs 中的一个示例:

func Eatchar(pat)
   let c = nr2char(getchar(0))
   return (c =~ a:pat) ? '' : c
endfunc
iabbr <silent> if if ()<Left><C-R>=Eatchar('\s')<CR>

我想您可能可以解析函数中的缩写表达式,但我不确定您是否还可以在缩写中包含括号等字符。也许这里会给你一个想法。

编辑:您始终可以执行以下操作:

:iab for() for(int i = 0; i < ; i++)<C-o>T<

当然,它缺少参数自动完成功能,但可以让您立即开始输入它。

You can include function definitions in abbreviations, but they cannot take arguments. This is an example from the vimdocs:

func Eatchar(pat)
   let c = nr2char(getchar(0))
   return (c =~ a:pat) ? '' : c
endfunc
iabbr <silent> if if ()<Left><C-R>=Eatchar('\s')<CR>

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:

:iab for() for(int i = 0; i < ; i++)<C-o>T<

Which lacks the argument autocompletion of course but lets you start typing it immediately.

平安喜乐 2024-09-13 18:17:01

它对我有用:

iab FF <c-o>:FF
com -nargs=* FF call s:FF(<f-args>)
fu s:FF(i, n)
    let t = "for (int a = 0; a < b; ++a) {\e"
    let t1 = substitute(t, 'a', a:i, 'g')
    exe 'normal! A'.substitute(t1, 'b', a:x, 'g')
    exe "normal o\<space>\<BS>\e"
endf

在插入模式下 FF e 10 将是 for (int e = 0; e < 10; ++e) { >。

It worked for me:

iab FF <c-o>:FF
com -nargs=* FF call s:FF(<f-args>)
fu s:FF(i, n)
    let t = "for (int a = 0; a < b; ++a) {\e"
    let t1 = substitute(t, 'a', a:i, 'g')
    exe 'normal! A'.substitute(t1, 'b', a:x, 'g')
    exe "normal o\<space>\<BS>\e"
endf

at insert mode FF e 10<cr> will be for (int e = 0; e < 10; ++e) {<cr>.

香橙ぽ 2024-09-13 18:17:01

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文