Search code examples
bashvimautomationscriptingline

Multiple lines added to vim line by line


Can you please help add multiple lines of txt to the file via bash script through vim?

I tried this:

vim -c "3 s/^/
         add-this-line1
         add-this-line2
         add-this-line3/" -c "wq" /var/www/html/webserver/output_file.txt

But, the output of the file looks like this:

3 add-this-line1 add-this-line2 add-this-line3 

What I want to do is, add the lines one by one FROM the line 3 in the output_file.txt not at the line 3 one next to another.


Solution

  • This is more of a job for , IMO

    seq 10 > file
    
    ed file <<END_ED
    3a
    first
    second
    third
    .
    wq
    END_ED
    

    For those new to ed, the line with the dot signals the end of "insert mode".

    file now contains:

    1
    2
    3
    first
    second
    third
    4
    5
    6
    7
    8
    9
    10