Ramendra Sarma
Ramendra Sarma

Reputation: 83

Vim - insert a column of text/insert repeated characters to multiple rows

I have a dataset like by inserting a new column of data between 1st column

     M1    M2   M3   M4
G1    1     2    3    4
G2    4     3    2    1
...
G120  1     3    4    2

I would like to modify dataset with another column of data in vim to

     M1    M2   M3   M4
G1    1     1    2    3    4
G2    1     4    3    2    1
...
G120  1     1    3    4    2

Upvotes: 3

Views: 2659

Answers (2)

Kache
Kache

Reputation: 16667

Vim's blockwise visual mode, ctrl-v, is probably the best way to do this.

In particular, you should use "Visual-block Insert" (vim help: :help v_b_I)

With your example, with the cursor at |1| in normal mode:

     M1    M2   M3   M4
G1   |1|    2    3    4
G2    4     3    2    1
...
G120  1     3    4    2

Do the following:

  • ctrl-v - start visual blockwise selection
  • 3j - extend selection 3 lines down (can substitute any other movement command here)
  • I - begin block insert mode
  • 1<space><space><space><space><space> - the text you want inserted every line
  • Esc or ctrl-[ (its synonym) - complete the visual-block insert

Visual block insert can also be used to indent/unindent multiple lines, append text to every line (even if they don't end on the same column), etc, etc.

Upvotes: 2

Jason Hu
Jason Hu

Reputation: 6333

i assume you use \t to align your text. then regex can be used

:%s/^\(G\d\+\)/\1\t1/

Upvotes: 2

Related Questions