bqui56
bqui56

Reputation: 2111

How to create a new file containing an existing file's content in vim

I want to create a file with a block of code in it and then, when I open a new file, this block is already in the file without having to copy paste every time. Something like:

:e newfile.cpp/template.cpp

where I now have a new file named newfile.cpp and it has the contents of template.cpp in it; template.cpp will just sit in my directory and wont be changed unless I open it specifically.

Upvotes: 0

Views: 150

Answers (4)

Ingo Karkat
Ingo Karkat

Reputation: 172500

If rely on templates a lot, you probably need something more advanced. There are several such plugins on vim.org; one is tSkeleton.

Upvotes: 0

kev
kev

Reputation: 161604

Use |(bar) to concate two commands:

:e newfile.cpp | r template.cpp
  • create a new file
  • read the template

Upvotes: 2

sidyll
sidyll

Reputation: 59277

You can leave your template opened and use:

:saveas newfile.cpp

Or, use one snippets plugin like snipMate or XPTemplate to implement a similar functionality.

Upvotes: 1

Mark Wilkins
Mark Wilkins

Reputation: 41232

One generic possibility is simply to use this command:

:r template.cpp

Upvotes: 3

Related Questions