检查vim中当前选项卡是否为空

Shr*_*rat 6 vim

我正在编写一个vim插件,我需要检查用户正在查看的当前选项卡是否为空.如果它不是空的,比如说用户已经在查看缓冲区或者有几个窗口,那么我想创建一个新的空选项卡并在那里处理我的插件.但如果它是空的,我想加载我的插件而不打开新标签.

我在文档中找不到合适的内容,所以任何人都知道如何做到这一点?

谢谢.

DrA*_*rAl 5

为此,我唯一想到的就是用于:windo遍历当前选项卡中的所有窗口并检查是否已加载文件。像这样:

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
Run Code Online (Sandbox Code Playgroud)

如果唯一打开的是帮助页面或预览窗口之类的东西,它可能会返回1,因为我认为 windo这些操作不起作用。


Kur*_*urt 5

也许我不理解这个问题,但要检查选项卡是否没有缓冲区,请执行以下操作:

if bufname("%") == ""
Run Code Online (Sandbox Code Playgroud)