【问题标题】:List @ tag auto completes in vim列表@标签在vim中自动完成
【发布时间】:2016-07-24 13:35:11
【问题描述】:

在 vim 中编辑时,我想在我的 markdown 文件中植入一些 @tags(例如 @sea_ice@models)。目前我正在使用 SuperTab 来完成普通单词。但是,如果我在@ 符号之后点击<tab>,它不会给我一个所有@tags 的列表,而是给我一个在当前上下文中找到的所有单词的长列表。

我注意到 SuperTab 允许自定义上下文定义,但是,由于我对 vim 脚本一无所知,并且文档仅包含 2 个示例,因此我无法自己编写脚本。

经过一番搜索,我想我可能需要定义一个新的自定义全能完整函数,特别是函数的第二半:

function! TagComplete(findstart, base) if a:findstart " locate the start of the word let line = getline('.') let start = col('.') - 1 while start > 0 && line[start - 1] != '@' let start -= 1 endwhile return start else " find @tag let res = [] ???? ???? endif return res endif endfun

这是我正在处理的代码。但我不知道如何测试它或放置它的正确位置。请帮忙

谢谢

【问题讨论】:

    标签: vim autocomplete tags supertab


    【解决方案1】:

    我从未使用过 SuperTab,所以我不知道是否以及如何使该解决方案与该插件一起使用,但是使用内置的手动完成非常容易。

    1. 如果不存在,则创建此目录结构:

      ~/.vim/after/ftplugin/
      
    2. ~/.vim/after/ftplugin/markdown.vim 中,添加这一行:

      setlocal define=@
      
    3. 在降价缓冲区中,输入@,然后按<C-x><C-d>

    请参阅 :help 'define':help ctrl-x_ctrl-d

    【讨论】:

    • 感谢您的回答。它有点工作,但不完全。当我在测试文件中有 6 个这样的 @tags 并使用您的方法时,完成只给了我 2 个选择(前 2 个),并且循环通过 2。是否有长度限制参数或什么?
    • 我发现define 用于标记定义。就我而言,我在一行中有多个@tags,因此无法识别所有后续标签。有没有办法解决这个问题?
    【解决方案2】:

    经过一番挣扎和寻求帮助,我想出了一个解决方案。

    首先创建一个completefunc,在当前文件中搜索@tags(致cherryberryterry:https://www.reddit.com/r/vim/comments/4dg1rx/how_to_define_custom_omnifunc_in_vim_seeking/):

    function! CompleteTags(findstart, base)
        if a:findstart
            return match(matchstr(getline('.'), '.*\%' . col('.') . 'c'), '.*\(^\|\s\)\zs@')
        else
            let matches = []
    
            " position the cursor on the last column of the last line
            call cursor(line('$'), col([line('$'), '$']))
    
            " search backwards through the buffer for all matches
            while searchpos('\%(^\|\s\)\zs' . (empty(a:base) ? '@' : a:base) . '[[:alnum:]_]*', 'bW') != [0, 0]
                let matches += [matchstr(getline('.'), '\%' . col('.') . 'c@[[:alnum:]_]*')]
            endwhile
    
            return filter(matches, "v:val != '@'")
        endif
    endfunction
    set completefunc=CompleteTags
    

    将以下内容放入.vimrc 以使用 SuperTab 设置制表符补全:

    function! TagCompleteContext()
        let line = getline('.')
        if line[col('.') - 2] == '@'
            return "\<c-x>\<c-u>"
        endif
    endfunction
    
    
    let g:SuperTabDefaultCompletionType = "context"
    let g:SuperTabCompletionContexts = ['TagCompleteContext', 's:ContextText']
    let g:SuperTabContextDefaultCompletionType = "<c-p>"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-22
      • 2023-03-30
      • 2011-11-05
      • 1970-01-01
      • 2015-02-18
      • 2011-07-22
      • 2011-10-22
      • 1970-01-01
      相关资源
      最近更新 更多