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

StackOverflow https://stackoverflow.com/questions/3907639

  •  29-09-2019
  •  | 
  •  

Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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.

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top