Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

Given the information on the VIM wiki:

:e %:p:h/myNewFile.txt
share|improve this answer
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

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

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

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

Open the Explore window:


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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .