44

Is it possible to open NERDTree in every tab with pressing t or T in NERDTree, if yes, How?

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
tech.kapil
  • 563
  • 1
  • 5
  • 7
  • How do you open it in split view? It works, but it opened the file over it when i double clicked on the file. vim newbie here... – ma11hew28 Jun 23 '10 at 16:26

7 Answers7

114
autocmd VimEnter * NERDTree
autocmd BufEnter * NERDTreeMirror

edit: The above command seems to open the new tab in NERDTree's buffer. Instead use this as mentioned by wejrowski in the comment below :

autocmd BufWinEnter * NERDTreeMirror
Dustin
  • 1,157
  • 1
  • 7
  • 2
  • 7
    You add these to your ~/.vimrc – Kris Dec 15 '10 at 10:45
  • 6
    I did this and when I open a file through nerdtree in a new tab "t" it opens the file in a new tab in the side nerdtree panel and makes the main area blank. so there's no nerdtree anymore. – wejrowski Nov 20 '12 at 21:55
  • 6
    Hmm I fixed it by swapping "autocmd BufEnter * NERDTreeMirror" with "autocmd BufWinEnter * NERDTreeMirror".. seems like it was opening nerdTree then using that nerdtree window to open the buffer – wejrowski Nov 20 '12 at 23:30
  • 2
    this is the wrong answer. use nerdtreetabs. nerdtree is garbage without it – Andy Ray Feb 14 '13 at 19:10
  • Quickfix window doesn't work properly with that. – schmijos Jan 18 '22 at 14:57
65

I wrote a vim plugin that does this and also adds some goodies on top (i.e. keeps all trees in sync, ensures meaningful tab captions - not captions like 'NERD_tree_1' etc.).

It's here on Github: https://github.com/jistr/vim-nerdtree-tabs

Jiří Stránský
  • 1,331
  • 1
  • 10
  • 5
48
autocmd VimEnter * NERDTree
autocmd BufEnter * NERDTreeMirror

autocmd VimEnter * wincmd w

This one is a little better than Dustin's one because it places the cursor directly on the file you are intending to edit for quick edits. Thanks dustin for the original example ^^

Pedro
  • 1,001
  • 11
  • 16
7

A better solution is to open NERDTree only if there are no command line arguments set.

" Open NERDTree in new tabs and windows if no command line args set autocmd VimEnter * if !argc() | NERDTree | endif autocmd BufEnter * if !argc() | NERDTreeMirror | endif

NERDTree is e.g. not helpful if you do a git commit or something similiar.

shi
  • 480
  • 6
  • 10
6

This is probably not the best way, but if you edit plugin/NERDTree.vim and change this:

 exec "nnoremap <silent> <buffer> ". g:NERDTreeMapOpenInTab ." :call <SID>openInNewTab(0)<cr>"

to this:

 exec "nnoremap <silent> <buffer> ". g:NERDTreeMapOpenInTab ." :call <SID>openInNewTab(0)<cr>:NERDTree<cr>"

it will alter the binding of 't' in the NERDTree view to first open the file and then open NERDTree. Note, that the NERDTree views will not keep in sync.

Rytmis
  • 31,467
  • 8
  • 60
  • 69
6

How about toggling it.

" in .vimrc
" NERDTree, Use F3 for toggle NERDTree
nmap <silent> <F3> :NERDTreeToggle<CR>

In OSX, you just need to fn-F3 to toggle NERDTree.

shin
  • 31,901
  • 69
  • 184
  • 271
0

This problem was actually mentioned in the official Repository's Readme file including three situations related to opening NERDTree automatically:


How can I open a NERDTree automatically when vim starts up?

Stick this in your vimrc: autocmd vimenter * NERDTree


How can I open a NERDTree automatically when vim starts up if no files were specified?

Stick this in your vimrc:

autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 0 && !exists("s:std_in") | NERDTree | endif

Note: Now start vim with plain vim, not vim .


How can I open NERDTree automatically when vim starts up on opening a directory?

autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 1 && isdirectory(argv()[0]) && !exists("s:std_in") | exe 'NERDTree' argv()[0] | wincmd p | ene | endif

This window is tab-specific, meaning it's used by all windows in the tab. This trick also prevents NERDTree from hiding when first selecting a file.

千木郷
  • 1,595
  • 2
  • 19
  • 30