$ cd ~/Sites/project
project $ vim

I would like to see NERDTree opening with ~/Sites/project as a root folder.

Instead I see ~/Sites with project folder selected. So I have to cd to the work folder each time.

I am sure there should be a setting for that but I can't find it. Help.

5 Answers 5


Use :set autochdir command to change the current directory. It will set the working directory as specific to the opened file.

  • This is the answer OP asked for!
    – zzeroo
    Commented Nov 26, 2020 at 11:47

From the documention it would seem that it should be using your ~/Sites/project directory. (See :h :NERDTree)

:NERDTree [<start-directory> | <bookmark>]                         *:NERDTree*
    Opens a fresh NERD tree. The root of the tree depends on the argument
    given. There are 3 cases: If no argument is given, the current directory
    will be used.  If a directory is given, that will be used. If a bookmark
    name is given, the corresponding directory will be used.  For example: >
        :NERDTree /home/marty/vim7/src
        :NERDTree foo   (foo is the name of a bookmark)

:NERDTreeToggle [<start-directory> | <bookmark>]             *:NERDTreeToggle*
    If a NERD tree already exists for this tab, it is reopened and rendered
    again.  If no NERD tree exists for this tab then this command acts the
    same as the |:NERDTree| command.

If :NERDTreeToggle is loading the incorrect directory you need to try the following:

  • Verify the shell's current working directory. <c-z> to suspend Vim and then do pwd. Use fg to bring back Vim.
  • Make sure your current working directory is set correctly inside of Vim. Check via :pwd.
  • Do you have some plugin/customization that would change your current working directory?
  • Error still occurring? Create a ticket in NERDTree's tracker. Make sure you use a minimal vimrc and no plugins other than NerdTree so that others can reproduce this bug.
  • After restarting vim with everything commented out in .vimrc except nerdtree... everything is back to normal. I don't have any explanation for this at the moment, but it fixed itself.
    – firedev
    Commented Apr 18, 2015 at 5:49
  • Sorry it is not consistent actually. I need to investigate it further.
    – firedev
    Commented Apr 18, 2015 at 6:31

Here is what I did in the end. Thanks to GitHub

function! NERDTreeToggleInCurDir()
  " If NERDTree is open in the current buffer
  if (exists("t:NERDTreeBufName") && bufwinnr(t:NERDTreeBufName) != -1)
    exe ":NERDTreeClose"
    if (expand("%:t") != '')
      exe ":NERDTreeFind"
      exe ":NERDTreeToggle"

" nnoremap <leader>nf :NERDTreeFind<cr>
nnoremap <leader>nf :call NERDTreeToggleInCurDir()<cr>
  • The exec’s aren’t necessary; you can write an ex command directly.
    – D. Ben Knoble
    Commented Sep 11, 2020 at 22:51

From the NERDTree docs, you should be able to add the following to your .vimrc

"Open NERDTree if no files specified
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 0 && !exists("s:std_in") | NERDTree | endif

This will open the present working directory if you do not specify a file to be opened when you call vim

I hope that helps!

  • what does !exists("s:std_in") mean?
    – dopatraman
    Commented Jul 2, 2015 at 20:50
  • I would imagine it pairs with argc==0 to check that a plain invocation of vim has been called. Then it opens NERDTree
    – Avery
    Commented Jul 4, 2015 at 23:53

FWIW, I use this (in my .vimrc): autocmd vimenter * silent! lcd %:p:h

It sets the pwd to be the current folder (or, the folder/file given when launching vim).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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