0

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'.

uralbash
  • 3,219
  • 5
  • 25
  • 45
  • I don't know whether NERDTree supports doing this. IF you were loading the file manually, you could use `:tab drop {filename}` to open the file in a new tab, or jump to it if already open. – Ben Nov 27 '14 at 17:42
  • 1
    I find your issue to be common among many nerdtree users. I have submitted an issue on Github https://github.com/scrooloose/nerdtree/issues/439 – moeabdol Apr 22 '15 at 20:19

3 Answers3

2

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

uralbash
  • 3,219
  • 5
  • 25
  • 45
1

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>
Community
  • 1
  • 1
Peter Rincker
  • 43,539
  • 9
  • 74
  • 101
  • The NERDTree aside is easily solved by a mapping for `NERDTreeToggle` in conjunction with `let NERDTreeQuitOnOpen = 1`. "If we select a file in the project drawer, which split window will it appear in?" The one where I punched ``. "It is easy to imagine a scenario where the user mistakenly thinks that window C has the focus, when in fact the focus belongs to window A"? No, not if NERDTree is only open when I want to open something. It's like "I forgot if I'm in insert or command mode" - not if you only go into insert when inserting, and `` the hell out the second you stop typing. Vim. – Amadan Nov 28 '14 at 01:57
  • @Amadan I agree with Drew Neil in his [Oil and Vinegar](http://vimcasts.org/blog/2013/01/oil-and-vinegar-split-windows-and-project-drawer/) blog post. I do not think your NerdTree setting really goes far enough. I personally use Tim Pope's [vinegar.vim](https://github.com/tpope/vim-vinegar). If you rather NerdTree instead of netrw dhruvasagar has a [NerdTree fork of vinegar.vim](https://github.com/dhruvasagar/vim-vinegar). – Peter Rincker Nov 28 '14 at 02:36
  • I agree to the point that that having a permanent project drawer is a bad idea in Vim. But can you give any complaints to my config (for a temporary drawer) that are more specific than that? Why do you think it does not go far enough? (For full disclosure, I must say that I do use Ctrl-P or plain `:e` with tab-completion a lot of the time, but I do like NERDTree for exploration, as the author says.) – Amadan Nov 28 '14 at 02:41
  • @Amadan, Personally I see little point in opening a temporary window when the window I am going to change will do just as well. It avoids disrupting my window layout, gives me a full window's width, and undo accidental exploring w/ a simple ``. If you like CtrlP and `:e` (check out `` as well) you are going to love [Projectionist](https://github.com/tpope/vim-projectionist). Projectionist is helpful if you have a structured project by creating custom navigation commands (with fuzzy completion!) as well as setting up alternative file navigation (Great for switching to test files). – Peter Rincker Nov 28 '14 at 08:38
  • The layout is restored as soon as I open something (or hit `` again), I know exactly where it will be and what its width will be (and a window might be tiny), and I use bufexplorer so accidental explore is still easy. I probably wouldn't mind having NERDTree in-window, it would make it a bit similar to bufexplorer, but I'm already used to it. :) – Amadan Nov 28 '14 at 09:00
0

to open a new buffer, just press o

ladislas
  • 3,037
  • 19
  • 27
  • It rewrite current buffer. I want open new tab if not exist or open exist tab. – uralbash Nov 27 '14 at 15:29
  • @uralbash what do you mean "it rewrites current buffer"? it opens the file in a new buffer if it does not exist or opens the existing buffer. – ladislas Nov 27 '14 at 15:55
  • 1
    @uralbash: You seem to be confused about the difference between buffers and windows. – Amadan Nov 28 '14 at 01:59