手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

将Vim改造为强大的IDE—Vim集成Ctags/Taglist/Cscope/Winmanager/NERDTree/OmniCppComplete

[日期:2017-02-06] 来源:Linux社区  作者:Linux [字体: ]

1、安装Vim和Vim基本插件
首先安装好Vim和Vim的基本插件。这些使用apt-get安装即可:
lingd@Ubuntu:~/arm$sudo apt-get install vim vim-scripts vim-doc
其中vim-scripts是vim的一些基本插件,包括语法高亮的支持、缩进等等。
vim中文帮助文档tar包下载地址:
http://sourceforge.net/projects/vimcdoc/files/vimcdoc/
解压后其中有个doc文件夹, 将其中的内容全部复制到~/.vim/doc, 或者vim安装目录下的doc目录中, 此时vim中的help信息已经是中文的了.
网页版中文帮助文档网址http://vimcdoc.sourceforge.net/doc/help.html
首页就时vim帮助文档的目录,阅读起来更方便有效、更有针对性!
 
2、Vim配置文件
Vim强大的功能,其来源基本上就两个地方:Vim插件以及Vim配置文件。
Vim本身的系统配置文件夹是在/usr/share/vim/和/etc/vim/两个文件夹下。一般情况下,我们不会去改变这两个文件夹下的配置文件,而是在用户文件夹/home/user(其中,user为用户名,我的用户名是lingd)下建立自己的配置文件。进入用户文件夹(/home/user/)之后,用gedit新建一个名叫.vimrc的文件:
lingd@ubuntu:~/arm$ cd ~
lingd@ubuntu:~$ gedit .vimrc
注:使用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.
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
colorscheme ron " elflord ron peachpuff default 设置配色方案,vim自带的配色方案保存在/usr/share/vim/vim72/colors目录下

" 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 ignorecase " 搜索模式里忽略大小写
"set smartcase " 如果搜索模式包含大写字符,不使用 'ignorecase' 选项。只有在输入搜索模式并且打开 'ignorecase' 选项时才会使用。
set autowrite " 自动把内容写回文件: 如果文件被修改过,在每个 :next、:rewind、:last、:first、:previous、:stop、:suspend、:tag、:!、:make、CTRL-] 和 CTRL-^命令时进行;用 :buffer、CTRL-O、CTRL-I、'{A-Z0-9} 或 `{A-Z0-9} 命令转到别的文件时亦然。
set autoindent " 设置自动对齐(缩进):即每行的缩进值与上一行相等;使用 noautoindent 取消设置
"set smartindent " 智能对齐方式
set tabstop=4 " 设置制表符(tab键)的宽度
set softtabstop=4 " 设置软制表符的宽度
set shiftwidth=4 " (自动) 缩进使用的4个空格
set cindent " 使用 C/C++ 语言的自动缩进方式
set cinoptions={0,1s,t0,n-2,p2s,(03s,=.5s,>1s,=1s,:1s "设置C/C++语言的具体缩进方式
"set backspace=2 " 设置退格键可用
set showmatch " 设置匹配模式,显示匹配的括号
set linebreak " 整词换行
set whichwrap=b,s,<,>,[,] " 光标从行首和行末时可以跳到另一行去
"set hidden " Hide buffers when they are abandoned
set mouse=a " Enable mouse usage (all modes) "使用鼠标
set number " Enable line number "显示行号
"set previewwindow " 标识预览窗口
set history=50 " set command history to 50 "历史记录50条


"--状态行设置--
set laststatus=2 " 总显示最后一个窗口的状态行;设为1则窗口数多于一个的时候显示最后一个窗口的状态行;0不显示最后一个窗口的状态行
set ruler " 标尺,用于显示光标位置的行号和列号,逗号分隔。每个窗口都有自己的标尺。如果窗口有状态行,标尺在那里显示。否则,它显示在屏幕的最后一行上。

"--命令行设置--
set showcmd " 命令行显示输入的命令
set showmode " 命令行显示vim当前模式

"--find setting--
set incsearch " 输入字符串就显示匹配点
set hlsearch

注:配置文件中,以单个双引号开头的文字为注释。
保存文件之后,启动Vim。此时,Vim已经是这种效果了(语法高亮挺漂亮的–这个是由vim-scripts中的插件支持的):

3、ctags安装与配置
ctags可以建立源码树的标签索引(标签就是一个标识符被定义的地方,如函数定义),使程序员在编程时能迅速定位函数、变量、宏定义等位置去查看原形
以下是在ubuntu下ctags的下载安装和配置过程:
下载并安装ctags,终端输入命令
lingd@ubuntu:~/arm$ sudo apt-get install ctags
建立源码索引,比如我经常需要查阅Linux的内核代码,而这些代码放在/home/lingd/arm/linux-2.6.24.7目录下
那么在终端进入到该目录后,输入命令ctags -R *,你会发现多了一个tags文件,这个就是索引文件
lingd@ubuntu:~/arm$ cd linux-2.6.24.7
lingd@ubuntu:~/arm/linux-2.6.24.7$ ls
arch    crypto        include  kernel      mm              samples  usr
block    Documentation  init    lib          net            scripts
COPYING  drivers        ipc      MAINTAINERS  README          security
CREDITS  fs            Kbuild  Makefile    REPORTING-BUGS  sound
lingd@ubuntu:~/arm/linux-2.6.24.7$ ctags -R *
lingd@ubuntu:~/arm/linux-2.6.24.7$ ls
arch    crypto        include  kernel      mm              samples  tags
block    Documentation  init    lib          net            scripts  usr
COPYING  drivers        ipc      MAINTAINERS  README          security
CREDITS  fs            Kbuild  Makefile    REPORTING-BUGS  sound
向vim注册索引文件tags的路径,
lingd@ubuntu:~/arm/linux-2.6.24.7$ vi ~/.vimrc
在打开文件的最后添加如下内容(当然,具体路径根据你自己的情况)

"--ctags setting--
" 按下F5重新生成tag文件,并更新taglist
map <F5> :!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<CR><CR> :TlistUpdate<CR>
imap <F5> <ESC>:!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<CR><CR> :TlistUpdate<CR>
set tags=tags
set tags+=./tags "add current directory's generated tags file
set tags+=~/arm/linux-2.6.24.7/tags "add new tags file(刚刚生成tags的路径,在ctags -R 生成tags文件后,不要将tags移动到别的目录,否则ctrl+]时,会提示找不到源码文件)

set tags+=./tags表示在当前工作目录下搜索tags文件
set tags+=~/arm/linux-2.6.24.7/tags表示在搜寻tags文件的时候,也要搜寻~/arm/linux-2.6.24.7/文件夹下的tags文件。
然后保存并退出vi。这样,你就可以用vim在任意地方查看有关Linux的函数原形
------------------------------------
tag命令用法:
Ctrl+]  跳到当前光标下单词的标签
Ctrl+O  返回上一个标签
Ctrl+T  返回上一个标签
:tag TagName 跳到TagName标签
以上命令是在当前窗口显示标签,当前窗口的文件替代为包标签的文件,当前窗口光标跳到标签位置。如果不希望在当前窗口显示标签,可以使用以下命令:
:stag TagName 新窗口显示TagName标签,光标跳到标签处
Ctrl+W + ]  新窗口显示当前光标下单词的标签,光标跳到标签处
当一个标签有多个匹配项时(函数 (或类中的方法) 被多次定义),":tags" 命令会跳转到第一处。如果在当前文件中存在匹配,那它将会被首先使用。
可以用这些命令在各匹配的标签间移动:
:tfirst    到第一个匹配
:[count]tprevious 向前 [count] 个匹配
:[count]tnext  向后 [count] 个匹配
:tlast    到最后一个匹配
或者使用以下命令选择要跳转到哪一个
:tselect TagName
输入以上命令后,vim会为你展示一个选择列表。然后你可以输入要跳转到的匹配代号 (在第一列)。其它列的信息可以让你知道标签在何处被定义过。
以下命令将在预览窗口显示标签
:ptag TagName 预览窗口显示TagName标签,光标跳到标签处
Ctrl+W + }  预览窗口显示当前光标下单词的标签,光标跳到标签处
:pclose  关闭预览窗口
:pedit file.h 在预览窗口中编辑文件file.h(在编辑头文件时很有用)
:psearch atoi 查找当前文件和任何包含文件中的单词并在预览窗口中显示匹配,在使用没有标签文件的库函数时十分有用。
 
最简单的使用方法举例
用vi在任意目录写一个Test.c文件,内容如下:

int main(void)
{
printf("Hello World!\n");
return 0;
}

写好后末行模式输入w保存好(不要退出vi),按Esc回到指令模式,把光标停留在printf上
然后按 Ctrl+W + ],vi会自动跳到Linux系统函数printf()处,这时我们能查看printf()的原形
查看完了,按Ctrl+o(回到上一个标签) 就回到原来的地方

将Vim改造为强大的IDE—Vim集成Ctags/Taglist/Cscope/Winmanager

4、管理vim插件——vim-addons
通过vim-addons,我们可以管理vim插件。我们在sudo apt-get install vim vim-scripts vim-doc时,一般会自动安装上vim-addons。��未安装可通过sudo apt-get install vim-addon-manager手动安装。安装完成后,就可以用vim-addons管理vim插件了。
# 系统中已有的vim-scripts中包含的插件及其状态:
lingd@ubuntu:~$ vim-addons status
# Name                    User Status  System Status
align                      removed      removed   
alternate                  removed      removed   
bufexplorer                removed      removed   
calendar                    removed      removed   
closetag                    removed      removed   
colors sampler pack        removed      removed   
cvsmenu                    removed      removed   
debPlugin                  removed      removed   
detectindent                removed      removed   
doxygen-toolkit            removed      removed   
editexisting                removed      removed   
enhanced-commentify        removed      removed   
gnupg                      removed      removed   
info                        removed      removed   
justify                    removed      removed   
lbdbq                      removed      removed   
markdown-syntax            removed      removed   
matchit                    removed      removed   
minibufexplorer            installed    removed   
nerd-commenter              removed      removed   
omnicppcomplete            installed    removed   
po                          removed      removed   
project                    installed    removed   
Python-indent              removed      removed   
secure-modelines            removed      removed   
snippetsEmu                removed      removed   
sokoban                    removed      removed   
supertab                    removed      removed   
surround                    removed      removed   
taglist                    installed    removed   
tetris                      removed      removed   
utl                        removed      removed   
vcscommand                  removed      removed   
vimplate                    removed      removed   
whatdomain                  removed      removed   
winmanager                  removed      removed   
xmledit                    removed      removed 
上面我们介绍了如何独立于系统配置文件之外,建立自己的Vim配置文件。当我们自己下载安装Vim插件的时候,也可以另外建立目录,放置我们自己的插件。这个目录一般为/home/user/.vim,另外还需要建立一个插件子目录,一个插件文档子目录,以上的可以进入/home/user目录下通过下面的命令执行:
lingd@ubuntu:~$ mkdir .vim
lingd@ubuntu:~$ cd .vim
lingd@ubuntu:~/.vim$ mkdir plugin
lingd@ubuntu:~/.vim$ mkdir doc
# vim官方插件的安装,xxxx是要安装的插件名,以status中显示的名称为准。安装插件xxxx时使用以下命令(前提是在目录/home/user/.vim/下建立好了plugin和doc两个文件夹)
vim-addons install xxxx
 关于vim-addons命令的详细用法,可以通过“man vim-addons”查看其帮助文档

5、vim自动补全——OmniCppComplete
vim的自动补全功能可通过其插件OmniCppComplete实现。
安装OmniCppComplete
lingd@ubuntu:~$ vim-addons install omnicppcomplete
配置OmniCppComplete
在vim配置文件/home/user/.vimrc中加入如下的配置:

"-- omnicppcomplete setting --
" 按下F3自动补全代码,注意该映射语句后不能有其他字符,包括tab;否则按下F3会自动补全一些乱码
imap <F3> <C-X><C-O>
" 按下F2根据头文件内关键字补全
imap <F2> <C-X><C-I>
set completeopt=menu,menuone " 关掉智能补全时的预览窗口
let OmniCpp_MayCompleteDot = 1 " autocomplete with .
let OmniCpp_MayCompleteArrow = 1 " autocomplete with ->
let OmniCpp_MayCompleteScope = 1 " autocomplete with ::
let OmniCpp_SelectFirstItem = 2 " select first item (but don't insert)
let OmniCpp_NamespaceSearch = 2 " search namespaces in this and included files
let OmniCpp_ShowPrototypeInAbbr = 1 " show function prototype in popup window
let OmniCpp_GlobalScopeSearch=1 " enable the global scope search
let OmniCpp_DisplayMode=1 " Class scope completion mode: always show all members
"let OmniCpp_DefaultNamespaces=["std"]
let OmniCpp_ShowScopeInAbbr=1 " show scope in abbreviation and remove the last column
let OmniCpp_ShowAccess=1

(前几行就是提供了C++中的./->/::等操作符的提示和自动完成)。
OmniCppComplete是基于ctags数据库即tags文件实现的(基于ctags生成的索引信息来实现自动补全的),所以在ctags -R生成tags时还需要一些额外的选项,这样生成的tags文件才能与OmniCppComplete配合运作。使用下列命令生成tags文件,就可以与OmniCppComplete配合运作:
ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .
--c++-kinds=+p  : 为C++文件增加函数原型的标签
--fields=+iaS  : 在标签文件中加入继承信息(i)、类成员的访问控制信息(a)、以及函数的指纹(S)
--extra=+q      : 为标签增加类修饰符。注意,如果没有此选项,将不能对类成员补全
# vim自动补全功能的测试
# 为了测试自动补全功能,我们先下载C++一份C++标准库的源代码。
lingd@ubuntu:~$ sudo apt-get install build-essential
# 然后在/usr/include/c++下就可以找到标准库的头文件了。
lingd@ubuntu:~$ cd /usr/include/c++
lingd@ubuntu:/usr/include/c++$ ls
4.4  4.4.3
# 在此文件夹下生成能与OmniCppComplete配合运作的tags文件
lingd@ubuntu:/usr/include/c++$ ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .
lingd@ubuntu:/usr/include/c++$ ls
4.4  4.4.3  tags
# 在vim的配置文件中如下内容,然后在编程的时候就可以使用自动补全功能了。
lingd@ubuntu:/usr/include/c++$ vi ~/.vimrc

set tags+=/usr/include/c++/tags

# 用vi打开前面的Test.c文件,在printf("Hello World!\n")下一行中,输入pri,然后按下Ctrl+X Ctrl+O,此时vi会弹出一个窗口,所有以pri开头的tag都会出现在这个窗口中,printf就出现在第6行中
lingd@ubuntu:~$ cd ~
lingd@ubuntu:~$ vim Test.c

将Vim改造为强大的IDE—Vim集成Ctags/Taglist/Cscope/Winmanager

注意:在自动补全的点,Vim必须知道可能补全的定义。比如说,在namespace std命名空间下的变量和函数,必须要用using namespace std;暴露出来,否则是不能补全的。在.cpp文件中还可以,在.h文件中这样就不是好的做法了。暂时不知道这个问题是由于我自己配置错误还是程序没有实现。
当自动补全下拉窗口弹出后,一些可用的快捷键:
Ctrl+P  向前切换成员
Ctrl+N  向后切换成员
Ctrl+E  表示退出下拉窗口, 并退回到原来录入的文字
Ctrl+Y  表示退出下拉窗口, 并接受当前选项
其他补全方式:
Ctrl+X Ctrl+L 整行补全
Ctrl+X Ctrl+N  根据当前文件里关键字补全
Ctrl+X Ctrl+K  根据字典补全
Ctrl+X Ctrl+T  根据同义词字典补全
Ctrl+X Ctrl+I  根据头文件内关键字补全
Ctrl+X Ctrl+]  根据标签补全
Ctrl+X Ctrl+F  补全文件名
Ctrl+X Ctrl+D  补全宏定义
Ctrl+X Ctrl+V  补全vim命令
Ctrl+X Ctrl+U  用户自定义补全方式
Ctrl+X Ctrl+S  拼写建议
帮助文档
:help omnicppcomplete
 
6、提示函数原型echofunc
echofunc可以在命令行中提示当前输入函数的原型。
echofunc下载地址:http://www.vim.org/scripts/script.php?script_id=1735
下载完成后,把echofunc.vim文件放到 ~/.vim/plugin文件夹中
当你在vim插入(insert)模式下紧接着函数名后输入一个"("的时候, 这个函数的声明就会自动显示在命令行中。如果这个函数有多个声明, 则可以通过按键"Alt+-"和"Alt+="向前和向后翻页, 这个两个键可以通过设置g:EchoFuncKeyNext和g:EchoFuncKeyPrev参数来修改。这个插件需要tags文件的支持, 并且在创建tags文件的时候要加选项"--fields=+lS"(OmniCppComplete创建的tag文件也能用), 整个创建tags文件的命令如下:
$ ctags -R --fields=+lS
其他插件说明详见echofunc.vim

将Vim改造为强大的IDE—Vim集成Ctags/Taglist/Cscope/Winmanager

如果你在编译vim时加上了"+balloon_eval"特性,那么当你把鼠标放在函数名上的时候会有一个tip窗口弹出, 该窗口中也会有函数的声明

更多详情见请继续阅读下一页的精彩内容http://www.linuxidc.com/Linux/2017-02/140282p2.htm 

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款