VIM:检查当前选项卡中是否打开了文件?窗户?(并激活它)

VIM:检查当前选项卡中是否打开了文件?窗户?(并激活它),vim,Vim,在vim中,您可以使用bufexists检查当前缓冲区中是否打开了文件。对于短文件名(不是完整路径),可以使用bufexists(bufname('filename'))检查它是否打开 有没有办法检查文件是否在选项卡中打开 我最接近的解决方法是执行以下操作: :tabdo if bufnr(bufname('filename')) in tabpagebuflist(): echo "Yes" 然而,这是一种Python伪代码。。。我不知道如何让它在vim中工作。我的目标是让外部applesc

在vim中,您可以使用
bufexists
检查当前缓冲区中是否打开了文件。对于短文件名(不是完整路径),可以使用
bufexists(bufname('filename'))
检查它是否打开

有没有办法检查文件是否在选项卡中打开

我最接近的解决方法是执行以下操作:

:tabdo if bufnr(bufname('filename')) in tabpagebuflist(): echo "Yes"
然而,这是一种Python伪代码。。。我不知道如何让它在vim中工作。我的目标是让外部applescript检查文件是否已打开,如果已打开,则转到该文件中的一行


理想情况下,我也希望能够搜索不同的GUI窗口,但我发现(例如)在VIM中使用不同的GUI窗口是非常具有挑战性的/不可能的。

我的不耐烦和良好的文档让我感觉更好。。。以下是解决方案(在和的帮助下)。消息来源是

然后像这样使用:

exec "tabnext ".WhichTab('my_filename')

echo remote_foreground( WhichWindow('my_filename') )
或者,从命令行,这里有一个脚本,使用
WhichTab
转到文件的特定行:

#!/bin/bash

file="$1"
line="$2"

for server in `mvim --serverlist` 
do
    foundfile=`mvim --servername $server --remote-expr "WhichTab('$file')"`
    if [[ $foundfile > 0 ]]
    then
        mvim --servername $server --remote-expr "foreground()" 
        mvim --servername $server --remote-send ":exec \"tabnext $foundfile\" <CR>"
        mvim --servername $server --remote-send ":$line <CR>"
    fi
done
#/bin/bash
file=“$1”
第行=“2美元”
对于'mvim--serverlist'中的服务器
做
foundfile=`mvim--servername$server--remote expr“WhichTab('$file')”`
如果[$foundfile>0]]
然后
mvim--servername$server--remote expr“前台()”
mvim--servername$server--remote send“:exec\“tabnext$foundfile”
mvim--服务器名$server--远程发送“:$line”
fi
完成

我想回复keflavich,但我还不能

我正在研究一个类似的问题,我想模仿gvim的行为——在gvim中打开文件时,远程选项卡保持沉默。我找到了您的WhichTab脚本,但在任何给定选项卡中打开多个窗口时遇到了问题。如果在选项卡内拆分窗口,则tabpagebuflist()将返回多个缓冲区,因此使用缓冲区编号在列表中的位置的方法不起作用。这是我的解决方案,可以解释这种可能性

" Note: returns a list of tabnos where the buf is found or 0 for none.
"               tabnos start at 1, so 0 is always invalid
function! WhichTabNo(bufNo)
    let tabNos = []
    for tabNo in range(1, tabpagenr("$"))
        for bufInTab in tabpagebuflist(tabNo)
            if (bufInTab == a:bufNo)
                call add(tabNos, tabNo)
            endif
        endfor
    endfor
    let numBufsFound = len(tabNos)
    return (numBufsFound == 0) ? 0 : tabNos
endfunction

我想我可以返回tabNos,它将是一个空列表,被计算为标量0,但我刚刚学习了vimscript,对其动态键入行为的细节还不太熟悉,所以我现在就这样离开它。

好东西。我已将macvim skim移到github:如果您想尝试将其中一些内容添加为PR,我将稍后尝试添加。@cpstubing06-您能用一个示例案例更新上的问题,以便我可以测试一下您的代码吗?我已经将您的函数添加到
WhichTab.vim
,但它现在没有使用。我认为这将是非常有用的
命令-nargs=1-complete=file TN:let tab=WhichTab()如果tab==0:tabnew:else:exe'norm'.tab.gt'endif
仅当包含该文件的选项卡不存在时,才使用该命令创建新选项卡。
" Note: returns a list of tabnos where the buf is found or 0 for none.
"               tabnos start at 1, so 0 is always invalid
function! WhichTabNo(bufNo)
    let tabNos = []
    for tabNo in range(1, tabpagenr("$"))
        for bufInTab in tabpagebuflist(tabNo)
            if (bufInTab == a:bufNo)
                call add(tabNos, tabNo)
            endif
        endfor
    endfor
    let numBufsFound = len(tabNos)
    return (numBufsFound == 0) ? 0 : tabNos
endfunction