Vim插件neocomplcache 配置心得


對於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*::'


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM