加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-15 22:06:38 所属栏目:安全 来源:网络整理
导读:在 vim中,您可以使用bufexists检查当前缓冲区中的文件是否已打开.对于短文件名(非完整路径),您可以使用bufexists检查它是否打开(bufname(‘filename’)). 有没有办法检查文件是否在选项卡中打开? 我最接近的解决方法是做类似的事情: :tabdo if bufnr(bufna
在 vim中,您可以使用bufexists检查当前缓冲区中的文件是否已打开.对于短文件名(非完整路径),您可以使用bufexists检查它是否打开(bufname(‘filename’)).

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

我最接近的解决方法是做类似的事情:

:tabdo if bufnr(bufname('filename')) in tabpagebuflist(): echo "Yes"

然而,那种pythonic伪代码…我不知道如何让它在vim中工作.我的目标是使用外部AppleScript检查文件是否已打开,如果是,则转到该文件中的一行.

理想情况下,我也希望能够搜索不同的GUI窗口,但我收集(例如Open vim tab in new (GUI) window?)在VIM中使用不同的GUI窗口非常具有挑战性/不可能.

解决方法

我的不耐烦和良好的文档使我变得更好……这是解决方案(在 Check if current tab is empty in vim和 Open vim tab in new (GUI) window?的帮助下).来源是 https://github.com/keflavich/macvim-skim

function! WhichTab(filename)
    " Try to determine whether file is open in any tab.  
    " Return number of tab it's open in
    let buffername = bufname(a:filename)
    if buffername == ""
        return 0
    endif
    let buffernumber = bufnr(buffername)

    " tabdo will loop through pages and leave you on the last one;
    " this is to make sure we don't leave the current page
    let currenttab = tabpagenr()
    let tab_arr = []
    tabdo let tab_arr += tabpagebuflist()

    " return to current page
    exec "tabnext ".currenttab

    " Start checking tab numbers for matches
    let i = 0
    for tnum in tab_arr
        let i += 1
        echo "tnum: ".tnum." buff: ".buffernumber." i: ".i
        if tnum == buffernumber
            return i
        endif
    endfor

endfunction

function! WhichWindow(filename)
    " Try to determine whether the file is open in any GVIM *window*
    let serverlist = split(serverlist(),"n")

    "let currentserver = ????
    for server in serverlist
        let remotetabnum = remote_expr(server,"WhichTab('".a:filename."')")
        if remotetabnum != 0
            return server
        endif
    endfor

endfunction

然后像这样使用:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读