开发者

how to iab <CR> temporarily in gVim

I put this in my _vimrc

iab fi for(int i=0; i<; i++)<CR>{<CR>}<ESC>2kf<a

It expand fi to for...loop

for(int i=0; i< █; i++)
{
}

After input 100, then hit ENTER, I want to open a new line between {...}

for(int i=0; i< 100; i++)
{
    █
}

Is there some way to do this? Thanks!


I use a self-destroying mapping to <CR>, I think there's another way.

iab fi for(int i=0; i<; i++)<开发者_开发知识库;CR>{<CR>}<ESC>2kf<a<C-R>=EatSpace()<CR>

fun! EatSpace()
    let nr = getchar()
    call MapEnter()
    if nr==32
        return ''
    else
        return nr2char(nr)
    endif
endfun

fun! MapEnter()
    inoremap <CR> <ESC>:iunmap <C-V><CR><CR>jo
    return ''
endfun


The snipmate vim plugin is exactly providing this feature.

for<Tab> will produce the following code :

 for (i = 0; i < count; i++)
 {
    /* code */
 }

On the following <TAB> press you'll jump to count then /* code */

You can also easily customize the snippets of code you want to insert.

If you don't want to use an external plugin you can still download snipmate code to see how it is done.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜