Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a line after every few lines in vim

Tags:

I wanted to add a line after every 3 lines in a file (having about 1000 lines) using vim editor. Can someone help me out?

Thanks, Alisha

like image 373
Alisha Avatar asked May 02 '12 12:05

Alisha


People also ask

How do I insert a line break in Vim?

in my . vimrc for years. Press Enter to insert a blank line below current, Shift + Enter to insert it above.

How do you repeat a line in Vim?

Press Esc . This will cause you to go back to command mode, and the text you typed will be repeated on each line before the start of your visual block selection (in this case, at the start of each line, since that's where you began the selection).

How do I add multiple lines in vi?

Go to the line from which you want to start commenting. Then, press ctrl + v , this will enable the visual block mode. use the down arrow to select multiple lines that you want to comment. Now, press SHIFT + I to enable insert mode.


1 Answers

there is a vim-specific regular expression to do that

  :%s/.*\n.*\n.*\n/\0\r/g 
  • %s is vim ex command to substitute in the whole file
  • .*\n is a line including the end of line
  • \0 is the entire matched expression
  • \r vim way to say add a new line (not \n as one would expect)

Edit: if you want anything else than a new line, just put the text in front of the \r (properly regex escaped, if it contains some regex characters)

like image 147
jJ' Avatar answered Oct 04 '22 23:10

jJ'