6

In NERDTree, if I select a file node and hit o, it opens the file in a window in the current tab. However, if that particular file is already open in another tab, NERDTree switches to that tab. I find this quite disorienting in my workflow.

Is there an option to tell NERDTree to stay in the current tab?


Update: I opened a pull request on the NERDTree repo at Github to add an option to prevent this tab-switching: https://github.com/scrooloose/nerdtree/pull/417

3 Answers 3

4

There is currently no way to do this.

From the source code (ref. lib/nerdtree/opener.vim line 238):

function! s:Opener._openFile()
    if self._reuse && self._reuseWindow()
        return
    endif

And the _reuseWindow() function (ref. line 309):

function! s:Opener._reuseWindow()
    "check the current tab for the window
    let winnr = bufwinnr('^' . self._path.str() . '$')
    if winnr != -1
        call nerdtree#exec(winnr . "wincmd w")
        call self._checkToCloseTree(0)
        return 1
    else
        "check other tabs
        let tabnr = self._path.tabnr()
        if tabnr
            call self._checkToCloseTree(1)
            call nerdtree#exec('normal! ' . tabnr . 'gt')
            let winnr = bufwinnr('^' . self._path.str() . '$')
            call nerdtree#exec(winnr . "wincmd w")
            return 1
        endif
    endif
    return 0
endfunction

It wouldn't be too hard, however, to simply remove the entire else block in the code above (or comment it out). Since we're on the Vim site, why not do it with Vim:

vim "+/function! s:Opener._reuseWindow()" "+/else" "+normal d9j" "+wq" lib/nerdtree/opener.vim

Run this command from the directory in which the NERDTree plugin is located to automagically remove this functionality.

1
  • Thanks very much. I was hoping there would be a better way or that someone had already forked NERDTree on Github to add such an option, but I'll modify my copy of NERDTree.
    – Morton Fox
    Feb 12, 2015 at 19:22
4

Yes there is a way! It's called silent open. In NERDTree focus your cursor on the file you want to open in new tab then press T. this will open it silently in a new tab without switching to it.

In NERDTree press ? to check out all the mappings available for you.

3
  • No, I actually want NERDTree to open the file in the current tab and only the current tab, without switching to an existing open buffer in a different tab. What T does is open the file in a new tab.
    – Morton Fox
    Apr 23, 2015 at 20:28
  • Yes! Sorry I didn't get it the first time; however, this is not implemented by default in NERDTree. By default NERDTree will switch you to the file if it already exist. Which is - to most of us - is the natural required behavior of any editor. According to my experience with NERDTree and its complement NERDTreeTabs (NERDTreeTabs makes NERDTree tab-aware, you have to install them both), you can have the same file open in multiple tab. Right now I can open a file in multiple tabs using T and t, but I have to explicitly tell NERDTree to do that.
    – moeabdol
    Apr 24, 2015 at 8:18
  • 1
    Actually I came looking for exactly this answer, even if it doesn't answer the OP question! Apr 23, 2020 at 19:05
0

you can try go , also you can add a new keymapping.

I just map gg key to do this, here is my config

call NERDTreeAddKeyMap({
        \ 'key': 'gg',
        \ 'callback': 'NERDTreeOpenHandler',
        \ 'quickhelpText': 'open in current tab',
        \ 'scope': 'FileNode' })

func! NERDTreeOpenHandler(node)
    call a:node.open({'stay': 1, 'where': 'p', 'keepopen': 1})
endf

English is not my mother tongue; please excuse any errors on my part.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.