Insert output of a system command at the current location in vim

:r !command 

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 of ls -l into register a. You can then paste it (in insert mode) with ^R-a.


Here is alternative way of pasting output from external command before the cursor:

:exe 'norm i' . system("ls -l")

or use expression register (:help @=):

"=system('ls -la')

then hit P. Or shorter way by:

<CTRL-R>=system('ls -la')<CR>