Wikia

Vim Tips Wiki

Changes: Insert a file

Edit

Back to page

(Standard category names + minor manual clean.)
(Change <tt> to <code>, perhaps also minor tweak.)
 
Line 11: Line 11:
 
|category2=
 
|category2=
 
}}
 
}}
Use the <tt>:read</tt> command to insert a file, or the output from a system command, into the current buffer. Examples:
+
Use the <code>:read</code> command to insert a file, or the output from a system command, into the current buffer. Examples:
 
<pre>
 
<pre>
 
:r foo.txt Insert the file foo.txt below the cursor.
 
:r foo.txt Insert the file foo.txt below the cursor.

Latest revision as of 05:09, July 13, 2012

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

ReferencesEdit

CommentsEdit

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

Around Wikia's network

Random Wiki