vim and NERD Tree extension - adding a file

VimNerdtree

Vim Problem Overview


When using the vim editor with the NERDTree plugin to navigate through the tree of your project, is there an easy way to create a new source code file under the currently highlighted directory?

Currently I go into my shell, add the file and then refresh the tree. There must be a better way.

Vim Solutions


Solution 1 - Vim

Activate the NERDTree and navigate to the directory in which the new file should live. Then press m to bring up the NERDTree Filesystem Menu and choose a for "add child node". Then simply enter the file's (or directory's name) and you're done.

Solution 2 - Vim

From vim you can run shell commands. So in this case I use:

:!touch somefile.txt

and then hit r to reload the nerdtree window.

The other thing to do is to just start the new file from within vim.

:e somefile.txt

One handy thing for this is that in my .vimrc I auto change the cwd to the directory my current file is in:

" Auto change the directory to the current file I'm working on

autocmd BufEnter * lcd %:p:h 

This way if I'm editing a file and want another one in the same place the path is changed right there. Opening any file from NERDTree sets the directory to the one that file is in.

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
QuestionDanielView Question on Stackoverflow
Solution 1 - ViminnaMView Answer on Stackoverflow
Solution 2 - VimRickView Answer on Stackoverflow