vimautocompletetagssupertab

List @ tag auto completes in vim


I'd like to plant a number of @tags (e.g. @sea_ice, @models) in my markdown files when editing in vim. Currently I'm using SuperTab to tab-complete ordinary words. However if I hit <tab> after the @ symbol, it won't give me a list of all @tags, but rather a long list of all words found in the current context.

I noticed that SuperTab allows custom context definitions, however, as I know nothing about vim scripting and the documentation contains only 2 examples, I'm unable to script it myself.

After bit searching around I think I probably need to define a new custom omni complete function, specifically the 2nd half of the function:

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

This is the code I'm working on. But I don't know how to test it or where is the right place to put it. Please help

Thanks


Solution

  • After quite a bit of struggling and searching for help, I figured out one solution.

    Firstly create a completefunc that searches for @tags in the current file (Credits to 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
    

    The put the following in .vimrc to setup tab-completion using 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>"