- 论坛徽章:
- 0
|
vim自动补全
autocomplpop.vim : Automatically open the popup menu for completion
http://www.vim.org/scripts/script.php?script_id=1879
介绍
用惯一些IDE的朋友,一开始可能不习惯Vim的自动补全,主要是每次都要按下一个组合键才会出现提示,而不是像IDE里面那样只要输入了某个操作符就会触发自动补全。
autocomplpop.vim 这个插件就可以很好的解决这个问题。
基本使用
首先访问链接[1],下载 autocomplpop.vim 后,放到Vim文件目录下的plugin目录中,然后重启一下 vim 就会发现在编码时会自动的弹出提示了。
打开 autocomplpop.vim ,可以再 DOCUMENT 部分看到使用方式与一些设置。
增加智能提示触发命令
该插件的默认设置可以完成一些基本的提示,但是每种语言都不同,需要触发 全能 (omni) 补全 的操作符也不同,所幸 autocomplpop 可以让我们自己定制触发的命令模式,这样就可以实现无限扩展以达到自己的需求。
autocomplpop已经实现了部分语言的自动全能补全,比如 ruby文件中按 "." 或者 "::" 就会触发全能补全,看一下改插件中已经实现的一些语言
- " Which completion method is used depends on the text before the cursor. The
- " default behavior is as follows:
- "
- " 1. The keyword completion is attempted if the text before the cursor
- " consists of two keyword character.
- " 2. The filename completion is attempted if the text before the cursor
- " consists of a filename character + a path separator + 0 or more
- " filename characters.
- " 3. The omni completion is attempted in Ruby file if the text before the
- " cursor consists of "." or "::". (Ruby interface is required.)
- " 4. The omni completion is attempted in Python file if the text before
- " the cursor consists of ".". (Python interface is required.)
- " 5. The omni completion is attempted in HTML/XHTML file if the text
- " before the cursor consists of "<" or "</".
- " 6. The omni completion is attempted in CSS file if the text before the
- " cursor consists of ":", ";", "{", "@", "!", or in the start of line
- " with blank characters and keyword characters.
复制代码 光有这些我们可能还不能满足,下面我们试着自己来添加一些触发命令
加入PHP的全能提示触发命令
php 中 一般是会在 "$", "->", "::" 后需要出现自动补全,在 .vimrc 中加入以下代码:
- if !exists('g:AutoComplPop_Behavior')
- let g:AutoComplPop_Behavior = {}
- let g:AutoComplPop_Behavior['php'] = []
- call add(g:AutoComplPop_Behavior['php'], {
- \ 'command' : "\<C-x>\<C-o>",
- \ 'pattern' : printf('\(->\|::\|\$\)\k\{%d,}$', 0),
- \ 'repeat' : 0,
- \})
- endif
复制代码 这样就可以了。
注意,某些时候,可能会在第一次按下触发补全的操作符时停顿一会,这可能是因为可匹配的项目过多,Vim正在索引,过后就会快了。
在 Vim 中实现括号自动补全
流行的 IDE 的编辑器,诸如 Eclipse,都提供了括号自动补全的功能,相当的方便。可惜 Vim 默认情况下并没有提供这样的功能,那就只有自己来写了。
将下面的代码加入到 ~/.vimrc 中,重启 Vim,即可:
- :inoremap ( ()<ESC>i
- :inoremap ) <c-r>=ClosePair(')')<CR>
- :inoremap { {}<ESC>i
- :inoremap } <c-r>=ClosePair('}')<CR>
- :inoremap [ []<ESC>i
- :inoremap ] <c-r>=ClosePair(']')<CR>
- :inoremap < <><ESC>i
- :inoremap > <c-r>=ClosePair('>')<CR>
- function ClosePair(char)
- if getline('.')[col('.') - 1] == a:char
- return "\<Right>"
- else
- return a:char
- endif
- endf
复制代码 这样,写代码的时候不再担心会丢掉右边的括号了,尤其是函数嵌套的时候 |
|