7

Given:

$ cd ~/Sites/project
project $ vim
:NERDTreeFind

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.

3

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 Apr 18 '15 at 5:49
  • Sorry it is not consistent actually. I need to investigate it further. – firedev Apr 18 '15 at 6:31
2

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 Jul 2 '15 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 Jul 4 '15 at 23:53
2

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"
  else
    if (expand("%:t") != '')
      exe ":NERDTreeFind"
    else
      exe ":NERDTreeToggle"
    endif
  endif
endfunction

" nnoremap <leader>nf :NERDTreeFind<cr>
nnoremap <leader>nf :call NERDTreeToggleInCurDir()<cr>
0

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, privacy policy and cookie policy

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