Friday, December 17, 2004

My own 2 cent VIM Tip

I was editing some code and it required HELL lot of comments (well ... I do put ample comments in the code!!!) and FIXME/TODO notes. I like the format to be something like:
AtulT 17-Dec-04 TODO <text follows here>

Typing this tens of times is !@#$ing boring. You can abbreviate it as:
ab coM AtulT 17-Dec-04
ab TODO AtulT 17-Dec-04 TODO
ab FIXME AtulT 17-Dec-04 FIXME
But that's no GOOD!!! Why??? Hell, I have to change ab settings every day! Thats not how a programmer codes! I knew that HAS to be a better way, esp. when I have VIM! And yes, I discovered my own very very sexy way of doing that. I would love to share my own 2 cent tip with you.

Add this to your .vimrc file:
" Special settings to include date and my name in
" TODO FIXME and comments
" date "+%d-%b-%y"
ab coM AtulT ^O:r! date +\%d-\%b-\%y^[ kJA
ab TODO AtulT ^O:r! date +\%d-\%b-\%y^[ kJA TODO
ab FIXME AtulT ^O:r! date +\%d-\%b-\%y^[ kJA FIXME

BINGO! There I am! Now I coolly type 'coM' and vim expands it to 'AtulT 17-Dec-04'. Same goes for TODO and FIXME!!! The best part is that it will ALWAYS picks up the current date. I'm not sure if there's a sexier way of achieving this, but my own tip is SEXY enough ;-) You can change this to include time, pwd, exact user-id, GID, ... any damn thing.

AtulT 30-Jun-08:Adding space between ^[ and k (i.e. Esc and k) made this tip work for terminal-based vim.

^O and ^[ are special control characters, the way to enter them is, in insert mode, press Ctrl-Q followed by Ctrl-O and Ctrl-Q Ctrl-Esc resp. You know you have entered them right if you navigate over ^O and ^[ and cursor passes through both at once, as a single character. Use Ctrl-V if Ctrl-Q doesn't work for you.


Stephan said...

Are you sure you didn't make a typo? I had to stick a ^M before the ^[ to make it work as advertised.

Otherwise, the ^[ acts like pressing enter and it just types the kJA in insert mode.

The Shaolin said...

Hello Stephan, nope, that is not a typo. It indeed is ^[ and it works perfectly ok with my gvim on linux and MacVim on my Mac. I'm sure you are using a terminal-based pure vim. I verified that I get something akin to you reported in vim. I couldn't get it working with ^M though. But adding a space between ^[ and k did the trick for me on terminal based vim. Thanks for reporting.