使用外部文件完成Vim行

vbd*_*vbd 4 vim

可以使用行完成Ctrl+ X Ctrl+ L来显示来自特定外部文件的行完成,而不是来自当前缓冲区的"仅"吗?像词典,但线条.

更新:

测试我做了以下:

  • tt.txt用一些测试行创建了一个文件
  • 将文件放在D:\ t1 \中(我在Windows上)
  • 包含文件 :set path+=D:\\t1\\tt.txt
  • :set complete ? 回报 complete =.,w,b,u,t,i
  • :set path ? 回报 path=.,,,D:\t1\tt.txt
  • checkpath 返回:找到所有包含的文件
  • 键入一行,该行应使用来自tt.txt的匹配内容以Ctrl+ X Ctrl+ L返回返回模式

我错过了什么?

Pri*_*ash 7

我认为实现您想要的唯一方法是使用自定义完整功能.请参阅help complete-functions(非常有用!)文档.这是我尝试解决方案:

首先,您需要一个单独的函数来静默地为一个字符串grep一个文件(如果您只是调用裸vimgrep函数,如果没有匹配则会出现一个丑陋的错误).

function! SilentFileGrep( leader, file )
    try
        exe 'vimgrep /^\s*' . a:leader . '.*/j ' . a:file
    catch /.*/
        echo "no matches"
    endtry
endfunction
Run Code Online (Sandbox Code Playgroud)

现在,这是你的完成功能.请注意,您要搜索的文件的路径在此处是硬编码的,但如果您愿意,可以将其更改为使用变量.我们调用SilentFileGrep(),将结果转储到quickfix列表中.然后我们从qflist中提取结果(修剪前导空格)并在返回结果之前清除qflist.

function! LineCompleteFromFile(findstart,base)
    if a:findstart
        " column to begin searching from (first non-whitespace column):
        return match(getline("."),'\S')
    else
        " grep the file and build list of results:
        let path = <path_to_file>
        call SilentFileGrep( a:base, path )
        let matches = []
        for thismatch in getqflist()
            " trim leading whitespace
            call add(matches, matchstr(thismatch.text,'\S.*'))
        endfor
        call setqflist([])
        return matches
    endif
endfunction
Run Code Online (Sandbox Code Playgroud)

要使用此功能,您需要设置completefunc指向它的选项:

set completefunc=LineCompleteFromFile
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用它<C-X><C-U>来调用您可以轻松映射到的完成<C-X><C-L>.

这对我来说似乎很有效,但它并没有经过详尽的测试.