免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4846 | 回复: 1
打印 上一主题 下一主题

vim自动补全 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-10 21:02 |只看该作者 |倒序浏览
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文件中按 "." 或者 "::" 就会触发全能补全,看一下改插件中已经实现的一些语言

  1. "   Which completion method is used depends on the text before the cursor. The
  2. "   default behavior is as follows:
  3. "
  4. "     1. The keyword completion is attempted if the text before the cursor
  5. "        consists of two keyword character.
  6. "     2. The filename completion is attempted if the text before the cursor
  7. "        consists of a filename character + a path separator + 0 or more
  8. "        filename characters.
  9. "     3. The omni completion is attempted in Ruby file if the text before the
  10. "        cursor consists of "." or "::". (Ruby interface is required.)
  11. "     4. The omni completion is attempted in Python file if the text before
  12. "        the cursor consists of ".". (Python interface is required.)
  13. "     5. The omni completion is attempted in HTML/XHTML file if the text
  14. "        before the cursor consists of "<" or "</".
  15. "     6. The omni completion is attempted in CSS file if the text before the
  16. "        cursor consists of ":", ";", "{", "@", "!", or in the start of line
  17. "        with blank characters and keyword characters.
复制代码
光有这些我们可能还不能满足,下面我们试着自己来添加一些触发命令

加入PHP的全能提示触发命令

php 中 一般是会在 "$", "->", "::" 后需要出现自动补全,在 .vimrc 中加入以下代码:

  1. if !exists('g:AutoComplPop_Behavior')
  2.     let g:AutoComplPop_Behavior = {}
  3.     let g:AutoComplPop_Behavior['php'] = []
  4.     call add(g:AutoComplPop_Behavior['php'], {
  5.             \   'command'   : "\<C-x>\<C-o>",
  6.             \   'pattern'   : printf('\(->\|::\|\$\)\k\{%d,}$', 0),
  7.             \   'repeat'    : 0,
  8.             \})
  9. endif
复制代码
这样就可以了。

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

在 Vim 中实现括号自动补全

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

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

  1. :inoremap ( ()<ESC>i
  2. :inoremap ) <c-r>=ClosePair(')')<CR>
  3. :inoremap { {}<ESC>i
  4. :inoremap } <c-r>=ClosePair('}')<CR>
  5. :inoremap [ []<ESC>i
  6. :inoremap ] <c-r>=ClosePair(']')<CR>
  7. :inoremap < <><ESC>i
  8. :inoremap > <c-r>=ClosePair('>')<CR>

  9. function ClosePair(char)
  10. if getline('.')[col('.') - 1] == a:char
  11. return "\<Right>"
  12. else
  13. return a:char
  14. endif
  15. endf
复制代码
这样,写代码的时候不再担心会丢掉右边的括号了,尤其是函数嵌套的时候

论坛徽章:
0
2 [报告]
发表于 2011-12-20 12:52 |只看该作者
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP