Saturday, 12 September 2009
Generating lines of Repetitive Code Rapidly
A very common editing problem when writing repetitive lines of code.
list($jan,$jan_color)=fn_display_month($record['jan']);
list($feb,$jan_color)=fn_display_month($record['jan']);
list($mar,$jan_color)=fn_display_month($record['jan']);
.... etc etc
list($nov,$jan_color)=fn_display_month($record['jan']);
list($dec,$jan_color)=fn_display_month($record['jan']);
The first line (jan) has been hand written and then the other 11 generated with yy11p
Then the first mention of the month has been hand-edited, now you want to make the
month the same throughout the line
list($jan,$jan_color)=fn_display_month($record['jan']);
list($feb,$feb_color)=fn_display_month($record['feb']);
list($mar,$mar_color)=fn_display_month($record['mar']);
....
list($nov,$nov_color)=fn_display_month($record['nov']);
list($dec,$dec_color)=fn_display_month($record['dec']);
Do this with a recording:- with contents
0f$l"ayiw:s/jan/\=@a/gj
explanation
Start recording @q
0f$ find first dollar
store month into register a
substitute jan with contents of register a
descend a line
list($jan,$jan_color)=fn_display_month($record['jan']);
list($feb,$jan_color)=fn_display_month($record['jan']);
list($mar,$jan_color)=fn_display_month($record['jan']);
.... etc etc
list($nov,$jan_color)=fn_display_month($record['jan']);
list($dec,$jan_color)=fn_display_month($record['jan']);
The first line (jan) has been hand written and then the other 11 generated with yy11p
Then the first mention of the month has been hand-edited, now you want to make the
month the same throughout the line
list($jan,$jan_color)=fn_display_month($record['jan']);
list($feb,$feb_color)=fn_display_month($record['feb']);
list($mar,$mar_color)=fn_display_month($record['mar']);
....
list($nov,$nov_color)=fn_display_month($record['nov']);
list($dec,$dec_color)=fn_display_month($record['dec']);
Do this with a recording:- with contents
0f$l"ayiw:s/jan/\=@a/gj
explanation
Start recording @q
0f$ find first dollar
store month into register a
substitute jan with contents of register a
descend a line
Wednesday, 2 September 2009
Substitute Text in Last Visual Area
Got this from VIM Wiki
:%s/\%Vold/new/g # Substitute Text in Last Visual Area
# Now imagine you'd selected a visual block with (Control V, or Control Q (Windows)) you could do a column based substitute!
gv # revisualise last visual area
:%s/\%Vold/new/g # Substitute Text in Last Visual Area
# Now imagine you'd selected a visual block with (Control V, or Control Q (Windows)) you could do a column based substitute!
gv # revisualise last visual area
Labels: gvim, substitute, vim, visual mode
Subscribe to Posts [Atom]