用Vim编程——配置与技巧
出自Linux Wiki
本文介绍如何用强大的文本编辑器 Vim编程。
目录 |
配置
基本配置[1]:
"开启语法高亮 syntax on "依文件类型设置自动缩进 filetype indent plugin on "显示当前的行号列号: set ruler "在状态栏显示正在输入的命令 set showcmd "关闭/打开配对括号高亮 "NoMatchParen DoMatchParen
行号的显示与隐藏[2]:
"显示行号: set number "为方便复制,用<F2>开启/关闭行号显示: nnoremap <F2> :set nonumber!<CR>:set foldcolumn=0<CR>
启用Modeline(即允许被编辑的文件以注释的形式设置Vim选项,详见Vim Wiki: Modeline magic)[3]:
set modeline
如果终端使用的是深色背景:
"为深色背景调整配色 set background=dark
代码补全
Vim 7已经内置了代码补全功能[4],补全操作可分为两种:
- 关键字补全
- 即简单地补全到文档中已有的词,快捷键为 Ctrl-N 或 Ctrl-P。
- 智能补全
- Vim 7中引入 Omni Complete,可根据语义补全,快捷键为 Ctrl-X Ctrl-O。
Vim的补全菜单操作方式与一般IDE的方式不同,可加入如下设置[5]:
"让Vim的补全菜单行为与一般IDE一致(参考VimTip1228) set completeopt+=longest inoremap <expr> <CR> pumvisible() ? "\<C-y>" : "\<CR>" inoremap <expr> <Down> pumvisible() ? "\<C-n>" : "\<Down>" inoremap <expr> <Up> pumvisible() ? "\<C-p>" : "\<Up>" inoremap <expr> <PageDown> pumvisible() ? "\<PageDown>\<C-p>\<C-n>" : "\<PageDown>" inoremap <expr> <PageUp> pumvisible() ? "\<PageUp>\<C-p>\<C-n>" : "\<PageUp>"
如需自动补全,可安装AutoComplPop插件,安装后如需在补全是显示文档(预览),可加入设置:
let g:acp_completeoptPreview = 1
注释管理
手动地逐行加注释一般较繁,可用插件来快速注释(或取消注释)代码。
常见的注释管理插件为The NERD Commenter,默认支持多种语言,使用方法也很简单:只需在可视(V)模式中选择一段代码,按下\cc加逐行注释,\cu取消注释,\cm添加块注释。
如有其它需求,也可考虑comments、EnhCommentify等插件。
缩进
使用自动缩进可能需要设置,vim中对自动缩进的详细设置办法见Vim代码缩进设置。
在不同的模式中调整缩进的方法不同:
- 插入模式
- Ctrl-T增加缩进,Ctrl-D减小缩进。
- 命令模式
- >> 右缩进, << 左缩进,注意n<< 或 n>>是缩进多行,如4>>
- 可视模式
- < 、 > 用于左右缩进, n< 、 n> 可做多节缩进,如 2> 。
另外,= 可对选中的部分进行自动缩进;]p可以实现p的粘贴功能,并自动缩进。
代码浏览与跳转
代码跳转类似于IDE中的Ctrl+点击功能。与代码补全类似,代码浏览工具亦可分为基于文本分析的和基于代码理解的两类。
简单代码跳转
在命令模式下可直接使用下述指令:[6]
- 跳转到定义
- gd到局部变量的定义,gD到全局变量的定义
- 搜索
- *, # 可对光标处的词向前/向后做全词搜索,g*, g# 做相对应的非全词匹配搜索
- 代码块首尾
- [[, ]] 可跳到当前代码块起始或结束的大括号处。
- 括号首尾
- % 可在配对的括号、块首尾之前跳转。
- 位置历史
- Ctrl-O 在历史记录中后台,Ctrl-I 则为前进。
基于代码理解的跳转
该功能依赖于ctags工具。[7]安装好ctags后,在存放代码的文件夹运行
ctags -R .
即可生成一个描述代码结构的tags文件。
建议在~/.vimrc中添加如下配置以使Vim在父目录中寻找tags文件[8]:
set tags=tags;/
设置好后,可在Vim中使用如下功能:Ctrl-]转至最佳匹配的相应Tag,Ctrl-T返回上一个匹配。如果有多个匹配,g Ctrl-]可显示所有备选的tags。如有需要,可互换Ctrl-]和g Ctrl-] [9]:
"在普通和可视模式中,将<c-]>与g<c-]>互换 nnoremap <c-]> g<c-]> vnoremap <c-]> g<c-]> nnoremap g<c-]> <c-]> vnoremap g<c-]> <c-]>
代码折叠
代码较长时可启用代码折叠功能,如按语法高亮元素折叠:
set foldmethod=syntax "默认情况下不折叠 set foldlevel=99
随后即可使用z系列命令管理代码折叠。如za会翻转当前位置的折叠状态,zA会递归翻转当前层所有代码的折叠状态等。当然也可以把功能键映射到za:
map <F3> za
代码框架
要查阅代码的大体结构,如类、函数、变量等,可使用taglist插件,其可配置性很强,可将下面的代码按需加入~/.vimrc中(用<F4>开启/关闭Tag list):
" F4: Switch on/off TagList nnoremap <silent> <F4> :TlistToggle<CR> "let Tlist_Show_One_File = 1 " Displaying tags for only one file~ let Tlist_Exist_OnlyWindow = 1 " if you are the last, kill yourself "let Tlist_Use_Right_Window = 1 " split to the right side of the screen let Tlist_Sort_Type = "order" " sort by order or name let Tlist_Display_Prototype = 0 " do not show prototypes and not tags in the taglist window. let Tlist_Compart_Format = 1 " Remove extra information and blank lines from the taglist window. let Tlist_GainFocus_On_ToggleOpen = 1 " Jump to taglist window on open. let Tlist_Display_Tag_Scope = 1 " Show tag scope next to the tag name. "let Tlist_Close_On_Select = 1 " Close the taglist window when a file or tag is selected. let Tlist_Enable_Fold_Column = 0 " Don't Show the fold indicator column in the taglist window. let Tlist_WinWidth = 40 " let Tlist_Ctags_Cmd = 'ctags --c++-kinds=+p --fields=+iaS --extra=+q --languages=c++' " very slow, so I disable this " let Tlist_Process_File_Always = 1 " To use the :TlistShowTag and the :TlistShowPrototype commands without the taglist window and the taglist menu, you should set this variable to 1. ":TlistShowPrototype [filename] [linenumber]
查看文档
K 键可用于查看当前函数的文档。
特定编程语言的配置
参考资料
- ↑ How to setup VIM properly for editing Python files - *.py (StackOverflow)
- ↑ Python and vim: Make your own IDE (2009.2)
- ↑ Python官网上的Vim介绍
- ↑ 细说 Vim 7 之新特性-LinuxToy
- ↑ VimTip 1228: Improve completion popup menu
- ↑ Vim文档
- ↑ Browsing programs with tags -- Vim Tips Wiki
- ↑ Vim and Ctags tips and tricks
- ↑ use :tjump instead of :tag vim on pressing ctrl-] (StackOverflow)