sponsor Vim development Vim logo Vim Book Ad

basic Tip #40: Insert a file

 tip karma   Rating 74/32, Viewed by 2907 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   March 8, 2001 7:25      complexity:   basic
author:   Anonymous      as of Vim:   5.7

To insert the contents of a file (or the output of a system command)
into the current buffer, use the r[ead] command:

Examples:

:r foo.txt        inserts the file foo.txt below the cursor

:0r foo.txt       inserts the file foo.txt above the first line

:r !ls            inserts a listing of your directory below the cursor

:$r !pwd          inserts the current working directory below the last line


For more information about the r[ead] command refer to:

:help read

See also:

:help cmdline-ranges
:help !cmd

 rate this tip  Life Changing Helpful Unfulfilling 

<< Undo and Redo | Command-history facilities for Oracle/sqlplus user >>

Additional Notes

[email protected], July 16, 2002 19:05
Don't know the path to perl (for #!/.../perl)?  

:r!which perl
[email protected], November 2, 2006 4:22
Thanks, 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

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
SourceForge.net Logo