Chinaunix首页 | 论坛 | 博客
  • 博客访问: 438024
  • 博文数量: 85
  • 博客积分: 3580
  • 博客等级: 中校
  • 技术积分: 970
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-09 14:09
文章分类

全部博文(85)

文章存档

2011年(7)

2010年(78)

我的朋友

分类:

2010-09-28 19:34:33

http://hi.baidu.com/515314237/blog/item/08a4f0f8d840a853242df245.html

autocomplpop.vim : Automatically open the popup menu for completion

介绍

用惯一些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’ : “\\“,
\ ‘pattern’ : printf(‘\(->\|::\|\$\)\k\{%d,}$’, 0),
\ ‘repeat’ : 0,
\})
endif

这样就可以了。

注意,某些时候,可能会在第一次按下触发补全的操作符时停顿一会,这可能是因为可匹配的项目过多,Vim正在索引,过后就会快了。

在 Vim 中实现括号自动补全

流行的 IDE 的编辑器,诸如 Eclipse,都提供了括号自动补全的功能,相当的方便。可惜 Vim 默认情况下并没有提供这样的功能,那就只有自己来写了。

将下面的代码加入到 ~/.vimrc 中,重启 Vim,即可:

:inoremap ( ()i
:inoremap ) =ClosePair(‘)’)
:inoremap { {}i
:inoremap } =ClosePair(‘}’)
:inoremap [ []i
:inoremap ] =ClosePair(‘]’)
:inoremap < <>i
:inoremap > =ClosePair(‘>’)

function ClosePair(char)
if getline(‘.’)[col('.') - 1] == a:char
return “\
else
return a:char
endif
endf
这样,写代码的时候不再担心会丢掉右边的括号了,尤其是函数嵌套的时候

No related posts.

阅读(838) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~