Vim Tips Wiki

Insert a file

Redirected from VimTip40

1,624pages on
this wiki
Add New Page
Talk0 Share
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

Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.