Vim: How to synchronize NERDTree with current opened tab file path?

62

33

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?

Jichao

Posted 2010-10-02T03:57:27.510

Reputation: 5 245

Answers

15

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.

Yaser Sulaiman

Posted 2010-10-02T03:57:27.510

Reputation: 266

118

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

 map <leader>r :NERDTreeFind<cr>

shinzui

Posted 2010-10-02T03:57:27.510

Reputation: 1 281

3I 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 – benzen – 2015-02-18T18:16:44.187

1Awesome! Example what I was looking for. – mawaldne – 2015-04-28T02:32:12.213

Can you elaborate on this? – jterm – 2017-08-14T17:50:16.380

If you are using this amazing vimrc (not mine), it is mapped to ,nf: https://github.com/amix/vimrc

– alpha_989 – 2017-11-21T15:34:20.013

What key is <leader>? – stillanoob – 2018-07-28T07:15:31.803

@stillanoob, <leader> defaults to backslash \ but @alpha_989 and many of us remap it to comma , which one can do with let mapleader = ',' in the .vimrc – krry – 2019-02-27T19:49:47.977

32

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>

schpet

Posted 2010-10-02T03:57:27.510

Reputation: 909

Such a boss answer! – ecbrodie – 2017-03-06T15:14:14.943

1much boss, such answer – mhz – 2017-07-12T15:50:45.227

The only issue is when starting from a blank file, the toggle won't work as there's no dir of the current buffer. – X.Arthur – 2019-03-25T14:15:51.250

2You 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 – 2013-11-25T02:26:15.090

26

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()

Lambart

Posted 2010-10-02T03:57:27.510

Reputation: 622

Ran 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 doing let l:curwinnr = winnr() before NERDTreeFind and exec l:curwnum . "wincmd w" after it. Solved all the issues. – Patrick – 2014-06-23T04:04:21.597

2Function name must start with a capital or "s:": rc:isNTOpen() Are you using a plugin extending scopes? – Brian Haak – 2017-08-03T20:40:55.537

1Sorry 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 the rc:, 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. :) – Lambart – 2017-08-03T20:59:41.377

@Lambart I just replaced it with s: – Brian Haak – 2017-08-03T21:36:10.333

@BrianHaak super. I hope it works for you! – Lambart – 2017-08-03T22:49:18.400

5

@Lambart I've created a usable config with all problems solved: https://gist.github.com/avesus/1954d9384d86cc1e39cb2b2eff7017b7

– Brian Haak – 2017-08-04T00:10:41.717

1Cool. I've been meaning for years to git-ify my various .rc files. Some day... – Lambart – 2017-08-04T17:46:41.677

1What is the purpose of isNTFocused()? Doesn't the &modifiable check cover that case? – jrdioko – 2013-06-28T19:28:44.580

@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 – 2013-12-21T19:02:14.633

2

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.

weynhamz

Posted 2010-10-02T03:57:27.510

Reputation: 129

Doesn'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 (https://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 – 2017-11-21T16:44:20.127

Yeah, this results in NERDTree always being open. Sadness. – Meredith – 2018-04-29T23:09:11.523

1

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

Casper Gerritsen

Posted 2010-10-02T03:57:27.510

Reputation: 111

1

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 commandline) 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

Matthias Braun

Posted 2010-10-02T03:57:27.510

Reputation: 572

What's <silent> key? – Diego Somar – 2020-02-03T12:02:04.650

That's not a key, it's a command.

– Matthias Braun – 2020-02-03T15:10:15.437

0

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

Keith Schaab

Posted 2010-10-02T03:57:27.510

Reputation: 1

0

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

j2fly

Posted 2010-10-02T03:57:27.510

Reputation: 11

Actually this could be a good solution for what was asked.. – alpha_989 – 2017-11-21T16:47:15.120

3

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 – 2013-08-30T05:04:55.073