The Rules
  • Feel free to leave constructive criticism, or point out a better way to do something.
  • Personal attacks or flames, on me or anyone else, will be deleted.
  • Past history has shown that 99% of comments I can't read (i.e. those in other languages) to be spam. Therefore, any comment I can't read will be removed.
  • I'm pretty mellow concerning profanity, but excessive (as determined subjectively by me), bad language will be removed.

Sunday, November 30, 2008

Vim Sneakiness

One of the things that I like most about Xcode is the auto-insertion of a closing curly-brace on the second line below whenever I enter an opening curly-brace, and the insertion of the cursor on the line in-between them. However, at heart I'm still a Vim guy, and tonight I got tired of actually typing that closing curly-brace. So, to make Vim automagically create my curly-brace blocks for me, I did the following.

In my .vimrc, I place the following lines


if has("autocmd")
autocmd FileType c source ~/.vim/brace.vim
autocmd FileType cpp source ~/.vim/brace.vim
autocmd FileType objc source ~/.vim/brace.vim
autocmd FileType java source ~/.vim/brace.vim
autocmd FileType pl source ~/.vim/brace.vim
endif


brace.vim is very simple, containing only the following line:

imap { {<CR>}<Esc>O


This causes typing a opening curly brace to immediately insert a Carriage Return character (equivalent to pressing return/enter) then inserts a closing curly-brace, then switching to command-mode and "typing" O to place an insert-mode cursor on a newly-inserted line above the closing curly-brace.

If anyone has a better way to do this, I'm open to suggestions.

1 comment:

Anonymous said...

I just use this for Perl:

inoremap { {<CR>}<Esc>O <TAB>