Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number of Windows in VIM

Tags:

vim

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?

like image 343
Matt Luongo Avatar asked Nov 16 '10 20:11

Matt Luongo


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
like image 80
dsummersl Avatar answered Oct 20 '22 17:10

dsummersl


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.

like image 39
Benoit Avatar answered Oct 20 '22 18:10

Benoit