How can I make NERDTree to open on the same drive that the file that I'm editing?
Asked Answered
G

4

5

NERDTree shows in viewport disk c: regardless from which disk do I open the file.

When I use gvim in windows I open files using:

gvim.exe --remote-tab-silent [FILE]

I'm loading NERDTree with this line in _vimrc:

au VimEnter * NERDTree

Can NERDTree automaticaly change drive to correct drive somehow?

Great answered 11/10, 2010 at 15:28 Comment(0)
S
5

NERDTree provides several Ex commands to manipulate its buffer (see :help NERDTreeGlobalCommands). Among them there is the :NERDTreeFind command which behaves the same way as the :NERDTree command except it opens the NERDTree buffer in the directory containing currently opened file.

So, in order to achieve the desired effect described in the question, you can simply change the auto-command to read

:autocmd VimEnter * NERDTreeFind
Selfabuse answered 12/10, 2010 at 5:44 Comment(0)
O
6

Actually, my last answer does not work because once the NERDTree have been opened, it does not open again in the new buffer dir. It must work similarly to NERDTreeFind but it does not have a Toggle feature.

I made a function and mapped it to my key and now it works perfectly even opening the Ruby project if you have the vim-rails plugin.

Add this to your vimrc:

    function! NTFinderP()
    "" Check if NERDTree is open
    if exists("t:NERDTreeBufName")
        let s:ntree = bufwinnr(t:NERDTreeBufName)
    else
        let s:ntree = -1
    endif
    if (s:ntree != -1)
        "" If NERDTree is open, close it.
        :NERDTreeClose
    else
        "" Try to open a :Rtree for the rails project
        if exists(":Rtree")
            "" Open Rtree (using rails plugin, it opens in project dir)
            :Rtree
        else
            "" Open NERDTree in the file path
            :NERDTreeFind
        endif
    endif
endfunction


"" Toggles NERDTree
map <silent> <F1> :call NTFinderP()<CR>

It should work now.


Previous answer below:

You could map the key you use to open NERDTree like this(in .vimrc):

map <silent> <F1> :NERDTreeToggle %:p:h<CR>

This maps my F1 key to toggle(open/close) NERDTree using the path of the currently active buffer. If no buffer is open, it opens in the currently launched Macvim directory.

Overweight answered 13/11, 2010 at 0:55 Comment(2)
This was exactly what I was looking for, but since I'm on Windows and not a primary Rails developer, I made the "if (s:ntree != -1) else" simply call :NERDTreeFind.Jiggermast
Can someone help me understand what does "t:NERDTreeBufName" mean?Eventually
S
5

NERDTree provides several Ex commands to manipulate its buffer (see :help NERDTreeGlobalCommands). Among them there is the :NERDTreeFind command which behaves the same way as the :NERDTree command except it opens the NERDTree buffer in the directory containing currently opened file.

So, in order to achieve the desired effect described in the question, you can simply change the auto-command to read

:autocmd VimEnter * NERDTreeFind
Selfabuse answered 12/10, 2010 at 5:44 Comment(0)
H
1

I use mapping for NERDTree and in this way when I open it always opens in current dir

" NERDTree mappings
 nnoremap <silent> <F9> :NERDTreeToggle <cr>
 inoremap <silent> <F9> <Esc>:NERDTreeToggle <cr>

But if you open a file like gvim ~/other/dir/file NERDTree will open current dir from where gvim was called. So this is not a real solution to your problem.

Perhaps if you cd in working dir before calling gvim will solve your problem. In this case even your au VimEnter * NERDTree in _vimrc must work as you espect .

About changing directory and setting working dir set autochdir read here

Hypoxia answered 11/10, 2010 at 18:26 Comment(0)
Z
1

Add

au VimEnter,BufWinEnter * NERDTreeFind

to your .vimrc.

  • VimEnter part makes it work on load.
  • BufWinEnter makes it happen you open a new file.
  • * tells it to do this with all files
  • NERDTreeFind is the command to run

srcs: http://vimdoc.sourceforge.net/htmldoc/autocmd.html

Zima answered 16/8, 2012 at 21:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.