2016-04-04 124 views
0

我想在vim中編輯我的markdown文件時種下若干@tags(例如@sea_ice,@models)。目前我正在使用SuperTab來製表普通單詞。但是,如果我在@符號後點擊<tab>,它將不會給我一個所有@tags的列表,而是列出當前上下文中所有單詞的一長串列表。List @標籤自動在vim中完成

我注意到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

這是我工作的代碼。但我不知道如何測試它,或者在哪裏放置它。請幫助

感謝

回答

0

經過相當多的努力和尋求幫助,我找到了一個解決方案。

首先創建一個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\)\[email protected]') 
    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('.') . '[email protected][[:alnum:]_]*')] 
     endwhile 

     return filter(matches, "v:val != '@'") 
    endif 
endfunction 
set completefunc=CompleteTags 

的投入使用SUPERTAB在.vimrc以下設置選項卡完成:

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>" 
0

我從來沒有用過SUPERTAB所以我不知道是否和如何該解決方案可以作出與插件的工作,但它與內置手動完成很容易。

  1. 如果它不存在,創建該目錄結構:

    ~/.vim/after/ftplugin/ 
    
  2. ~/.vim/after/ftplugin/markdown.vim,加入這一行:

    setlocal [email protected] 
    
  3. 在降價緩衝,類型@並按<C-x><C-d>

    enter image description here

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

+0

感謝您的回答。它有點作品,但不完全。當我在一個測試文件中有6個這樣的'@ tags'並且使用你的方法時,完成只給了我2個選擇(前2個),它在2中循環。是否有長度限制參數或者什麼? – Jason

+0

我發現'define'用於標記**行**的定義。在我的情況下,我在一行中有多個'@ tags',所以後面的所有標籤都不被識別。有沒有辦法解決這個問題? – Jason