Vim外掛程式neocomplcache 配置心得

來源:互聯網
上載者:User

對於Vimist 來說,neocomplcache 這個外掛程式讓人又愛又恨。主要是功能強大,配置複雜,而且喜歡跟各種外掛程式衝突。

我也是被他折騰了個半死。期間搜尋無數次,卻找不到詳細的配置。在不斷摸索和閱讀協助文檔以及原始碼後,終於有了一點心得。而且把這個外掛程式配置的比較爽了,配合SNIPMATE使用後基本類似VISUAL STUDIO下的VISUAL ASSIST X的感覺。

neocomplcache 其實不用配 SNIPMATE 也基本能用,但是SNIPMATE的snippets比較全,而且網上有加強版本的snippets,snippets的數目要大大超過neocomplcache 。而可悲的是neocomplcache 不能直接調用 SNIPMATE 的 snippets。所以沒法去掉SNIPMATE。

但是SNIPMATE把關鍵的TAB鍵給佔用了(用TAB啟用它的snippets,有方法可以改它的原始碼來實現)。neocomplcache 選擇候選字時就沒法通過TAB來很爽的選擇了,不過經過配置後我還是有幾種方法可以用的比較爽:

1、啟用快速匹配:

let g:neocomplcache_enable_quick_match = 1 “For input-saving, this variable controls whether you can  choose a candidate with a alphabet or number displayed beside a candidate after '-'.  When you input 'ho-a',  neocomplcache will select candidate 'a'.

開啟這個開關後,每次補全菜單彈出時,可以再按一個”-“鍵,這是補全菜單中的每個候選詞會被標上一個字母,只要再輸入對應字母就可以馬上完成選擇。

2、CTRL-N,CTRL-P:

這兩個按鍵組合可以替代TAB的功能,向上或者向下選擇你的候選字。

3、使用SPACE:
inoremap <expr><space>  pumvisible() ? neocomplcache#close_popup() . "\<SPACE>" : "\<SPACE>"

用SPACE自動旋轉當前的候選字,並附加一個空格。這個才是真正無阻礙的輸入方式。在SPACE面前什麼<TAB>,<ENTER>都是浮雲。neocomplcache 的作者居然都沒想到這點 。給出的推薦配置中還在糾結於 <TAB>,<ENTER>。這也是 VISUAL ASSIST X推薦的方式。

 

當然研究這麼久 neocomplcache 肯定不止這一點收穫。還有幾天TIPS:

1、neocomplcache 可以根據檔案類型選用 dict 詞典,這個是EDITPLUS的首創吧,可以直接拷貝EDITPLUS的文法檔案過來了,稍微整理一下就可以指定給neocomplcache去用:

" Define dictionary.
let g:neocomplcache_dictionary_filetype_lists = {
    \ 'default' : '',
    \ 'vimshell' : $HOME.'/.vimshell_hist',
    \ 'scheme' : $HOME.'/.gosh_completions',
    \ 'css' : $VIMFILES.'/dict/css.dic',
    \ 'php' : $VIMFILES.'/dict/php.dic',
    \ 'javascript' : $VIMFILES.'/dict/javascript.dic'
    \ }

2、neocomplcache 預設的斷行符號定義會跟 'auto-pairs' ,'endwise' 之類的通過斷行符號補全的外掛程式相衝突。這個BUG廢掉我一個晚上。不過我也從中找出一個尋找衝突外掛程式的配置方法。就是用 pathogen 的禁用功能: call add(g:pathogen_disabled, 目錄名) 。先將bundle下的所有外掛程式都用這個函數禁用掉。然後再分批在前面加”注釋掉禁用--也就是開啟外掛程式。這樣多測試幾次,只用幾分鐘就能找到衝突的外掛程式了。

call add(g:pathogen_disabled, 'auto-pairs')  " conflict with neocomplcache
call add(g:pathogen_disabled, 'endwise')     " conflict with neocomplcache

關於 pathogen ,這年頭高手管理VIM外掛程式都是用 pathogen 和  git 了。如果你還是將下載的外掛程式混雜在一起,那就該考慮更新一下了。

而GIT對於維護一個時刻保持更新的 網路版的 VIM 配置庫是必不可少的。這方面需要嚴重參考這篇文章:

http://blog.wu-boy.com/2011/09/introduction-to-git-submodule/

也就是說,你基本只用維護你的VIMRC檔案,以及外掛程式列表,而將具體的外掛程式,通過PATHOGEN配置到BUNDLE下的各個目錄,然後通過GIT SUBMODULE指向各外掛程式的原檔案庫,這樣你就擁有了一個永遠最新的VIM 發行版。

有人說EMACS是一個偽裝成編輯器的作業系統,VIM又何嘗不是。。。啟動引導,載入模組,案頭管理,這些概念在這些編輯器中都體現的淋漓盡致。

 

關於鍵衝突的問題,這裡有討論:https://github.com/sjbach/lusty/issues/20

https://github.com/ervandew/supertab/issues/10

3、neocomplcache 設定檔中用到的一些函數說明:

pumvisible() 這個: 如果快顯功能表可見,返回非零,不然返回零。

neocomplcache#undo_completion  這個看名字就知道,做一次undo,取消補全。

neocomplcache#close_popup() 這個是用候選字補全後關閉彈出框

neocomplcache#cancel_popup()  這個是什麼也不做,直接關閉彈出框

neocomplcache#smart_close_popup() 這個直接看函數定義吧。號稱是智能的,但是我設定了半天 g:neocomplcache_enable_auto_select  還是無效,索性直接換成 neocomplcache#close_popup() 了。

  1: function! neocomplcache#smart_close_popup()
  2:   return g:neocomplcache_enable_auto_select ? neocomplcache#cancel_popup() : neocomplcache#close_popup()
  3: endfunction

 

最後是我的設定檔,由於設定檔太大,我是直接存了一個 neocomplcache.conf的檔案,然後在vimrc中去調用它:

" --- NeoComplcache  It's such a big config file that I split into a single file.
source $VIM/vimfiles/neocomplcache.conf

 


  1: " Disable AutoComplPop.
  2: let g:acp_enableAtStartup = 0
  3: " Use neocomplcache.
  4: let g:neocomplcache_enable_at_startup = 1
  5: " Use smartcase.
  6: let g:neocomplcache_enable_smart_case = 1
  7: " Use camel case completion.
  8: let g:neocomplcache_enable_camel_case_completion = 1
  9: " Use underbar completion.
 10: let g:neocomplcache_enable_underbar_completion = 1
 11: " Set minimum syntax keyword length.
 12: let g:neocomplcache_min_syntax_length = 3
 13: let g:neocomplcache_lock_buffer_name_pattern = '\*ku\*'
 14: 
 15: " AutoComplPop like behavior.
 16: let g:neocomplcache_enable_auto_select = 0
 17: 
 18: " When you input 'ho-a',neocomplcache will select candidate 'a'.
 19: let g:neocomplcache_enable_quick_match = 1
 20: 
 21: " Define dictionary.
 22: let g:neocomplcache_dictionary_filetype_lists = {
 23:     \ 'default' : '',
 24:     \ 'vimshell' : $HOME.'/.vimshell_hist',
 25:     \ 'scheme' : $HOME.'/.gosh_completions',
 26:     \ 'css' : $VIMFILES.'/dict/css.dic',
 27:     \ 'php' : $VIMFILES.'/dict/php.dic',
 28:     \ 'javascript' : $VIMFILES.'/dict/javascript.dic'
 29:     \ }
 30: 
 31: let g:neocomplcache_snippets_dir=$VIMFILES."/snippets"
 32: inoremap <expr><S-TAB>  pumvisible() ? "\<C-p>" : "\<TAB>"
 33: inoremap <expr><C-TAB>  pumvisible() ? "\<C-p>" : "\<TAB>"
 34: 
 35: 
 36: " Define keyword.
 37: if !exists('g:neocomplcache_keyword_patterns')
 38:   let g:neocomplcache_keyword_patterns = {}
 39: endif
 40: let g:neocomplcache_keyword_patterns['default'] = '\h\w*'
 41: 
 42: " Plugin key-mappings.
 43: imap <C-k>     <Plug>(neocomplcache_snippets_expand)
 44: smap <C-k>     <Plug>(neocomplcache_snippets_expand)
 45: inoremap <expr><C-g>     neocomplcache#undo_completion()
 46: inoremap <expr><C-z>     neocomplcache#undo_completion()
 47: inoremap <expr><C-l>     neocomplcache#complete_common_string()
 48: 
 49: " SuperTab like snippets behavior.
 50: "imap <expr><TAB> neocomplcache#sources#snippets_complete#expandable() ? "\<Plug>(neocomplcache_snippets_expand)" : pumvisible() ? "\<C-n>" : "\<TAB>"
 51: 
 52: " Recommended key-mappings.
 53: " <CR>: close popup and save indent.
 54: " inoremap <expr><CR>  neocomplcache#close_popup() . "\<CR>"
 55: inoremap <expr><CR> pumvisible() ? neocomplcache#close_popup() : "\<CR>"
 56: " <TAB>: completion. THIS HAS NO USE WHEN WITH SNIPMATE
 57: " inoremap <expr><TAB>  pumvisible() ? "\<C-n>" : "\<TAB>"
 58: " <SPACE>: completion.
 59: inoremap <expr><space>  pumvisible() ? neocomplcache#close_popup() . "\<SPACE>" : "\<SPACE>"
 60: 
 61: 
 62: " <C-h>, <BS>: close popup and delete backword char.
 63: inoremap <expr><C-h> neocomplcache#close_popup()."\<C-h>"
 64: inoremap <expr><BS> neocomplcache#close_popup()."\<C-h>"
 65: 
 66: inoremap <expr><C-y>  neocomplcache#close_popup()
 67: inoremap <expr><C-e>  neocomplcache#cancel_popup()
 68: 
 69: " Shell like behavior(not recommended).
 70: "set completeopt+=longest
 71: "let g:neocomplcache_enable_auto_select = 1
 72: "let g:neocomplcache_disable_auto_complete = 1
 73: "inoremap <expr><TAB>  pumvisible() ? "\<Down>" : "\<TAB>"
 74: "inoremap <expr><CR>  neocomplcache#close_popup() . "\<CR>"
 75: 
 76: " Enable omni completion.
 77: autocmd FileType css setlocal omnifunc=csscomplete#CompleteCSS
 78: autocmd FileType html,markdown setlocal omnifunc=htmlcomplete#CompleteTags
 79: autocmd FileType javascript setlocal omnifunc=javascriptcomplete#CompleteJS
 80: autocmd FileType python setlocal omnifunc=pythoncomplete#Complete
 81: autocmd FileType xml setlocal omnifunc=xmlcomplete#CompleteTags
 82: autocmd FileType ruby setlocal omnifunc=rubycomplete#Complete
 83: 
 84: " Enable heavy omni completion.
 85: if !exists('g:neocomplcache_omni_patterns')
 86:   let g:neocomplcache_omni_patterns = {}
 87: endif
 88: let g:neocomplcache_omni_patterns.ruby = '[^. *\t]\.\w*\|\h\w*::'
 89: let g:neocomplcache_omni_patterns.php = '[^. \t]->\h\w*\|\h\w*::'
 90: let g:neocomplcache_omni_patterns.c = '\%(\.\|->\)\h\w*'
 91: let g:neocomplcache_omni_patterns.cpp = '\h\w*\%(\.\|->\)\h\w*\|\h\w*::'
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.