How to use abbreviations in Vim with arguments?

In Vim, you can make it so that every time you write "FF" in insert mode in some code, use:

:iab FF for ( int i = 0 ; i < n ; i++ )

But is there a way to use this with arguments? Something like C # defines, so if I write

FF(e, 10)

This will:

for ( int e = 0 ; e < 10 ; e++ )
+3
source share
4 answers

SnipMate ( vim). , . for i, e, e for. , .

:

snipMate.vim , vim script, TextMate Vim. - , , .

, C, snipMate.vim , "" , C:

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

}

, ; , "i", , , , , .

tab s-tab c-d c-a, 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>
+7

, . 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<

, , .

+2

:

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<cr> for (int e = 0; e < 10; ++e) {<cr>.

+1

mu-template . - , , , ( , i ), , .

0

Source: https://habr.com/ru/post/1751633/


All Articles