Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Question: Ability to always use existing window/buffer/tab when possible. #237

jordwalke opened this Issue Feb 2, 2013 · 6 comments


None yet
1 participant


I've found the plugin Ctrl-p to pair very nicely with NERDTree. One thing that Ctrl-p does well, is that it has a mode where all open commands will find the existing file/buffer/tab whenever possible. This means that if you do a vertical split "s", it will only perform the vertical split if that file is not already present somewhere else. If it is present somewhere else, it instead will jump to that file. In other words, the type of open command "s/t" is just a fallback in case the file cannot be found globally. Is it possible to configure NERDTree to do the same?

Any thoughts from @scrooloose?

Something that would tie most people over, would be a way to, instead of executing a Vim command to open the file, simply allow a custom application uri handler scheme. Meaning all files will open mvim:///Path/to/file.txt - so people who are using macvim, or other similar vim distros will get this behavior for free. Macvim will always focus the existing file if it is already open.

Currently, o works this way but split/new_tab do not.

Anyone have thoughts here? It's really difficult to remember which files you already have open when you have more than ten or so.

@scrooloose: Any suggestions about where I can hack this myself?

I dug up an old discussion where someone mentioned how to perform this behavior. It is accomplished through custom mappings - that cannot be specified in your .vimrc - they must be specified in a special ~/.vim/nerdtree_plugin/myMappings.vim file as follows:

let g:NERDTreeCustomReuseWindows = '1'
call NERDTreeAddKeyMap({
       \ 'key': 'w',
       \ 'scope': 'all',
       \ 'callback': 'NERDTreeCustomToggleReuse',
       \ 'quickhelpText': 'Toggle use existing windows' })

function! NERDTreeCustomToggleReuse()
    let g:NERDTreeCustomReuseWindows = g:NERDTreeCustomReuseWindows ? 0 : 1
    echomsg (g:NERDTreeCustomReuseWindows ? 'Reusing' : 'Not reusing') . ' existing windows'

call NERDTreeAddKeyMap({
       \ 'key': 'i',
       \ 'scope': 'FileNode',
       \ 'callback': 'NERDTreeCustomOpenSplit',
       \ 'quickhelpText': 'open split reusing if able' })

function! NERDTreeCustomOpenSplit(node)
    call a:node.open({'where': 'h', 'reuse': g:NERDTreeCustomReuseWindows})

call NERDTreeAddKeyMap({
       \ 'key': 's',
       \ 'scope': 'FileNode',
       \ 'callback': 'NERDTreeCustomOpenVSplit',
       \ 'quickhelpText': 'open vsplit reusing if able' })

function! NERDTreeCustomOpenVSplit(node)
    call a:node.open({'where': 'v', 'reuse': g:NERDTreeCustomReuseWindows})

call NERDTreeAddKeyMap({
       \ 'key': '<ENTER>',
       \ 'scope': 'FileNode',
       \ 'callback': 'NERDTreeCustomOpenInTab',
       \ 'quickhelpText': 'open in new tab reusing if able' })

" If you're opening a new instance of vim - hitting enter should open in the
" first buffer not a new tab! #dotherightthing
function! NERDTreeCustomOpenInTab(node)
    call a:node.open({'where': tabpagenr('$') == 1 && winnr('$') == 1 ? 'p' : 't', 'reuse': g:NERDTreeCustomReuseWindows })

call NERDTreeAddKeyMap({
       \ 'key': 'T',
       \ 'scope': 'FileNode',
       \ 'callback': 'NERDTreeCustomOpenInTabSilent',
       \ 'quickhelpText': 'open in new background tab reusing if able' })

function! NERDTreeCustomOpenInTabSilent(node)
    call a:node.open({'where': 't', 'stay': 1, 'reuse': g:NERDTreeCustomReuseWindows})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment