您的位置:首页 > 运维架构 > Linux

打造linux下的sourceinsight(待续)

2009-01-30 18:15 369 查看
1、CTAGS

一、用好系统自带软件ctags
大部分的unix系统都有ctags软件,它能跟vim很好地合作。
用途:
生成c语言的标签文件,实现相关c文件之间的跳转。
用法:
1.生成标签文件
在当前目录下(运行$提示符后面的命令):
$ctags -R .
-R表示recursive,递归,为当前目录及其子目录中的c文件生成标签文件。最后一个.表示在当前目录。
运行完当前目录会多一个文件tags,就是c标签的索引文件。

2.跳转
1)用vim打开一个已经建过标签的c文件
2)ctrl+] 找到光标所在位置的标签定义的地方
3)ctrl+o 回到跳转之前的标签处

注意:此时运行vim,必须在"tags"文件所在的目录下运行。否则,运行它会找不到"tags"文件,而需要在vim中用":set
tags="命令设定"tags"文件的路径。对于一个稍微大点的项目,你可能在任何一个目录下打开vim,然而在每个目录下都生成一个tags文件并不
是个好主意,那么如何解决呢?方法是在.vimrc中增加一行:
set tags=tags;/
这是告诉vim在当前目录找不到tags文件时请到上层目录查找。

2:TagList

1、taglist
下载地址http://www.vim.org/scripts/script.php?script_id=273
若你下载时地址已改变,请到 www.vim.org 找到正确的地址,这很简单。

用途:
打开后,可以显示源码的整体架构,方便地进行跳转。(用惯source insight的人一定勾起某些回忆了^_^)
用法:
下载插件并安装,使用时在vim中输入命令
:Tlist
即可打开/关闭taglist窗口。
一个简单的方法是设定快捷键,在.vimrc中增加一行:
nnoremap <silent> <F8> :TlistToggle<CR>
这样在vim中按F8就可以打开/关闭taglist了。
更多相关配置请看后面关于.vimrc的介绍。

三、基础知识探讨

约定:为了方便和准确,我们约定本文中"$"标示符后的命令为在终端下运行,而":"后的命令为在vim中运行。

VIM的配置文件一般放在用户主文件夹下,也就是非root状态时在终端运行
$cd ~/
会到的那个目录,文件名为.vimrc。
看不到?有两个可能:
1、文件名前面有一个点,表示是隐藏文件,ls查看时需加-a选项。
$ls -a
2、你还没有建.vimrc文件,自己创建一个就行,先建个空的吧,以后可以不断往里面填东西。
$touch .vimrc

主文件夹下还有一个.vim文件夹,没有请自己mkdir
$mkdir ~/.vim
在.vim文件夹下,再建两个子文件夹:plugin和doc
$mkdir ~/.vim/plugin
$mkdir ~/.vim/doc
plugin文件夹下放插件,doc文件夹下放相应的help文档。
去下一个taglist吧(我们应该把它叫作脚本还是插件呢?它是放在plugin文件夹下的,那么应该是插件;而在vim.org,它是作为scripts存在,那么应当是脚本。),我们当作例子来请解。
下载的是一个zip包,把它放在 ~/.vim 目录下,然后
$unzip filename.zip
它已经自动把taglist.vim和taglist.txt分别放到plugin、doc文件夹下了。
这时重新启动vim
$vim
运行
:Tlist
发现旁边多了一栏没有?如果你打开的是c文件,并且已经生成了tags文件,那么里面应当会显示一些有用的信息。
这个时候,taglist的help文档已经在 ~/.vim/doc 目录下了,但是你在vim下敲
:help Tlist
却没有任何反应,那是因为vim还没有获取帮助文档里面的tag,解决方法是在vim下
:helptags ~/.vim/doc
现在,你再
:help Tlist
看看有没有反应?

Taglis技巧:在左边的tags区域和正常编辑区域切换时用ctrl+2个w。


".vimrc
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" General
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"For ctags, then it can find the 'tags' file even not in current directory
set tags=tags;/

"Get out of VI's compatible mode..
set nocompatible

"Sets how many lines of history VIM har to remember
set history=400

"Set to auto read when a file is changed from the outside
set autoread

"Have the mouse enabled all the time:
"when you need to copy from vim, maybe you have to ':set mouse=' first
set mouse=a

"""""""""""""""""""""""""""""""""""""
" Colors and Fonts
"""""""""""""""""""""""""""""""""""""
"Enable syntax highlight
syntax enable

"set colorscheme
colorscheme elflord
"endif

"""""""""""""""""""""""""""""""""""""
" VIM userinterface
"""""""""""""""""""""""""""""""""""""
"Set 7 lines to the curors away from the border- when moving vertical..
set so=7

"Turn on WiLd menu
set wildmenu

"Always show current position
set ruler

"The commandbar is 2 high
set cmdheight=2

"Show line number
set nu

"Set backspace
set backspace=eol,start,indent

"Bbackspace and cursor keys wrap to
set whichwrap+=<,>,h,l

"show matching bracets
set showmatch

"How many tenths of a second to blink
set mat=2

"Highlight search things
set hlsearch
"imediately show the search result
set is

"""""""""""""""""""""""""""""""""""""
" Folding
"""""""""""""""""""""""""""""""""""""
"Enable folding, I find it very useful
set nofen
set fdl=0

"""""""""""""""""""""""""""""""""""""
" Text options
"""""""""""""""""""""""""""""""""""""
set expandtab
set shiftwidth=2
set ambiwidth=double
set smarttab
"Set Tab=4 spaces
set ts=4
set lbr
set tw=500
set selection=inclusive
""""""""""""""""""""""""""""""
" Indent
""""""""""""""""""""""""""""""
"Auto indent
set ai
"Set auto indent width = 4 spaces
set sw=4

"Smart indet
set si

"C-style indenting
set cindent "usage: select codes, press '=' key, the codes will autoindenting

"Wrap lines
set wrap

"Encoding settings
if has("multi_byte")
" Set fileencoding priority
if getfsize(expand("%")) > 0
set fileencodings=ucs-bom,utf-8,cp936,big5,euc-jp,euc-kr,latin1
else
set fileencodings=cp936,big5,euc-jp,euc-kr,latin1
endif

" CJK environment detection and corresponding setting
if v:lang =~ "^zh_CN"
" Use cp936 to support GBK, euc-cn == gb2312
set encoding=cp936
set termencoding=cp936
set fileencoding=cp936
elseif v:lang =~ "^zh_TW"
" cp950, big5 or euc-tw
" Are they equal to each other?
set encoding=big5
set termencoding=big5
set fileencoding=big5
elseif v:lang =~ "^ko"
" Copied from someone's dotfile, untested
set encoding=euc-kr
set termencoding=euc-kr
set fileencoding=euc-kr
elseif v:lang =~ "^ja_JP"
" Copied from someone's dotfile, unteste
set encoding=euc-jp
set termencoding=euc-jp
set fileencoding=euc-jp
endif
" Detect UTF-8 locale, and replace CJK setting if needed
if v:lang =~ "utf8$" || v:lang =~ "UTF-8$"
set encoding=utf-8
set termencoding=utf-8
set fileencoding=utf-8
endif
else
echoerr "Sorry, this version of (g)vim was not compiled with multi_byte"
endif

"""""""""""""""""""""""""""""""""""""
"plugins

"""""""""""""""""""""""""""""""""""""
" Tlist
if &diff
let Tlist_Auto_Open=0 "don't auto pen when compare two files
else
let Tlist_Auto_Open=1 "auto pen Tlist when open a file
endif

"set taglist window in right, delete the following line if you don't like
let Tlist_Use_Right_Window=1
let Tlist_Auto_Update=1
let Tlist_File_Fold_Auto_Close=1
"auto close Tlist when exiting file.
let Tlist_Exit_OnlyWindow = 1

nmap <F7> :copen<CR>
nmap <F6> :cclose<CR>

3、omnicppcomplete

【简介】
当ctags使用ctags -R的命令,总是只能打出c的tag,但是c++的tag不能打出来,
所以用<C-P>,<C-x><C-o>,不太好使。
所以需要用到vim插件,omnicppcomplete(需要vim7.1以上)。
【omnicppcomplete下载地址】
omnicppcomplete: http://www.vim.org/scripts/script.php?script_id=1520(new-omni-completion(全能补全)) 【安装方法】
install details
1) Unzip the plugin to ~/.vim (unix) or %HOMEPATH%/vimfiles (windows)
2) Run Vim and type the following command :
:helptags $HOME/.vim/doc
or
:helptags $HOME/vimfiles/doc

【设置步骤】

~/.vimrc:
filetype plugin indent on ,
set completeopt=longest,menu

【如何打tags】
shell下执行如下命令:
ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .
就产生了tags文件。
【如何使用tags】
方法1、把tags文件放在工程的根目录,这样当vim从根目录启动时就能自动找到
方法2、在vim里面执行如下命令:
:set tags=/workspace/Server/Rel0.8.2/HomeServer/src/release/tags
:set tags+=/workspace/Server/Rel0.8.2/HomeServer/src/release/tags.lib
vim就能找到这些tags。
【如何查看vim里面的tag设置】
在vim下打入如下命令:
:set tags
就能看到tags设定。

【如何方便的使用tags】
在每次启动vim都要,设置tags,很费劲的。所有想了一下方法,来使用tags。
1、在vimrc末尾加入如下代码:
:if getfsize(".vimscript")>0
source .vimscript
:endif
这样vim在启动的时候就会自动加载,.vimscipt的文件的里面的vim命令。
2、在工程的根目录下,编辑.vimscript文件,加入如下代码:
:set tags=/workspace/Server/Rel0.8.2/HomeServer/src/release/tags
:set tags+=/workspace/Server/Rel0.8.2/HomeServer/src/release/tags.lib
3、在工程的根目录启动vim时,就会自动加载,.vimscript的配置。这样就很方便了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: