您的位置:首页 > 其它

Vim学习经验小结(kelly)

2007-07-23 11:12 363 查看
最近又拾linux,发现相比于Windows,在Linux下写并行程序还是比较干净的,用起来没有那么多的优虑,但开发环境的话还是需要通过学习怎么去配置才能达到高效的效果,现在开始使用Vim来编写程序,所以把一些使用Vim的经验也慢慢地写进来。
以下是昨天搞的Vim配置文件,现在看起来效果比刚开始用的舒服多了。
-------------------------------------------------
" All system-wide defaults are set in $VIMRUNTIME/debian.vim (usually just
" /usr/share/vim/vimcurrent/debian.vim) and sourced by the call to :runtime
" you can find below. If you wish to change any of those settings, you should
" do it in this file (/etc/vim/vimrc), since debian.vim will be overwritten
" everytime an upgrade of the vim packages is performed. It is recommended to
" make changes after sourcing debian.vim since it alters the value of the
" 'compatible' option.

" 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 compatible

" Vim5 and later versions support syntax highlighting. Uncommenting the next
" line enables syntax highlighting by default.
"syntax 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("'/"") > 0 && line("'/"") <= line("$")
" /| exe "normal g'/"" | endif
"endif

" Uncomment the following to have Vim load indentation rules according to the
" detected filetype. Per default Debian Vim only load filetype specific
" plugins.
"if has("autocmd")
" filetype 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 hidden " Hide buffers when they are abandoned
set mouse=a " Enable mouse usage (all modes) in terminals
syn on
autocmd BufEnter * call DoWordComplete()
set nu
set expandtab
set shiftwidth=4
set tabstop=4
set cindent
set autoindent
highlight Comment ctermfg=darkcyan
highlight Search term=reverse ctermbg=4 ctermfg=7
highlight Normal ctermbg=black ctermfg=white
" Source a global configuration file if available
" XXX Deprecated, please move your changes here in /etc/vim/vimrc
if filereadable("/etc/vim/vimrc.local")
source /etc/vim/vimrc.local
endif
-------------------------------------------------

已经装上的插件有cppcomplete.vim gtags.vim taglist.vim word_complete.vim
这些插件都是放在/etc/vim/plugin/里面的
参与的文件是以下的

(转载)

-------------------------------------------------
vim + ctags + taglist + cscope + cppcomplete + global 使用初探

由于本人比较喜欢在终端状态下的vim,所以gvim下的扩展使用的不多。这里介绍的也是我简单的使用以上扩展的一点点体会。请大家指正。 emacs 不会用,也没有比较过拉。

1、大体感觉,(因为使用时间不长,不全面)
总体使用的感觉定位查找类型定义基本可以用,但自动补全类型方面由于ctags的功能有限,感觉在大工程时,不一定很好用。

大型程序一定要global的参与,它的分析比较全面,但在vim中的提示信息有限。

taglist 精干,需要ctags的支撑 ,直接可以在左边列出函数列表,全局参数列表。(可以排序)

cscope 比较强大,可以对函数以及部分类型定义进行跳转,但有些BUG,好像在某些条件下无法正确找到分析枚举的定义。

cppcomplete 需要ctags的支撑,可以补全类型或者函数名(可不是普通Ctrl+P/N的那种)

global 新版本可以嵌入vim使用,提供比较完整解析和类型索引,和cscope比,稍微差些的就是对类型引用的打印列表中没有标识这个引用在什么函数中进行的。 其实我的感觉global可能不是为vim所生,它的主要目的是用html的方式进行表达相关的关联关系和索引,使用起来感觉没有cscope的那么贴 切。

2、相关的扩展的安装
代码:

apt-get install exuberant-ctags cscope global

cppcomplete 和 taglist 要到www.vim.org的扩展列表中下载。

taglist 的下载地址: http://www.vim.org/scripts/script.php?script_id=273 cppcomplete 的下载地址: http://www.vim.org/scripts/script.php?script_id=527
然后在 建立目录
代码:

$HOME/.vim/plugin

将下载的cppcomplete.vim 和 taglist.vim拷贝到$HOME/.vim/plugin中。
安装了global后,最新4.8.6 以上版本有带vim的扩展,将它也拷贝到 $HOME/.vim/plugin

debian sid 版本global安装后扩展文件在
代码:

/usr/share/doc/global/examples/gtags.vim.gz

需要拷贝到$HOME/.vim/plugin后解压,解压方法:
代码:

gzip -d gtags.vim.gz

3、使用这些工具
1)准备工作,先修改一下$HOME/.vimrc文件

为了更好的使用cscope请添加如下内容,这样Ctrl-]的跳转将由cscope的tags进行分析。
代码:

if has("cscope") set csprg=/usr/bin/cscope set csto=0 set cst set nocsverb " add any database in current directory if filereadable("cscope.out") cs add cscope.out " else add database pointed to by environment elseif $CSCOPE_DB != "" cs add $CSCOPE_DB endif set csverb set cscopetag set cscopequickfix=s-,g-,c-,d-,t-,e-,f-,i- endif

2)使用cscope
cscope的tag生成最简单的方法是:
在你的开发工程的最上层目录执行cscope-indexer,它会遍历下面的所有目录,生成两个文件,一个是cscope.files,这个文件记录需要生成tags的文件名,可以手工修改,另一个是cscope格式的tags文件cscope.out。

完成后,你在生成了cscope.out的目录打开工程的任意文件,就可以使用Ctrl-]跳转查找类型定义了。
代码:

cs f s xxxx 查找xxxx出现的地方,它能详细列出哪些文件的哪行的哪个函数引用,以及该行的内容,比较不错。 :)

它的所有命令的使用请参考:
代码:

help cscope

3)使用taglist
taglist的功能是即时生成当前文件的函数列表和全局变量列表,便于索引。
在vim中命令模式下使用
Tlist 打开或者关闭当前文件的索引;
TlistSync 立即在打开的索引窗口中定位当前的光标所在位置属于哪个函数或者结构定义中。
还有其他命令,请参考它的帮助文件。

4)ctags的使用
其实cscope是用来替代ctags功能的,ctags的应用广泛,还有其他很多的软件依赖它。

例如要生成cppcomplete需要的tags文件的话,需要运行下面的命令:
代码:

ctags -n -f cppcomplete.tags --fields=+ai --C++-types=+p * -L cscope.files

注意,我在最后使用了参数"-L cscope.files" 这仅仅是借用拉cscope生成的文件索引来帮助ctags去查询相关工程文件生成tags。

5)使用cppcomplete
cppcomplete的使用我只是简单的发现它可以根据上述方法生成的tags文件来自动补全类型成员。
使用很简单,就是在你需要补全类型成员时,按F8键就可以拉。它一般第一次使用时,需要让你确认是使用已有的tags文件还是再生成一次。一般我们自己控制生成tags文件的时机,它就使用就行拉。
但它也受到ctags的分析能力比较差的限制,可能不是每次都能帮到你。

6)使用global
在工程的最上层目录执行命令:
gtags
等待它完成所有需要的global自己格式的tags的生成
生成完后,需要的就是在生成tags的目录打开你的工程的任意文件,用如下命令查询相关类型引用和关联关系:
代码:

Gtags xxxx 查找xxxx的定义 Gtags -r xxxx 查找xxxx的引用 Gtags -s xxxx 查找xxxx出现的地方

它的提示信息很有显,不如cscope更直观,但对大型分析的比较完整。
有时它无法正常分析的类型 可以试试用带-s 的参数的方式找找类型定义。再不行,我就不清楚拉

4、草草的收尾
我使用它们也是一点一点的摸索的没有系统的完整的使用过,也受限于本人的Linux使用经验和工作关系,所以非常简单的使用体会给大家一个参考,希望能有所帮助。更希望后来者能不断补充相关的使用体会。大家都需要交流提高,呵呵。 -----------------------------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: