20 April 2007

VIM tips: blockwise selection mode and recording macros

Here a table schema from postgresql


col1 | integer | not null default nextval('sc.col1_seq'::regclass)
col2 | timestamp without time zone | not null
col3 | character varying(100) |
col4 | numeric |
col5 | numeric |
col6 | integer |
col7 | integer |
col8 | numeric |
col9 | numeric |
col10 | character varying(100) |
col11 | character varying |
col12 | character varying(100) | not null
col13 | character varying |
col14 | character varying |
col15 | numeric |
col16 | numeric |
col17 | numeric |
col18 | character varying |
col19 | numeric |


and I want using VIM to convert to

$ary = array('col1', 'col2', 'col3', 'col4', 'col5', 'col6', 'col7', 'col8', 'col9',
'col10', 'col11', 'col12', 'col13', 'col14', 'col15', 'col16', 'col17',
'col18', 'col19');


How I do it?
The idea is use blockwise selection mode to insert (') in beginning of each line, and use recording macro to append (',) for each line. Format it (not exceed 80 characters per line). More details step by step

  1. <Ctrl+V>GI'<Esc> -- blockwise selection mode and insert (')

  2. 0qaelC',<Ctrl+c>j0q -- record macro to regiser a

  3. 18@a -- replay macro in register a

  4. insert the $ary = array(

  5. VGgq -- to format it, i.e. each line max chars=80



I do a lot of migration work currently, and frequently use these tips. Not sure for other editor, but I believe Kate don't have these features. Sorry Kate, I just don't like U! Someone look impress when saw me using blockwise selection, and that motivate me to write this blog :-)

more tips, http://jmcpherson.org/editing.html

No comments: