27

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

Alisha
  • 1,335
  • 4
  • 13
  • 11

4 Answers4

44

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)

jJ'
  • 3,038
  • 32
  • 25
  • 7
    vim also has `\{n\}` that matches `n` occurances of a pattern. For example I used this to insert the word 'GO' on every 100th line (as typing `.*\n` 100 times gets tedious): `%s/\(.*\n\)\{100\}/\0GO\r/g`. Or by enabling very magic mode (`\v`) within the regex, it becomes a bit simpler to read: `%s/\v(.*\n){100}/\0GO\r/` – Chris J Jul 16 '13 at 13:02
  • In spacemacs evil mode running on ubuntu 20.04, I had to use \n to indicate newline, rather than \r, or I would get &M at the beginning of all my lines instead of a newline. – Caleb Jay Aug 25 '20 at 21:56
24

You can use a macro. The complete process looks like:

qq     " start recording to register q (you could use any register from a to z)
o      " insert an empty line below cursor
<Esc>  " switch to normal mode
jjj    " move the cursor 3 lines downward
q      " stop recording

Then just move to the start line and type 1000@q to execute your macro 1000 times.

Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
  • 3
    The final command would look like `qqoThis is the line to addjjjq` and probably `333@q` (1000 / 3 = 333) – Sam Brinck May 02 '12 at 14:10
  • @Sam Vim will stop running the macro as soon as it hits end of file anyway. – Eugene Yarmash May 02 '12 at 19:36
  • This is also handy if you want to do anything with the *end* of a line rather than inserting a newline before the 4th line or similar. – HitScan Jan 24 '17 at 17:04
  • Or you can use a recursive macro like this: `qqqqqoThis is the line to addjjj@qq` and `@q`. I add 3 `q` at the beginning to clean the register `q` first. – politinsa Dec 12 '18 at 22:43
9
" insert a blank line every 3 lines

:%s/\v(.*\n){3}/&\r          

: .............. command
% .............. whole file
s .............. replace
/ .............. start pattern that we will replace
\v ............. very magic mode, see :h very-magic
(.*\n) ......... everything including the line break
{3} ............ quantifier 
/ .............. start new pattern to replace
& .............. corresponds to the pattern sought in (.*\n)
\r ............. add line break

source: http://www.rayninfo.co.uk/vimtips.html

SergioAraujo
  • 11,069
  • 3
  • 50
  • 40
5

I would do this:

:%s/^/\=(line(".")%4==0?"\n":"")/g

this works if your requirement changed to " *add a new blank line every 700 line*s" :) you just change the "4"

P.S. if I need do this, I won't do it in vim. sed, awk, could do it much simpler.

Kent
  • 189,393
  • 32
  • 233
  • 301
  • Can you explain to me what each cmd stands for? – Alisha May 03 '12 at 04:53
  • `^` Represents the start of the line. We are getting the Mod of number of the current line by 4, if it is true (equal zero) it ses a new line as a substituition, otherwise it uses nothing "". – SergioAraujo Aug 29 '17 at 00:55