append()
inserts several lines of text below the line specified as the first argument. This is in line with the append
ex command. You can for example type :append
(and enter ex mode) then write several lines of text and exit ex mode (by pressing <c-c>
for example), the lines will appear after the line your cursor is on. In other words, the append
command means: append the lines of text I'll write below the current one.
The VimScript function append()
allows for line specifiers, which makes it more useful than the ex command. Useful things that come often are ways of injecting pieces of text at the beginning and end of a file. e.g. for LaTeX this may be useful:
call append(line('^'), [ '\documentclass[12pt]{article}'
\ , '\begin{document}'])
call append(line('$'), '\end{document}')
Where line('$')
is the last line in the file and line('^')
always returns 0
(and I could have written that instead without problems).
To get the behaviour of Vim's A
, or any other Vim normal mode command you can use normal
. To get the output of Foo()
to appear at the end of a line may be a little more complicated, one way is to use execute
:
execute ':normal! A' . Foo()
Or you can use append and then perform J
:
call append(line('.'), Foo())
normal! Jx
(Note that x
is needed to remove the space generated by J
, but this may not work as expected if the string generated by Foo()
starts with spaces).
A
which indeed appends text to the current line.:help function-list
to see what built-in functions are available.