0
!sed -i "N;4i\xxxxxxxx" % 
!sed -i "N;10a\yyyyyyyy" % 

Add a line containing xxxxxxx before the 4-th line,and add a line containing yyyyyyyy after the 10-th line?

How to write a function to add a line containing xxxxxxx before the m-th line,and add a line containing yyyyyyyy after the n-th line with vimscript?

2

From :h append():

append({lnum}, {expr})                    *append()*
      When {expr} is a |List|: Append each item of the |List| as a
      text line below line {lnum} in the current buffer.
      Otherwise append {expr} as one text line below line {lnum} in
      the current buffer.

So you can use this to add a line after the 10th line:

:call append(10, "yyyyyyy")

And to add a line before the 4th one:

:call append(3, "xxxxxx")

You may also want to read :h :put

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.