繁体   English   中英

List @标签自动在Vim中完成

[英]List @ tag auto completes in vim

在vim中进行编辑时,我@models在markdown文件中@tags许多@tags (例如@sea_ice@models )。 目前,我正在使用SuperTab来制表完整的普通单词。 但是,如果我在@符号后打<tab> ,它不会给我所有@tags的列表,而是一整个当前上下文中所有单词的长列表。

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

经过一番搜索之后,我认为我可能需要定义一个新的自定义omni完整功能,特别是该功能的第二部分:

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

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

谢谢

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

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

     ~/.vim/after/ftplugin/ 
  2. ~/.vim/after/ftplugin/markdown.vim ,添加以下行:

     setlocal define=@ 
  3. 在降价缓冲区中,键入@并按<Cx><Cd>

    在此处输入图片说明

参见:help 'define':help ctrl-x_ctrl-d

经过大量的努力和寻求帮助之后,我找到了一个解决方案。

首先创建一个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>"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM