0

For example, if I do:

% vim file
:tabedit one
:split two

I end up with two tabs, and one tab with two windows.

How can I get the active window of the second tab? My goal is to get a list of all tabs with the currently active window/buffer, and should work in any context (i.e. not just when that tab is active).

2 Answers 2

2

The tabpagewinnr() can be used for this; for example:

:echo gettabinfo()
[{'windows': [1000],       'variables': {}, 'tabnr': 1},
 {'windows': [1002, 1001], 'variables': {}, 'tabnr': 2}]

:echo tabpagewinnr(1)
1
:echo tabpagewinnr(2)
1

In this case, the first window is active in both tabs; the "winnr" is scoped to a current tab and starts at 1, whereas the "window id" is global and starts at 1000. To transform a winnr to a windows ID you can use win_getid():

:echo tabpagewinnr(1)->win_getid(1)
1000
:echo tabpagewinnr(2)->win_getid(2)
1001

The argument for win_getid() is the tabnr; by default it will look up the winnr in the current tab so you need to specify it again.

For example to print all tabnrs with the currently active window's buffer name:

for t in gettabinfo()
    let w = tabpagewinnr(t.tabnr)->win_getid(t.tabnr)->getwininfo()[0]
    echo printf('%-4d %s', t.tabnr, bufname(w.bufnr))
endfor

Which should print something like:

1    file
2    two
2

In Vim the tabs are always numbered from 1 to tabpagenr("$").

Also, gettabinfo() returns window list ordered by winnr.

Given that, we are able to rewrite our logic like this

echo range(1, tabpagenr("$"))->map('tabpagewinnr(v:val)->win_getid(v:val)')

Or, alternatively,

echo gettabinfo()->map('v:val.windows[tabpagewinnr(v:key + 1) - 1]')

Both should produce list of active winids ordered by tab number.

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.