Bracket auto completion in vim for PHPer

I used to use this setting in .vimrc:

inoremap \( (
inoremap ( ()<LEFT>

But escape bracket every time for

second\(first();

is a little bit stressful. (typed “first()”, then move to the head of the line, typing “second”, back slash and bracket)

Here is an improved script:

function! CompleteDetachedBracket()
    let line = line('.')
    let col = col('.')
    let c = getline(".")[col(".")]
    execute "normal \<RIGHT>%"
    if c == "$"
        execute "normal 2wi)"
    elseif line != line('.') || col != col('.')
        execute "normal a)"
    else
        execute "normal a)"
        startinsert
    endif
endfunction
inoremap ( (<ESC>:call CompleteDetachedBracket()<CR>

This is the first time to code vim script for me. I was guessing vim script is a weird language, so have avoided long time. But at least coding small one is not so difficult than I thought. Documentation is very organized. There are many sample codes in your .vim/plugin directory. Not like a GUI programming, but still like a Tetris. It maybe just because of the first time, I think it’s pretty fun.

This entry was posted in Create and tagged . Bookmark the permalink. Both comments and trackbacks are currently closed.