0

在 vim 中编辑时,我想在我的 markdown 文件中植入一些@tags(例如@sea_ice, )。@models目前我正在使用 SuperTab 来完成普通单词。但是,如果我<tab>@符号后面点击,它不会给我一个 all 的列表@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

这是我正在处理的代码。但我不知道如何测试它,也不知道在哪里放置它。请帮忙

谢谢

4

2 回答 2

0

我从未使用过 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

于 2016-04-04T22:01:31.963 回答
0

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

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

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>"
于 2016-04-05T15:49:33.377 回答