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*::'
posted @ 2012-03-24 00:15  喻头快跑  阅读(15512)  评论(3编辑  收藏  举报