Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

When I open a new tab with different path from the previous file in VIM, NERDTree will still remains the same directory hierarchy of the previous file.

Is there a sync shortcut to change the current root directory to the new opened file's directory?

share|improve this question
add comment

6 Answers

up vote 3 down vote accepted

I'm not sure if there's a NERDTree-specific way to do that, but you can always configure Vim so that it sets the working directory to the current file's directory:

autocmd BufEnter * lcd %:p:h

Now all what you have to do after opening a file in a new tab is :NERDTreeToggle in the new tab itself.

share|improve this answer
add comment

I use the following mapping to view the current buffer in NERDTree:

 map <leader>r :NERDTreeFind<cr>
share|improve this answer
add comment

I found both the existing answers educational, and successfully combined the two so that the behavior is more like many people would expect from an IDE: Click on an open window/buffer, and have that file highlighted in the NERDTree. I put this in my ~/.vimrc:

autocmd BufEnter * if &modifiable | NERDTreeFind | wincmd p | endif

What this does:

  1. autocmd BufEnter - runs every time you focus on a buffer (including the NERDTree window)
  2. if &modifiable - when you do click the NERDTree window, do nothing else (the NERDTree window is not modifiable)
  3. wincmd p - NERDTreeFind leaves the cursor focused on the NERDTree; this switches back to the window you'd originally focused on

Note that this won't work on any other buffer that isn't modifiable -- but that's generally a good thing; otherwise (for example) any time you got :help in vim, NERDTree would find and focus the directory where help files are stored--probably not something you want it to do.

That one-line solution worked great for me at first, but I soon found that it causes NERDTree to activate any time I opened a file--and as a result, it prevents NERDTree from ever being closed! If you don't want to use NERDTree full-time, put this in your .vimrc instead:

" returns true iff is NERDTree open/active
function! rc:isNTOpen()        
  return exists("t:NERDTreeBufName") && (bufwinnr(t:NERDTreeBufName) != -1)
endfunction

" calls NERDTreeFind iff NERDTree is active, current window contains a modifiable file, and we're not in vimdiff
function! rc:syncTree()
  if &modifiable && rc:isNTOpen() && strlen(expand('%')) > 0 && !&diff
    NERDTreeFind
    wincmd p
  endif
endfunction

autocmd BufEnter * call rc:syncTree()
share|improve this answer
1  
What is the purpose of isNTFocused()? Doesn't the &modifiable check cover that case? –  jrdioko Jun 28 '13 at 19:28
    
@jrdioko yes, I think you are right, that's a bit redundant. I can't think of any good reason to have that there, and simpler is always better. I'll edit it out, thanks! –  Lambart Dec 21 '13 at 19:02
add comment

throw a % sign on the end like a boss

:NERDTree %

i have this in my .vimrc, it maps Ctrl+o to toggle nerdtree in the dir of the current buffer:

map <C-o> :NERDTreeToggle %<CR>

share|improve this answer
    
You know I was skeptical. Something in the way you said it :D. But this is the only thing that worked like I needed it to, well done. –  Hugo Nov 25 '13 at 2:26
add comment

I came across this question yesterday, after a few hours of digging, I submited a Pull Request to scrooloose's nerdtree repo introducing a NERDTreeCWDcommand that change NERD tree root to current working directory(Update on 2012-11-12: The PR has been merged into the upstream master,it should be usable on an updated version). With this change, this question can be simply solved by the following code.

autocmd BufEnter * silent! if bufname('%') !~# 'NERD_tree_' | cd %:p:h | NERDTreeCWD | wincmd p | endif

Compare to @shinzui's and @Lambart's NERDTreeFind approach, this does exactly what the question asked. Using NERDTreeFind will change the scroll position of the nerdtree and the result are not always the same(If CWD is in NERD tree root, it just simply expands the node instead changing into it).

Compare to @Yaser Sulaiman's answer, this solution alwys have a NERD tree window opened and can be easily codable. If a NERD tree window has already been opened, using NERDTreeToggle will need to be fired twice(first close the existing one,then open it again), unfortunately, the second openning will skip the whole cwd processing.

share|improve this answer
add comment

I think this plugin is what you want https://github.com/jistr/vim-nerdtree-tabs

share|improve this answer
2  
Welcome to Super User! Whilst having the link is nice, it would be preferable to include a bit of context here, and explain what the plugin does, how it should be used, etc. Thanks! –  slhck Aug 30 '13 at 5:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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