1

I'm working on nand2tetris, and I end up with a lot of files that end up looking like this:

Bit(in=in[0], load=load, out=out[0]);
Bit(in=in[1], load=load, out=out]1]);
...
Bit(in=in[15], load=load, out=out[15]);

So I've been yanking the first line, then using 15p, and then doing :s/0/i/g 15 times (where i is the index I need). I've noticed that I can replace this with :s/\[\d\]/\[i\]/g, but even here I manually set the value of i each time I run the command. Is there a command I can run so that i is automatically calculated to be \d+1 and I can just repeat the command for each line, without manually specifying the value?

zubergu
  • 3,646
  • 3
  • 25
  • 38
charmeleon
  • 2,693
  • 1
  • 20
  • 35

3 Answers3

7

One method is to use a Vim macro.

Overview

Copy the line then paste the line. Increment both numbers.

The macro

qqyyp<c-a>l<c-a>q

Note this saves the macro into register q.

Execute the macro via @q or do it all together via 14@q.

For more help see:

:h q
:h ctrl-a
Peter Rincker
  • 43,539
  • 9
  • 74
  • 101
2

I would do it like Peter Rincker (on-the-fly macros are awesome) but here is another solution, just for the sake of it:

yy                         " yank current line
15p                        " paste 15 times
:,']s/0/\=line('.')-1/g    " substitute every 0 from cursor to last
                             pasted line with the current line number
romainl
  • 186,200
  • 21
  • 280
  • 313
  • +1 Always good to have options! Should be noted that you may need to adjust the `-1` to some other number if the code block is lower down in the file. Or you can do some more trickery like `:,']s/0/\=line('.')-line("'[")/g`. – Peter Rincker Sep 04 '14 at 20:05
0

If I understand what you want to do correctly, you should be able to use the increment package in vim. Take your first line of text and paste it 15 times using 15p:

Bit(in=in[0], load=load, out=out[0]);
Bit(in=in[0], load=load, out=out[0]);
Bit(in=in[0], load=load, out=out[0]);
Bit(in=in[0], load=load, out=out[0]);
Bit(in=in[0], load=load, out=out[0]);
Bit(in=in[0], load=load, out=out[0]);
Bit(in=in[0], load=load, out=out[0]);
Bit(in=in[0], load=load, out=out[0]);
Bit(in=in[0], load=load, out=out[0]);
Bit(in=in[0], load=load, out=out[0]);
Bit(in=in[0], load=load, out=out[0]);
Bit(in=in[0], load=load, out=out[0]);
Bit(in=in[0], load=load, out=out[0]);
Bit(in=in[0], load=load, out=out[0]);
Bit(in=in[0], load=load, out=out[0]);
Bit(in=in[0], load=load, out=out[0]);

You can select the '0' on the first line, and enter visual block mode using Ctrl-V. Highlight to the bottom while in visual block mode, so that all of the 0s are highlighted. Then you can type :Inc<CR>. This will increment the numbers, essentially numbering your lines in text:

Bit(in=in[ 0], load=load, out=out[0]);
Bit(in=in[ 1], load=load, out=out[0]);
Bit(in=in[ 2], load=load, out=out[0]);
Bit(in=in[ 3], load=load, out=out[0]);
Bit(in=in[ 4], load=load, out=out[0]);
Bit(in=in[ 5], load=load, out=out[0]);
Bit(in=in[ 6], load=load, out=out[0]);
Bit(in=in[ 7], load=load, out=out[0]);
Bit(in=in[ 8], load=load, out=out[0]);
Bit(in=in[ 9], load=load, out=out[0]);
Bit(in=in[10], load=load, out=out[0]);
Bit(in=in[11], load=load, out=out[0]);
Bit(in=in[12], load=load, out=out[0]);
Bit(in=in[13], load=load, out=out[0]);
Bit(in=in[14], load=load, out=out[0]);
Bit(in=in[15], load=load, out=out[0]);
Steve
  • 2,401
  • 3
  • 24
  • 28