Vim NERDTree. How to prohibit duplicate files in tabs?

1.8k views Asked by At

I use NERDTree with the setting:

""""
" NerdTree
"
Bundle 'scrooloose/nerdtree'
Bundle 'jistr/vim-nerdtree-tabs'
map <F2> :NERDTreeTabsToggle<CR>

I can open any number of tabs with the same file by pressing 't'. For example:

|foo.txt|bar.txt|foo.txt|foo.txt|

How to prevent the opening of duplicate files? I want to open an existing buffer by pressing 't'.

3

There are 3 answers

0
uralbash On BEST ANSWER

I found the solution here https://github.com/scrooloose/nerdtree/issues/439

Grab the latest version and stick this in ~/.vim/nerdtree_plugin/override_tab_mapping.vim

https://gist.github.com/scrooloose/0495cade24f1f2ebb602

Thanks @moeabdol

5
Peter Rincker On

From what I understand NerdTree does not have such a behavior. I believe however what you are looking for is either :tab drop like @Ben mentioned or using :sb to switch buffers with the following setting: set swb=useopen,usetabe.

Personally I would suggest you use NerdTree for more of a File Explorer and less of a file/buffer manager. By leveraging Vim's buffer commands you can easily switch between buffers. Additionally by using Vim's buffer commands you can avoid the "one-to-one: file-to-tab relationship trap" that so many new vimmers get stuck on.

Aside about NerdTree

NerdTree is very helpful to explore a complex or unfamiliar file structure, but it comes at the cost of taking up screen real estate and disrupting buffer and window/split workflows. See Oil and vinegar - split windows and the project drawer for more. Using a nice fuzzyfinder plugin like CtrlP often takes the place of NerdTree for many people.

I have a nice post about NerdTree that might be of value: Files, Buffers, and Splits Oh My!

Aside about tabs

Vim's tabs are not like most text editors tab. They are more like viewports into a group of windows/splits. Additionally, Vim is buffer centric, not tab centric like most editors. Therefore using features like the quickfix list is often easier without tabs (See :h 'switchbuf if you must use tabs). Vim's tabs often get in the way of using a splits as there are better window and buffer navigation commands available. I personally have many files open (sometimes 100+) use no tabs and use on average 1-2 splits without any issue. Bottom line: read the following posts:

Best practices with Vim mappings

  • Supply a mode. So :map becomes :nmap
  • Unless using a <Plug> or <SID> mapping you should probably be using :noremap

By following these 2 rules your mapping will become:

nnoremap <f2> :NERDTreeTabsToggle<cr>
3
ladislas On

to open a new buffer, just press o