【问题标题】:vimscript: switch to buffer by filename pathvimscript:通过文件名路径切换到缓冲区
【发布时间】:2012-11-14 15:05:32
【问题描述】:

我有一个 vimscript 需要切换到特定的缓冲区。该缓冲区将由完整路径、部分路径或仅其名称指定。

例如:

我在目录 /home/user/code 中,我打开了 3 个 vim 缓冲区 foo.py src/foo.pysrc/bar.py

  • 如果脚本被告知切换到缓冲区/home/user/code/foo.py,它将切换到缓冲区foo.py

  • 如果它被告知切换到user/code/src/foo.py,它将切换到缓冲区src/foo.py

  • 如果它被告知切换到foo.py,它将切换到缓冲区foo.py

  • 如果它被告知切换到bar.py,它将切换到缓冲区src/bar.py

我能看到的最简单的解决方案是以某种方式获取存储在变量中的缓冲区列表并使用试错法。

如果解决方案是跨平台的就好了,但它至少需要在 Linux 上运行。

【问题讨论】:

    标签: path buffer filenames vim


    【解决方案1】:

    bufname() / bufnr() 函数可以通过部分文件名查找加载的缓冲区。您可以通过附加$ 将匹配锚定到末尾,如下所示:

    echo bufnr('/src/foo.py$')
    

    【讨论】:

    • 苦笑 我知道 vim 会有这样的功能。谢谢。
    【解决方案2】:

    我找到了一种在 vimscript 中使用 python 的方法。使用 python,我能够从 vim.buffers[i].name 获取所有缓冲区的名称,并使用 os.pathos.sep 来处理要切换到的缓冲区。

    最后,我决定如果它被请求切换到的缓冲区不明确,拒绝做任何事情会更有帮助。

    这里是:

    "Given a file, full path, or partial path, this will try to change to the
    "buffer which may match that file. If no buffers match, it returns 1. If
    "multiple buffers match, it returns 2. It returns 0 on success
    function s:GotoBuffer(buf)
    python << EOF
    import vim, os
    buf = vim.eval("a:buf")
    
    #split the paths into lists of their components and reverse.
    #e.g. foo/bar/baz.py becomes ['foo', 'bar', 'baz.py']
    buf_path = os.path.normpath(buf).split(os.sep)[::-1]
    buffers = [os.path.normpath(b.name).split(os.sep)[::-1] for b in vim.buffers]
    possible_buffers = range(len(buffers))
    
    #start eliminating incorrect buffers by their filenames and paths
    for component in xrange(len(buf_path)):
        for b in buffers:
            if len(b)-1 >= component and b[component] != buf_path[component]:
                #This buffer doesn't match. Eliminate it as a posibility.
                i = buffers.index(b)
                if i in possible_buffers: possible_buffers.remove(i)
    
    if len(possible_buffers) > 1: vim.command("return 2")
    #delete the next line to allow ambiguous switching
    elif not possible_buffers: vim.command("return 1") 
    else:
        vim.command("buffer " + str(possible_buffers[-1] + 1))
    EOF
    endfunction
    

    编辑: 上面的代码似乎有一些错误。我不会修复它们,因为还有另一个更好的答案。

    【讨论】:

      猜你喜欢
      • 2021-07-28
      • 2021-11-02
      • 1970-01-01
      • 2011-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-14
      • 2011-10-12
      相关资源
      最近更新 更多