Vim下的代码自动补全 使用ctags和OmniCppComplete taglist + cscope + global

本文介绍了如何在Vim中设置代码自动补全和代码跳转功能。通过安装ctags、OmniCppComplete插件,以及配置taglist和cscope,程序员可以在Linux环境下实现类似于Visual Studio的代码辅助体验。ctags用于生成tags文件,支持代码跳转,而OmniCppComplete利用tags文件实现自动补全。同时,文章还提到了不同项目间代码跳转的配置方法。
摘要由CSDN通过智能技术生成

 

 

 

【概念】

代码自动补全和代码跳转阅读,应该是作为程序员最常用的功能之一了,具体二者是指什么我就不解释了。微软的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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值