Create a new file in the directory of the open file in vim?

Vim

Vim Problem Overview


I find myself in the position where I want to create a new file in the same directory as the one that the open file is in. How do I create a new file in the directory of the open file in vim? Also, is there a a place where I can learn these things on my own? Googling didn't help.

Vim Solutions


Solution 1 - Vim

From within Vim, new files are created like existing files are edited, via commands like :edit filename or :split filename. To persist them to disk, you need to (optionally type in contents and) persist them via :write.

Like a command prompt, Vim has a notion of current directory (:pwd lists it). All file paths are relative to it. You don't need to duplicate the path to your current file, there are some nice shortcuts for them: % refers to the current file, :h is a modifier for its directory, minus the file name (cp. :help filename-modifiers). So,

:e %:h/filename
:w

will create a new file named filename in the same directory as the currently open file, and write it.

Alternatively, some people like Vim to always change to the current file's directory. This can be configured by placing

:set autochdir

into your ~/.vimrc file (which is read on Vim startup). Then, above becomes simply

:e filename
:w

Finally, Vim has a great built-in :help. Learn to navigate and search it!

Solution 2 - Vim

you should have a try with "nerdtree" plugin. In the nerdtree window, you typed key m, and file operation choices will display to you

Solution 3 - Vim

If you want to create a new file and also show it in the window next to your current file, you can try this:

:vsp newfile

The vsp stands for vertical split, and it splits the screen in half, one showing your current file, the other showing your new file (also works with just sp, which is a horizontal split).

Per @MartinLyne's comment above, this will create the file in the directory of the file in which you opened vim. To adjust for this, you can change the current working directory as follows:

:cd %:p:h

This command changes the current working directory to the directory of the active file, meaning that running the vsp command (or any of the commands above) will create the file in that directory.

Solution 4 - Vim

When you have opened vim in non existent location like

$ vim /etc/<some_folder/<next_folder>/file.cfg

then to create a new directory while being inside vim, just run in normal mode

:! mkdir -p /etc/<some_folder/<next_folder>

next save your doc as usual :w :x ZZ (whatever you like)

that's it

Solution 5 - Vim

I'm quite late to the party, but another option is to open NERDtree with :E or :Explore (or its splitting alternatives :Vexplore/:Sexplore == :Vex/:Sex).

In NerdTree you can create a new file with %, and type the name. It will automatically open the file, and create it after you :w/save it.

Solution 6 - Vim

This is for Gvim! Enter this to see the current directory.

:cd

then change it with

:cd desktop/somefolder

then save or make new file there

:enew asd.cpp

now again see the file

:cd

Solution 7 - Vim

I usually use:

:tabnew my-file

Then add some content and:

:w

It will create new tab with new file.

(I use Vim 8)

Solution 8 - Vim

With NERDtree

 ma <FILENAME>
 ma <DIRECTORY NAME> + /

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDawgView Question on Stackoverflow
Solution 1 - VimIngo KarkatView Answer on Stackoverflow
Solution 2 - VimharrisView Answer on Stackoverflow
Solution 3 - VimRocketDonkeyView Answer on Stackoverflow
Solution 4 - Vimmatson kepsonView Answer on Stackoverflow
Solution 5 - VimmazunkiView Answer on Stackoverflow
Solution 6 - VimThe FlashView Answer on Stackoverflow
Solution 7 - VimChau GiangView Answer on Stackoverflow
Solution 8 - VimlogbasexView Answer on Stackoverflow