In vim, when I use
:r !ls somefilename
it inserts output of that command on a new line below the current line.
If I do
let @a = system("ls")
and later
"ap
it still inserts the output on a new line below the current line.
Is there a way to make vim insert output at the current location?
will read the output from the command and insert it into the line under the current line. This is how vi is programmed you cannot change the behavior.
But say if you are in line number 3. If you try
:r !date
. It will insert the date value into line number 4.If you want the date value to be appeared on line number 3, then you try
:2r !date
will insert the date value in line number 3.You can paste the contents of the clipboard buffer between characters with Ctrl-R * in insert mode (and a similar approach for other buffers). So if you can get the system command into a buffer, you should be set. (Source: https://stackoverflow.com/questions/1491135/paste-multi-line-string-into-gvim-at-cursor-position ).
:let @a=system("ls -l")
will put the output ofls -l
into registera
. You can then paste it (in insert mode) with^R-a
.Here is alternative way of pasting output from external command before the cursor:
or use expression register (
:help @=
):then hit P. Or shorter way by:
In addition to
let
and p, we can also employ vim's functionssetreg()
andput
like so:Note, register
f
was chosen in this example simply for mnemonic representation for the word "files". There's nothing particularly special about it and you're free to choose your own register.The
:r
command takes a line range into which the results will be read. In vim command lines, ".
" in a range means "the current line." So:r ...
is equivalent to:.r ...
, and means "put what I'm reading after the current line.You're free to specify the range to write to using an expression based on
.
. Specifically, the thing you're asking for should be easily done with: