Wednesday, September 17, 2008

Working some magic in (G)Vim

It's hard not to love Vim when substitutions and movement is as beatiful as it is. The commands of the day are:
:g!/IMPORT.*\n/d
ggVG
:retab!
:g/\t.*\s.*\s/d
:%s/(\(.*\),/(KCommand\1,"\1"/g
:%s/_LIT(K\(.*\),.*);/TBool CAudioMgmtServerWrapper::\1()/g
These few commands actually lets us completely reformat a block of text, remove comments, add variable names and then turn it into a list of functions again. Sweeet.
Let's go through them a bit more in detail:

:g/exp1/d - cut (delete) the text that matches the expression1 inside the "//".

ggVG - go to the top (gg), go into 'mark mode' (V) and then go to the EOF (G). This is performed in ordinary command mode.

:retab! - Let's replace whitespaces with real tabs instead.

:%s/exp1/exp2/g - Replace expression1 with expression2, and do it for all occurences on every line (g).

No comments: