4

When we execute any system command and get the output, it prints in next line. But I want it in current line, next to the cursor or in place of cursor (not at the start of the current line)

Example:

:r! ls

Prints the list of files in the current folder.

But this prints the output in next line. Is there any way to print the output in the current line, next to the cursor?

Some example situations:

  • listing of files inside quotes
  • executing a perl/external program and getting its output pasted here.
1
9

In normal mode press double !, then enter the command, or just type the following:

:.!ls
2
  • Can you explain what's going on here? I know . is a range and ! calls an external command. But the combination is puzzling and I can't find documentation for it. Jul 8 at 17:45
  • 1
    @BigMcLargeHuge, It is explained in the complex-change documentation (:h complex-change). So the general form is :{range}![!]{filter} [!][arg]. In our case the {range} is . which is the current line (see :h range) and ls is the {filter} program. The filter program accepts the text of the current line at standard input. Vim executes the command and replaces the current line with the output of the filter command. If there is no intention to send anything to the command, it would be better to run this on an empty line. !!{filter} is a quick way of entering the same sequence. Jul 9 at 14:49
4

One can execute any external command in command line using the system function:

:let @a = system("ls -ltr")

And when you want to paste, press Ctrl+R and immediately a to paste the content next to cursor.

@a is the register name. You can use whatever register you want.

1
  • 1
    This is useful. But, I had to learn more tricks to get this done. I first tried executing the command needed to get the resultant text using :!. Then, had to enter into command-editing mode using Ctrl-F. Edit the command to create register @a as mentioned in this answer. And hit Enter key <CR> to get it executed. More on how to do get into command-editing mode at : stackoverflow.com/a/2078230/4106458 Mar 18 at 17:41

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.