我正在编写一个 vim 插件,其中我需要检查用户正在查看的当前选项卡是否为空。如果它不为空,比如用户已经在查看缓冲区或有几个窗口,那么我想创建一个新的空选项卡并在那里使用我的插件。但如果它是空的,我想加载我的插件而不打开新选项卡。
我在文档中找不到任何合适的内容。
最佳答案
我对此唯一能想到的是使用 :windo
遍历当前选项卡中的所有窗口并检查是否加载了文件。像这样的事情:
function! TabIsEmpty() abort
" 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
如果唯一打开的是帮助页面或预览窗口或类似的东西,它将返回 0,因为它们有文件名。
如果您打开一个新选项卡然后拆分它,这仍然返回 1,因为所有窗口都没有文件名。
关于vim - 检查 Vim 中当前选项卡是否为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5025558/