427

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.

dionyziz
  • 2,394
  • 1
  • 23
  • 29
Daniel
  • 16,026
  • 18
  • 65
  • 89

2 Answers2

908

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.

Alan Haggai Alavi
  • 72,802
  • 19
  • 102
  • 127
innaM
  • 47,505
  • 4
  • 67
  • 87
  • 37
    I'll add that if you're adding a directory name, remember to enter a "/" following the name. – Buffalo Billion Mar 22 '12 at 14:11
  • 16
    Type "?" and see more options of NERDTree that may save your time. – starikovs Jan 25 '14 at 10:06
  • Is there a way to create several dirs in each other before the file being created... e.g. [ma] then: whereiam/newdir1/newdir2/somefile doesn't work, but [ma] then: whereiam/newdir1/newdir2/ works (though it gives some errors, the dirs are created). – justin Apr 15 '14 at 12:00
  • Any way to save a step, and automatically open the created file? Use the create option all the time, but 90% of time wish to start editing the file in a new buffer right after creation. – arcseldon Jan 27 '18 at 00:02
  • Is it possible to add two or more files at once in NerdTree by `ma` ? – afacat Jul 15 '19 at 07:16
50

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.

Beterraba
  • 6,515
  • 1
  • 26
  • 33
Rick
  • 15,484
  • 5
  • 25
  • 29
  • 10
    Hit R (capitalized) instead to refresh the window. Lowercase r refreshes only the directory under the cursor. – Andrew Jul 08 '15 at 19:05