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?
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?
I use the following mapping to view the current buffer in NERDTree:
map <leader>r :NERDTreeFind<cr>
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>
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:
autocmd BufEnter - runs every time you focus on a buffer (including the NERDTree window)if &modifiable - when you do click the NERDTree window, do nothing else (the NERDTree window is not modifiable)wincmd p - NERDTreeFind leaves the cursor focused on the NERDTree; this switches back to the window you'd originally focused onNote 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()
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.
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.
This behaves like :NERDTreeToggle but will show the currently opened file in NERDTree.
If you haven't opened a file yet (i.e., you just entered vim in your command line) NERDTree shows /home.
Put this in your .vimrc:
" Open NERDTree in the directory of the current file (or /home if no file is open)
nmap <silent> <C-i> :call NERDTreeToggleInCurDir()<cr>
function! NERDTreeToggleInCurDir()
" If NERDTree is open in the current buffer
if (exists("t:NERDTreeBufName") && bufwinnr(t:NERDTreeBufName) != -1)
exe ":NERDTreeClose"
else
exe ":NERDTreeFind"
endif
endfunction
I apply both solutions from Change current directory using NERDTree: use cd to set the NERDTree working directory to the current directory and C to set the NERDTree root node to the current directory
I found the answer that Matthias posted to be a great answer with one problem, it doesn't work well in a couple of edge cases. It works a little better with the change below:
function! NERDTreeToggleInCurDir()
" If NERDTree is open in the current buffer
if (exists("t:NERDTreeBufName") && bufwinnr(t:NERDTreeBufName) != -1)
exe ":NERDTreeClose"
elseif bufname('%')
exe ":NERDTreeFind"
else
exe ":NERDTreeCWD"
endif
endfunction
Here's an improved version of @weynhamz 's answer that syncs NERDTree without leaving it open and also handles read-only and empty buffers:
" autocommands that call SyncNerdTree() when the buffer is changed or written
" to
augroup nerd_tree_sync_current_dir
autocmd!
autocmd BufEnter * call SyncNERDTree()
autocmd BufWrite * call SyncNERDTree()
augroup end
" sync NERDTree with the current file open
function SyncNERDTree()
" make sure the buffer isn't a NERDTree buffer, it is not empty, and also
" make sure the buffer contains a file
" file
if stridx(bufname('%'), 'NERD_tree_') == -1 && bufname() != "" && &buftype == "" && &filetype != ""
cd %:p:h
" open a new NERDTree buffer and close it
NERDTree
NERDTreeClose
endif
endfunction