You can do this from directly inside of vim with the :read
command, which is abbreviated to :r
The 'read' command is intended to allow you to read a file, e.g.
:r foo.txt
Will insert the contents of foo.txt
after your current cursor position.
From :help :r
*:r* *:re* *:read*
:r[ead] [++opt] [name]
Insert the file [name] (default: current file) below
the cursor.
See |++opt| for the possible values of [++opt].
However, you can read a command that outputs to STOUT just as if it was a file. To call the external command, just prepend an exclamation mark. This gives us:
:r !ls
You can also do this from the terminal before you start vim by using pipes. For example
ls | vim
However, you must explicitly tell vim to read from STDIN, which can by starting vim with a -
at the end.
ls | vim -
From :help --`
*--*
- This argument can mean two things, depending on whether Ex
mode is to be used.
Starting in Normal mode: >
vim -
ex -v -
Start editing a new buffer, which is filled with text
that is read from stdin. The commands that would normally be
read from stdin will now be read from stderr. Example: >