在 Vim 中,每次在插入模式下写入“FF”时,您都可以使用以下方法更改某些代码:
:iab FF for ( int i = 0 ; i < n ; i++ )
但是有什么方法可以将它与参数一起使用吗?像 C 的 #defines 这样的东西,所以如果我写
FF(e, 10)
它成为了:
for ( int e = 0 ; e < 10 ; e++ )
在 Vim 中,每次在插入模式下写入“FF”时,您都可以使用以下方法更改某些代码:
:iab FF for ( int i = 0 ; i < n ; i++ )
但是有什么方法可以将它与参数一起使用吗?像 C 的 #defines 这样的东西,所以如果我写
FF(e, 10)
它成为了:
for ( int e = 0 ; e < 10 ; e++ )
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>
您可以在缩写中包含函数定义,但它们不能带参数。这是来自 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<
当然,它缺少参数自动完成功能,但可以让您立即开始输入。
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>
.
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.