检查当前选项卡在 Vim 中是否为空 [英] Check if current tab is empty in Vim
问题描述
我正在编写一个 vim 插件,我需要在其中检查用户正在查看的当前选项卡是否为空.如果它不为空,比如用户已经在查看缓冲区或有几个窗口,那么我想创建一个新的空选项卡并在那里工作我的插件.但是如果它是空的,我想在不打开新标签的情况下加载我的插件.
I am writing a vim plugin in which I need to check if the current tab the user is looking at is empty. If it is not empty, like say the user is already viewing a buffer or has a couple of windows, then I want to create a new empty tab and work my plugin there. But if it is empty, I want to load my plugin without opening a new tab.
我在文档中找不到任何合适的内容.
I couldn't find anything appropriate in the docs.
推荐答案
我唯一能想到的就是使用 :windo
遍历当前选项卡中的所有窗口并检查文件是否已加载.像这样:
The only thing that I can think of for this is to use :windo
to iterate through all the windows in the current tab and check whether a file is loaded. Something like this:
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
对它们进行操作.
If the only thing open is a help page or preview window or something like that, it will probably return 1 as I don't think windo
operates over those.
这篇关于检查当前选项卡在 Vim 中是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!