tags:

views:

406

answers:

4

I do NOT have python/ruby enabled. My Question: I frequently have to write things like the following:

%macro(200701);x gzip /home/test/200701.txt;run;
%macro(200702);x gzip /home/test/200702.txt;run;
%macro(200703);x gzip /home/test/200703.txt;run;
%macro(200704);x gzip /home/test/200704.txt;run;
%macro(200705);x gzip /home/test/200705.txt;run;
%macro(200706);x gzip /home/test/200706.txt;run;
%macro(200707);x gzip /home/test/200707.txt;run;
%macro(200708);x gzip /home/test/200708.txt;run;
%macro(200709);x gzip /home/test/200709.txt;run;
%macro(200710);x gzip /home/test/200710.txt;run;
%macro(200711);x gzip /home/test/200711.txt;run;
%macro(200712);x gzip /home/test/200712.txt;run;

%macro(200801);x gzip /home/test/200801.txt;run;
%macro(200802);x gzip /home/test/200802.txt;run;
%macro(200803);x gzip /home/test/200803.txt;run;
%macro(200804);x gzip /home/test/200804.txt;run;
%macro(200805);x gzip /home/test/200805.txt;run;
%macro(200806);x gzip /home/test/200806.txt;run;
%macro(200807);x gzip /home/test/200807.txt;run;
%macro(200808);x gzip /home/test/200808.txt;run;
%macro(200809);x gzip /home/test/200809.txt;run;
%macro(200810);x gzip /home/test/200810.txt;run;
%macro(200811);x gzip /home/test/200811.txt;run;
%macro(200812);x gzip /home/test/200812.txt;run;

Is there a fast way to do this in vim?

I usually will type:

%macro(200701);x gzip /home/test/200701.txt;run;

Then issue the following commands:

yy11p10<up>13<right>r2<down>r3<down>r4<down>...

So in other words I paste the line 11 more times, then using the "replace char" command run through the list of dates. Then I'll copy the whole block and in the new block will type

:s/2007/2008/<enter>12&11<up>12&

to substitute 2007 for 2008 in the second block.

Vim is just so powerful I figure there has to be a better way then constantly mannually replacing 1 through 12 on each of the lines.

Thanks

Edit: Wow! Thank you all for the great responses! I figured there would be a way, but apparently there are a ton of ways. I'm learning a lot about Vim from your responses, thanks!

+8  A: 

Write the first line:

%macro(200701);x gzip /home/test/200701.txt;run;

Then, while still on that line, go into command mode and record a macro a that copies the line (yyp), increments the first number (ctrl-a, written ^A), moves one character to the right (l) and then increments the other number:

qayyp^Al^Aq

(Note that starting and stopping macro recording happens with q.) Now play macro a 110 times:

110@a
Stephan202
+1 vim macros can be a bit scary when you get into them (especially reading other's macro code), but it's absolutely incredible the amount of work they can save on repetitive text editing tasks. I think it's best to just dive in and experiment, once you're comfortable with vim and you're aware of the commands to record and play back macros.
therefromhere
Thank you! That was very effective!
Dan
+5  A: 

It can also be done with vim 7 functions:

:let t=map(range(200801,200815), '"macro(".v:val.");x gzip /home/test/".v:val.".txt;run;"')
:put=t
Luc Hermitte
+3  A: 

If you have the Unix/Linux core utilities at your disposition there is a way which is easier to remember, at least if you are accustomed to use block commands:

(1) Generate the sequence using seq(1)

:r!seq 200801 200812

seq also allows formating like this:

:r!seq -f "\%03.0f"  10 121

The percent sign must be escaped.

(2) Double this block with the Vim block commands: Mark with C-v, yank, put

(3) Insert (or append) the text between this blocks: Mark with C-V, insert with I

See :help blockwise-visual for more.

fgm
+1  A: 

Also see Vimtip#150.

This explains how to mark a block of text and automatically increase the numbers in the marked block.