Vim: How to synchronize NERDTree with current opened tab file path?
Solution 1
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.
Solution 2
I use the following mapping to view the current buffer in NERDTree:
map <leader>r :NERDTreeFind<cr>
Solution 3
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>
Solution 4
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 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()
Solution 5
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
Related videos on Youtube
Comments
-
Jichao over 1 year
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?
-
jrdioko almost 11 yearsWhat is the purpose of
isNTFocused()
? Doesn't the&modifiable
check cover that case? -
slhck over 10 yearsWelcome 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!
-
Hugo over 10 yearsYou 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.
-
Lambart over 10 years@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!
-
phemmer almost 10 yearsRan into a lot of issues being unable to navigate windows because of the
wincmd p
(I literally could not get back into certain windows). I ended up doinglet l:curwinnr = winnr()
beforeNERDTreeFind
andexec l:curwnum . "wincmd w"
after it. Solved all the issues. -
benzen about 9 yearsI find this very usefull, and i went to my .vimrc. I wanted to use some other binding to make it easier to me to remember. And i found out that there is already a binding for this with NERDTree
<Leader>f
-
mawaldne about 9 yearsAwesome! Example what I was looking for.
-
ecbrodie about 7 yearsSuch a boss answer!
-
mhz almost 7 yearsmuch boss, such answer
-
Brian Cannard almost 7 years
Function name must start with a capital or "s:": rc:isNTOpen()
Are you using a plugin extending scopes? -
Lambart almost 7 yearsSorry but I don't know, @BrianHaak. I don't use NerdTree currently, and have misplaced my old
.vimrc
in which I had written that code. I don't recall why I used therc:
, but I think it was a namespacing thing to avoid name conflicts with other functions. I wasn't using any plugin specifically related to that, as far as I recall. You'll have to study the docs, or just try leaving it out and see if anything breaks. But I think one of the function names conflicted with something in NerdTree or elsewhere. No one else has asked in almost 5 years, so I have a feeling it's something simple. :) -
Brian Cannard almost 7 years@Lambart I just replaced it with
s:
-
Lambart almost 7 years@BrianHaak super. I hope it works for you!
-
Brian Cannard almost 7 years@Lambart I've created a usable config with all problems solved: gist.github.com/avesus/1954d9384d86cc1e39cb2b2eff7017b7
-
Lambart over 6 yearsCool. I've been meaning for years to git-ify my various .rc files. Some day...
-
jterm over 6 yearsCan you elaborate on this?
-
alpha_989 over 6 yearsIf you are using this amazing vimrc (not mine), it is mapped to ,nf: github.com/amix/vimrc
-
alpha_989 over 6 yearsDoesn't your solution lead to NERDTree always being open? Also I find that if I use the MRU plugin and try to open files, it opens files in the NERDtree window after I use this change. As it stands this modification will cause a lot of issues and conflicts with MRU (github.com/yegappan/mru/wiki/User-Manual), however I don't know why. Maybe other people can check if they have similar issues. I do like that the NERDtree tab is always open..
-
alpha_989 over 6 yearsActually this could be a good solution for what was asked..
-
Meredith about 6 yearsYeah, this results in NERDTree always being open. Sadness.
-
stillanoob almost 6 yearsWhat key is
<leader>
? -
krry about 5 years@stillanoob,
<leader>
defaults to backslash\
but @alpha_989 and many of us remap it to comma,
which one can do withlet mapleader = ','
in the .vimrc -
X.Arthur about 5 yearsThe only issue is when starting from a blank file, the toggle won't work as there's no dir of the current buffer.
-
Diego Somar about 4 yearsWhat's
<silent>
key? -
Matthias Braun about 4 yearsThat's not a key, it's a command.
-
Eddie Cooro about 4 yearsThis config works well in most of the cases but it messed up everything when I used coc.nvim go to references functionality. using
BufRead
event in place of theBufEnter
fixed the issue. -
Oleg Andriyanov almost 4 yearsIt's funny that I saw this command in NERDTree help like 10 times and didn't bother to read the description for it even once, because the name didn't look like something useful to me: I expected it to do some FS search like UNIX
find(1)
. Turns out it was exactly what I was googling for. -
Kay over 2 yearsAh, saved me two extra key presses, since I was doing
:NERDTree %:h
:) -
Arpan Srivastava over 2 yearsTo add, you can check if the current buffer is modifiable and only then update the CWD. This is useful in case you use terminal inside neovim (using toggleterm maybe) and you don't want this update CWD to run when you switch to the terminal. autocmd BufEnter * silent! if bufname('%') !~# 'NERD_tree_' && &modifiable | cd %:p:h | NERDTreeCWD | wincmd p | endif
-
Admin almost 2 yearsSilent just means executed silently.
silent
just prevents things from being echoed.