I'd like to plant a number of @tags
(eg @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
I've never used SuperTab so I have no idea if and how this solution could be made to work with that plugin, but it's pretty easy with the built-in manual completion.
If it doesn't exist already, create this directory structure:
~/.vim/after/ftplugin/
In ~/.vim/after/ftplugin/markdown.vim
, add this line:
setlocal define=@
In a markdown buffer, type @
and press <Cx><Cd>
.
See :help 'define'
and :help ctrl-x_ctrl-d
.
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>"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.