Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How can I create a file adjacent to the one I'm editing in Vim? If I'm editing ./app/lib/foo.js I want to create ./app/lib/bar.js

share|improve this question

4 Answers 4

Just "open" the new file as if it already existed, like so:

vim ./app/lib/bar.js

Or, if bar.js is going to be very similar to foo.js, you could copy foo.js into bar.js like so:

cp ./app/lib/foo.js ./app/lib/bar.js

To create an empty foo.js file, just "touch" it like this:

touch ./app/lib/foo.js
share|improve this answer
    
I think the question was about doing this inside vim so maybe you should add some information about how you can run this external commands using :!this_is_thecommand –  tidbeck Feb 23 '12 at 1:29

Given the information on the VIM wiki:

:e %:p:h/myNewFile.txt
share|improve this answer
1  
I think the last solution must the preferred way as you in most situations don't want to change your working directory. –  tidbeck Feb 22 '12 at 13:22
    
@tidbeck You're right, I removed that part from the answer. –  Oliver Salzburg Feb 22 '12 at 13:41

One way I like to create files is using the Explore window.

Open the Explore window:

:E

Now to create a new file in the current directory press %, it will ask for a file name and then open the new file (actually the file will not be created until you write it).

share|improve this answer

If the name of the new file is easy to type, Oliver Salzburg's answer is probably best. If the name of the new file differs only slightly from the name of the current file, it may be easier to use

:e ^R%

where ^R is Ctrl-R. ^R% will be expanded immediately to the name of the current file. Then you can edit that name and change it to the name of the new file. See

:help c_CTRL-R
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.