Vim Tips Wiki
Tip 40 Printable Monobook Previous Next

created 2001 · complexity basic · version 6.0

Use the :read command to insert a file, or the output from a system command, into the current buffer. Examples:

:r foo.txt    Insert the file foo.txt below the cursor.
:0r foo.txt   Insert the file foo.txt before the first line.
:r !ls        Insert a directory listing below the cursor.
:$r !pwd      Insert the current working directory below the last line.

On Windows, the last two commands would be:

:r !dir
:$r !cd



Don't know the path to perl (for #!/.../perl)?

:r !which perl

I now no longer need to use "append system" code in my scripts. I used to use code like:

call append(line("."), system(l:cmd))

And because this puts the entire results of the system command on a single line separated by ^@, I need to split the lines up:

silent! exec "%s/\<CR>/\<CR>/g"
silent! exec "%s/\<NL>/\<CR>/g"

The worst thing was when the results were really large (try running %s on a single 6Mb line... not pleasant) I can now just use

exec "r !".l:cmd