Check for current tab in vim

I am writing a vim plugin in which I need to check if the current tab is not a user. If it is not empty, let's say the user is already viewing the buffer or has several windows, then I want to create a new empty tab and work with my plugin. But if it is empty, I want to download my plugin without opening a new tab.

I did not find anything suitable in the documents, so will someone tell me how to do this?

Thank.

+5
source share
3 answers

The only thing I can think of is to use :windoiterate through all the windows on the current tab and check the file download. Something like that:

function! TabIsEmpty()
    " Remember which window we're in at the moment
    let initial_win_num = winnr()

    let win_count = 0
    " Add the length of the file name on to count:
    " this will be 0 if there is no file name
    windo let win_count += len(expand('%'))

    " Go back to the initial window
    exe initial_win_num . "wincmd w"

    " Check count
    if win_count == 0
        " Tab page is empty
        return 1
    else
        return 0
    endif
endfunction

" Test it like this:
echo TabIsEmpty()

" Use it like this:
if TabIsEmpty() == 1
    echo "The tab is empty"
else
    echo "The tab is not empty"
endif

- , , , 1, , windo .

+4

, , .

, , ​​. , . .

function! TabIsEmpty()
    return winnr('$') == 1 && len(expand('%')) == 0 && line2byte(line('$') + 1) <= 2
endfunction
+3

Perhaps I do not understand this question, but to check if the tab has a buffer, do the following:

if bufname("%") == ""
+3
source

All Articles