I usually open each buffer in a separate tab-page (using NERDTree as directory navigator) since I feel more confortable this way.

But NERDTree always open new tab-page after the current one. Is there a way to make NERDTree always open new tab as last one ?

Manually this can be achivied using :$tabmove command, but I would like to automate this task.

For clearness, I post an example. In the following screenshot I am in NERDTree window with minilexer.py open in same tab-page #1. verilog_parser.py in the next one, #2.

Thus having:

Tab page #1: minilexer.py
Tab page #2: verilog_parser.py

enter image description here

Now, if I open vhdl_parser.py from tab-page #1 (exactly as in the screenshot), the new tab will be placed between #1 and #2, thus having:

Tab page #1: minilexer.py
Tab page #2: vhdl_parser.py
Tab page #3: verilog_parser.py

But I want to be:

Tab page #1: minilexer.py
Tab page #3: verilog_parser.py
Tab page #3: vhdl_parser.py

Always. So the last tab opened should always have tabepagenr('$').

In pseudocode should be: If the buffer does not exists and it's not open in a tab, open it and make the tab the last one ($tabmove).

I am actually looking for the function that returns the path under the cursor in NERDTree..


1 Answer 1


You could do the following:

let g:NERDTreeMapOpenInTab="a"
autocmd FileType nerdtree nmap <buffer> t a:$tabmove<CR>

It change the NERDTree default mapping use a to do the normal "Open In Tab" of NERDTree.

It introduces a NERDTree additional mapping to t that combine the two actions you want to make.

In case you would like additionally to switch to the tab that contains the buffer in case the buffer is already opened in some tab instead of creating a new tab you could do:

function! GotoTab()
  let bufnr = tabpagebuflist()[0]
  let tabnr = tabpagenr()
  for tabindex in range(1, tabpagenr('$'))
    if tabnr == tabindex
    let bufnrs = tabpagebuflist(tabindex)
    let bufindex = index(bufnrs, bufnr)
    if bufindex >= 0
      execute "tabn" .. tabindex
      execute "tabclose" .. tabnr

let g:NERDTreeMapOpenInTab="a"
autocmd FileType nerdtree nmap <buffer> t a:call GotoTab()<CR>
  • 1
    This command will move every tab I open (even for these buffers that are already open) to last one. I would like to do this only for new buffers.
    – xyx
    Commented Feb 2 at 14:28
  • I understand. I'll see if that could be improved. Commented Feb 2 at 14:30
  • 1
    I have added a small procedure that verify is the buffer is already present in another tab and switch to that tab in such case. Let me know if it works for you like it works for me :-) Commented Feb 2 at 17:08
  • 1
    I updated the question with a better explanation. It seems that your previous comment matches what I want to achieve.
    – xyx
    Commented Feb 2 at 17:11
  • 1
    Thanks for the feedback. If it solves your problem I'll be glad if you could accept the answer using the v button next to the arrow voting buttons. It allow the question to rest :-) Commented Feb 2 at 17:14

Your Answer

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

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