【概念】
代码自动补全和代码跳转阅读,应该是作为程序员最常用的功能之一了,具体二者是指什么我就不解释了。微软的Visual Studio就是靠这两样必杀技牢牢占据着广大windows程序员的心(这里面要有强大的VS插件Visual Assistant X一份功劳)。。。但是Linux程序员其实更幸福,不花钱就能搞定这两大功能。
从本质上说,这二者的实现都依赖于一样东西:tag。tag就是程序中的关键词,在C++中主要包括:变量、函数名、类名等。代码自动补全实际上是tag的匹配(例如,程序员输入cla时,由于存在class这个c++的tag,就可以用class匹配cla);代码跳转阅读实际上是tag的查找(例如,程序员要查找一个函数func(),只需要在别的文件中寻找这个func这个tag的位置即可)。
【准备】
我现在的系统是Ubuntu Desktop 10.04 LTS版本。当然,一切工作的前提是你能上网,而且配置好了一个可用的源。
1. 安装Vim和Vim基本插件
我们需要首先安装好Vim和Vim的基本插件。这些使用apt-get安装即可:
sudo apt-get install vim vim-doc vim-scripts
其中vim-scripts是vim的一些基本插件,包括语法高亮的支持、缩进等等。
2. Vim配置文件
Vim强大的功能,其来源基本上就两个地方:插件,以及配置文件。
上面已经下载了Vim的基本插件,下面说一下Vim的基本配置。Vim本身的系统配置文件夹是在/usr/share/vim/和/etc/vim/两个文件夹下,我们一般不要去改变这些,改了以后不容易恢复。我们需要在用户文件夹下建立自己的配置文件。假设用户的名字是user。进入用户文件夹(/home/user/)之后,用gedit新建一个名叫.vimrc的文件:
gedit .vimrc
之所以用gedit是因为vim里面不能拷贝粘贴,为了方便大段大段的文字粘贴,还是先用gedit吧。。。
然后把下面的文字拷贝进这个文件之后保存:
" This line should not be removed as it ensures that various options are " properly set to work with the Vim-related packages available in Debian. runtime! debian.vim " Uncomment the next line to make Vim more Vi-compatible " NOTE: debian.vim sets 'nocompatible'. Setting 'compatible' changes numerous " options, so any other options should be set AFTER setting 'compatible'. set nocompatible " Vim5 and later versions support syntax highlighting. Uncommenting the " following enables syntax highlighting by default. if has("syntax") syntax on endif " detect file type filetype on filetype plugin on " If using a dark background within the editing area and syntax highlighting " turn on this option as well set background=dark " Uncomment the following to have Vim jump to the last position when " reopening a file if has("autocmd") au BufReadPost * if line("'/"") > 1 && line("'/"") <= line("$") | exe "normal! g'/"" | endif "have Vim load indentation rules and plugins according to the detected filetype filetype plugin indent on endif " The following are commented out as they cause vim to behave a lot " differently from regular Vi. They are highly recommended though. set showcmd " Show (partial) command in status line. set showmatch " Show matching brackets. "set ignorecase " Do case insensitive matching "set smartcase " Do smart case matching set incsearch " Incremental search set autowrite " Automatically save before commands like :next and :make set autoindent set smartindent set tabstop=4 set shiftwidth=4 "s