Monday, July 21, 2014

Check number of windows in vim using python

As a test just open a file x.vim with the following content:

function! Test()
python << EOF
import vim
n = vim.eval("winnr('$')")
print(n)
EOF
endfunction

Now in vim exec source % to source the file, then use the function we defined call Test(), you will see the number 1 in the mini buffer.

Now do vsplit to get another window and call Test() again, you will see the number 2 there. Amazing!

To make things even more clear, change the above function into this:

function! Test()
python << EOF
import vim
n = vim.eval("winnr('$')")
n = int(n)
if n == 1:
    print("You only have 1 window!")
else:
    print("You have multiple windows!")
EOF
endfunction

Enjoy vimming!

0 comments: