I'd like to know how many windows are open in the current tab page from a Vim function; in particular, it would be handy to know if a particular window is the last, eg from an autocmd, in a tab page. Any ideas?
2 Answers
I'm guessing you can do it all with the winnr() command.
winnr() by itself tells you the window number you are currently in. winnr('$') tells you the last window (or window count)
The following would return '1' if you were in the last window, and 0 otherwise:
echo winnr() == winnr('$')
Taking your example you could then do something like this to execute something only on the last window:
:autocmd WinEnter * if winnr() == winnr('$')|echo "Welcome to the last window"|endif
-
4Actually according to vim documentation (vimdoc.sourceforge.net/htmldoc/eval.html#winnr%28%29) winnr('$') IS the number of windows on current tab. Jul 2, 2014 at 18:54
You could also do the following:
let window_counter = 0
windo let window_counter = window_counter + 1
echo window_counter
The :windo
command runs an ex command in each window of your current tab.
-
winnr('$') is sufficient solution. Besides, your code will result in selecting last windows which is rarely required effect of getting window count. Jul 2, 2014 at 18:56
-
@AmokHuginnsson, this solution is sufficient also. Your first point is not really clear. The second point makes sense, though. Aug 11, 2020 at 12:11