0

The original text:

line1
lline2

line

I want to add strings into the below format:

line1
lline2
x
y
z

line

Move cursor at the 2th line,and ctrl+;,enter into ex mode:

for s in ['x','y','z']
    call append('.',s)
endfor

Press enter key now,what i get is as below:

line1
lline2
z
y
z

line

Why the order is z,y,x,instead of x,y,z?

1

After you append the x, it is on line 3 but your cursor is still on line 2. So when you subsequently append the y, it is also placed on line 3, pushing the x down.

Try moving your cursor one line further down and then adding the lines above the cursor instead:

for s in ['x','y','z']
    call append(line('.') - 1, s)
endfor

As @ChristianBrabandt points out, you could also just reverse the list:

for s in reverse(['x','y','z'])
    call append('.', s)
endfor

However, an even better solution is to pass the list directly to append() and dispense with the loop entirely. (Thanks to @LucHermitte for the reminder!)

call append('.', ['x', 'y', 'z'])
2
  • 1
    alternatively, you could reverse the list Nov 30 '20 at 9:24
  • 2
    You could also simply use append('.', ['x', 'y', 'z']) Nov 30 '20 at 10:09

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.