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. Trackbacks are closed, but you can post a comment.

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

  • About

    I'm Nobu, a web developer living in Mountain View, CA. Feel free to comment on any posts. I'm also tweeting on twitter.

  • Archives

  • Recent Posts