vim-nerdtree-tabs icon indicating copy to clipboard operation
vim-nerdtree-tabs copied to clipboard

Double nerdtree when passing directory as argument

Open nybblr opened this issue 12 years ago • 18 comments

Love the plugin -- makes my NERD life that much better =)

Whenever I call: $ mvim /path/to/dir

Two NERDTree windows are opened: one on the left that is at my home directory, and a big one on the right that is cd'ed to the directory I passed in. It seems the left nerdtree also sets the PWD last, so if I focus from the left tree to the right tree, the directory is suddenly my home directory instead of the directory I pass in.

The left NERDtree is obviously generated by this plugin: if I open a new tab, it mirrors that one. The one on the right (takes up most of the screen) is what NERDTree auto loads, probably because it tries to take place of netrw, the default vim dir explorer.

Obviously this behavior is irritating...I like the behavior I get if I just run mvim with no args: then it opens this plugin's nerdtree window to the terminal's pwd, and opens a blank buffer on the left. What needs to be changed to fix the behavior to match this when given a directory? I'll gladly help out, but I'm somewhat new to vim plugin architecturing so a push in the right direction would be great!

Just FYI this only happens with directories: if I give a file, it uses the directory as the PWD and root of the nerdtree (which your plugin opens without competition from the netrw hook) as it should. It just misbehaves when I use a directory as the argument.

Thanks! ~ Jonathan Martin

nybblr avatar Oct 08 '12 22:10 nybblr

I've solved this issue already, then I had to move to a different loading method (moved the script from plugin dir to nerdtree_plugin dir to fix some issue) and then this got bad again I think. For me, the loading process here is like black magic :) Netrw + default NERDTree behavior + this plugin's behavior fight against each other. Nowadays I simply cd into the directory and run gvim there :D

If you want to dive into this by any chance, be aware that this...

The left NERDtree is obviously generated by this plugin: if I open a new tab, it mirrors that one.

... might not necessarily be a correct statement. NERDTreeMirror command is used for mirroring. It is present in default NERDTree distro and I'm not sure what is the rule for picking which buffer will be mirrored.

jistr avatar Oct 22 '12 20:10 jistr

Hmm, black magic =P I'm not so good in that field! =)

I've been doing cd myself, it just gets irritating when I really don't want to open another terminal, cd to my last directory, and then cd to the subdirectory I was interested in. I run into this often when I'm working with my dotfiles, and want to open mvim to a plugin's directory. Maybe there's a way to intercept the vim args and run cd, then open vim normally? A little hacky, but then you could definitely get consistent behavior.

Just some thoughts; what loading method worked at first?

~ Jonathan

nybblr avatar Oct 22 '12 20:10 nybblr

I won't recall from memory, but apparently it was working with v1.1.2: https://github.com/jistr/vim-nerdtree-tabs/issues/6#issuecomment-3527072

Here are the tagged versions: https://github.com/jistr/vim-nerdtree-tabs/tags

jistr avatar Oct 23 '12 16:10 jistr

By the time of v1.1.2, nerdtree-tabs was loaded from plugin directory, so it was loaded before nerdtree itself, but this caused some issues. So nerdtree-tabs had to be moved to nerdtree_plugin directory later, which loads after loading nerdtree. So the mechanism for initial nerdtree opening from v1.1.2 might be impossible to replicate to current master, but I'm not absolutely sure about that.

jistr avatar Oct 23 '12 17:10 jistr

Ugh, got autoclosed. Reopening because the two nerdtrees still do appear. But now they both point into a correct directory at least :)

jistr avatar Jan 26 '13 09:01 jistr

This is a real bummer +1 for a fix.

estk avatar Sep 10 '13 02:09 estk

Any sort of workaround? Pulling version v1.1.2 did not seem to help.

bendycode avatar Dec 27 '13 21:12 bendycode

:+1:!

ELLIOTTCABLE avatar Feb 08 '15 09:02 ELLIOTTCABLE

+1 for this error, it's really annoying. Love the plugin though, makes using vim a lot easier! I was thinking if there was a way to stop NERDTree opening by default, and only have NERDTreeTabs open by default, would that be a workaround for now?

tomoakley avatar Apr 12 '15 23:04 tomoakley

I am running into a similar problem as described here, when I first install this plugin today.

In my ~/.vimrc, I have this line

g:nerdtree_tabs_open_on_console_startup = 1

When opening a new vim session with the directory, i.e. vim ., I see the NERDTree smaller window on the left, as well as the big one on the right.

Now, when I open a file from the window on the left, it then opens as a smaller window at the bottom half of the large window. See the screenshot here:

screen shot 2015-10-06 at 6 15 28 pm

This is sort of annoying as I then have to go up and close out that window explicitly first. Is there any NERDTreeTabs or NERDTree option to disable this behavior?

tnguyen14 avatar Oct 06 '15 22:10 tnguyen14

I'm not sure if this will help anyone in this thread, but this is what I'm doing to handle duplicate NERDTree / netrw windows on startup:

let g:nerdtree_tabs_open_on_gui_startup = 0
let g:nerdtree_tabs_open_on_console_startup = 0

autocmd AuNERDTreeCmd VimEnter * call s:NERDTreeTabsOpen(expand("<amatch>"))

" If vim is started with a file, simply show that file
" If vim is started with nothing or a directory,
" open NERDTree and close any netrw buffers
function s:NERDTreeTabsOpen(directory)
  let l:main_bufnr = bufnr('%')
  if empty(a:directory) || isdirectory(a:directory)
    NERDTreeTabsOpen
    exe bufwinnr(l:main_bufnr) . "wincmd w"
    bd
  endif
endfunction

mikew avatar Feb 05 '16 16:02 mikew

This configuration doesn't really fix the problem but the result seems to be the closest thing to what people want.

" Run NERDTreeTabs on console vim startup.
" When set to 2, open only if directory was given as startup argument.
let g:nerdtree_tabs_open_on_console_startup = 2

" On startup, focus NERDTree if opening a directory, focus file if opening a file.
let g:nerdtree_tabs_smart_startup_focus = 1

" Open NERDTree Tabs on new tab
let g:nerdtree_tabs_open_on_new_tab = 1

" Make sure that Focus is on the file window when switching into a tab
let g:nerdtree_tabs_focus_on_files = 1

" Automatically find and select currently opened file in NERDTree.
let g:nerdtree_tabs_autofind = 1

hql287 avatar Oct 30 '16 20:10 hql287

@mikew doesn't seem to work.

hql287 avatar Oct 30 '16 20:10 hql287

Sorry to resurrect this, but I'm getting the same - does anyone know how to open with an empty buffer when you open a directory?

I previously used Janus where things worked fine... but now I decided to go with a stock Vim (MacVim) install. I've tried copying their code but I'm still getting two windows with NerdTree opening when opening with a directory.

Here's the Janus code: https://github.com/carlhuda/janus/blob/f285edf1533eaecf526dabe0962dcd5107319af7/janus/vim/tools/janus/after/plugin/nerdtree.vim

And my own .vimrc:

" Specify a directory for plugins (for Neovim: ~/.local/share/nvim/plugged)
call plug#begin('~/.vim/plugged')

" Make sure you use single quotes

Plug 'ap/vim-css-color'
Plug 'scrooloose/nerdtree'

" On-demand loading
" Plug 'scrooloose/nerdtree', { 'on':  'NERDTreeToggle' }

" Initialize plugin system
call plug#end()

let g:NERDTreeWinPos = "right"
autocmd vimenter * NERDTree
autocmd BufWinEnter * NERDTreeMirror
set guioptions-=T " Removes top toolbar
set guioptions-=r " Removes right hand scroll bar
set go-=L " Removes left hand scroll bar
autocmd User Rails let b:surround_{char2nr('-')} = "<% \r %>" " displays <% %> correctly
set cpoptions+=$ " puts a $ marker for the end of words/lines in cw/c$ commands
let mapleader="," " Change mapleader from \ to ,
"autocmd BufWritePre *.rb :%s/\s\+$//e
autocmd BufWritePre * :%s/\s\+$//e
set shortmess+=A " removes swap file warnings

"Better window navigation
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-h> <C-w>h
nnoremap <C-l> <C-w>l

set splitbelow
set splitright

AstonJ avatar Jan 13 '17 02:01 AstonJ

This has bothered me for a long time. @mikew's code didn't work for me (I think he's using Judas) but I used it as a starting point for my own fix. I'm completely new to VimScript so it can probably be improved but works for me. Using default NERDTreeTabs config just add these lines:

autocmd VimEnter * call CloseExtraNERDTree()

" If vim is started with a file, simply show that file
" If vim is started with nothing or a directory close extra NERDTree buffer
function CloseExtraNERDTree()
  wincmd l " move to right pane
  let l:main_bufnr = bufnr('%') 
  let l:fname = expand('%') " get name of current buffer
  if l:fname ==# 'NERD_tree_1'
    exe bufwinnr(l:main_bufnr) . "wincmd w"
    bd
  endif
endfunction

sfcgeorge avatar Mar 01 '17 11:03 sfcgeorge

I have found hijacking the highjacking code nerdtree uses to override netrw seems to work. Simply paste this into your .vimrc:

" Highjack nerdtree's highjacking to keep normal nerdtree from loading on directories
let g:NERDTreeHijackNetrw=0
augroup NERDTreeHijackNetrw
    autocmd VimEnter * silent! autocmd! FileExplorer
augroup END

This keeps nerdtree from trying to replace netrw while also replacing netrw with an empty buffer.

See: https://github.com/scrooloose/nerdtree/blob/f26eaf83551631e683e339f00e2c97bd6e8356d7/plugin/NERD_tree.vim#L168

robo-corg avatar Apr 26 '17 01:04 robo-corg

@robo-hamburger's solution seems to work for me!

chrishaig14 avatar Oct 07 '18 16:10 chrishaig14

Has anyone been working on a more permanent solution for this?

jsrj avatar Aug 26 '20 20:08 jsrj