我正在编写一个 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,因为所有窗口都没有文件名。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)