Deal properly with multiple windows within a single tab #1

keflavich opened this Issue Jan 21, 2013 · 4 comments


None yet
2 participants

Here's how to recreate the bug:

  1. Open a fresh vim
  2. split the window either with :split, :new or probably some other commands
  • now you should have two windows open in the first tab
  1. open a new tab with :tabnew $MYVIMRC
  • now you should have a 2nd tab open to your ~/.vimrc (or $HOME_vimrc on windows)
  1. test your function with :echo WhichTab($MYVIMRC) and it should return "3".

You only have 2 tabs open, though, and tabs are numbered from [1, N)

The reason for the bug can be seen if you switch back to the first tab with the 2 windows in it. This tab has 2 open buffers, as you'll see if you say :echo tabpagebuflist(). For further clarification, check the output of :buffers in the first tab, and you'll see the 2 numbered buffers in those 2 windows.

NOTE: the list returned by WhichTabNo may contain more than one tab, because the same buffer may be open in more than one place. It will return them in left to right order with regards to the tab number. You could even have the same tab number a couple times in a row too if the same buffer is opened in more than one window in the same tab. For my needs, I decided that I would use the first (leftmost) tab found, so I return them in this order. If you would rather use the rightmost tab, you could look at the last item in the tabNo list returned, or you could change:

for tabNo in range(1, tabpagenr("$"))
for tabNo in range(tabpagenr("$"), 1, -1)

Also of note, I have a bug I'm working on in my script (which is pretty similar to yours) with regard to undisplayed buffers. If you open a file with :e somefile, and then change to a different file with :e someotherfile, then check the output of :buffers, you'll see that there's still a buffer for somefile, even though it's not currently open. I just need to figure out the best way to filter for those.

keflavich added a commit that referenced this issue Jan 21, 2013


keflavich commented Jan 21, 2013

OK, cool, that works. I've merged your code with mine (so that it searches for a buffer name instead of a buffer number). I don't deal with the case of the file being in a buffer but not a tab, though. What are you trying to filter for, in the case of closed files? Right now, they just aren't found in WhichTab.vim

Yeah, I think my bug was elsewhere and I was interpreting it wrong. Looks like we're both safe on that front.


keflavich commented Jan 21, 2013

OK, this one looks solved. Thanks.

@keflavich keflavich closed this Jan 21, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment