Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim column of increasing numbers

Tags:

vim

vi

usually I deal with files that look like this:

0.98   3.45
2.45   3.90
.
.
.
4.56   8.45

lets say with 100 lines. I would like to get something like this:

1   0.98   3.45
2   2.45   3.90
.
.
.
100 4.56   8.45

with a first column of integers. What I usually do is to generate a column file with just the numbers 1,2...100 and then select that block and paste it into the file with two columns. As the number of rows is almost always different my approach seems to be very slow.

Do you have any suggestions?

Thanks.

like image 852
armando Avatar asked Aug 13 '13 21:08

armando


1 Answers

Here's an alternative vim-only Normal mode version. With your cursor in the first column, on the first row:

<C-v>GI0 <ESC>gvg<C-a>
  • <C-v> visual block mode (:help visual-block)
  • G is select to the bottom of the screen (:help G)
  • I starts insert mode on line 1 (:help v_b_I)
  • 0 enter a literal zero and a literal space
  • <ESC> go back to normal mode
  • gv reselect the last visual selection (all of column 1) (:help gv)
  • g<C-a> increment sequentially all numbers in the selection (:help v_g_CTRL-A)

Turns this

0.98   3.45
2.45   3.90
4.56   8.45

into this

1 0.98   3.45
2 2.45   3.90
3 4.56   8.45
like image 134
Parker Tailor Avatar answered Oct 08 '22 02:10

Parker Tailor