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

 

这样,写代码的时候不再担心会丢掉右边的括号了,尤其是函数嵌套的时候

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值