nerdtree icon indicating copy to clipboard operation
nerdtree copied to clipboard

Switch to last active buffer before opening tab

Open dweipert-3138720606 opened this issue 3 years ago • 1 comments

When switching to a new tab with t the tabline says NERD_tree_1.

How would I configure nerdtree to switch to the last active buffer before opening the new tab, so the message on the tab is more meaningful?

Seems like there's no config value or option for that.

Maybe with some autocmd for TabLeave or TabEnter?

dweipert-3138720606 avatar Mar 08 '22 09:03 dweipert-3138720606

I use the following code in my .vimrc

function MoveCursor()
	let tab = tabpagenr()
	let buflist = tabpagebuflist(tab)

	for b in buflist
		let buftype = getbufvar(b, "&filetype")
		if buftype != 'nerdtree'
			let bufnr = b
 			break
		elseif b == buflist[-1]
			let bufnr = b
		endif
	endfor

	let nr = bufwinnr(bufnr)
	exe nr . "wincmd w"
endfunction

Note though it doesn't switch to the last active buffer but to the first buffer in the tab that is not nerdtree.

ghost avatar May 02 '22 22:05 ghost

If you don't mind nerdtree closing you can use the NERDTreeQuitOnOpen open to achieve the desired effect in the tabbar.

imLew avatar Jan 04 '23 10:01 imLew

Thanks @AvSaba ! Switching to a buffer that is not nerdtree works for me! Based on your code I wrote my own function that does basically the same as yours, just with more descriptive variables, because I love those:) (and stepping over "help" buffers as well)

function NERDTreeStatusLineTabSwitch()
  let tab_nr = tabpagenr()
  let buffer_list = tabpagebuflist(tab_nr)

  " default to 2
  let meaningful_window_nr = 2

  for buffer_nr in buffer_list
    let buffer_type = getbufvar(buffer_nr, "&filetype")
    if buffer_type != "nerdtree" && buffer_type != "help"
      let meaningful_window_nr = bufwinnr(buffer_nr)
      break
    endif
  endfor

  exe meaningful_window_nr .. "wincmd w"
endfunction
autocmd TabLeave * call NERDTreeStatusLineTabSwitch()

dweipert-3138720606 avatar Jan 10 '23 08:01 dweipert-3138720606