- 论坛徽章:
- 0
|
VIM代替source Insight
这是我在学习VIVI的时候看着头晕的时候找的,后来发现以前我用vim,那根本就暴殄天物。那么好的一工具,我就只会编辑。太浪费了。
建议新手先学会vim里面的命令,配置。有个教程只要你在终端下输入vimtutor 就有个手把手的教程。
我给个链接吧:
VIM加个插件就变得很牛逼了,比source insight还好(我没用过,因为这东西不免费)。
使用catgs+VIM,catgs有些linux系统是默认安装了的。(今晚发现百度不灵了,我搜不到我看的那篇秘籍教程)
下面是我偷人家的
一、用好系统自带软件ctags
大部分的unix系统都有ctags软件,它能跟vim很好地合作。
用途:
生成c语言的标签文件,实现相关c文件之间的跳转。
用法:
1.生成标签文件
在当前目录下(运行$提示符后面的命令):
$ctags -R .
-R表示recursive,递归,为当前目录及其子目录中的c文件生成标签文件。最后一个.表示在当前目录。
运行完当前目录会多一个文件tags,就是c标签的索引文件。
2.跳转
1)用vim打开一个已经建过标签的c文件
2)ctrl+] 找到光标所在位置的标签定义的地方
3)ctrl+t 回到跳转之前的标签处
注意:此时运行vim,必须在"tags"文件所在的目录下运行。否则,运行它会找不到"tags"文件,而需要在vim中用":set tags="命令设定"tags"文件的路径。对于一个稍微大点的项目,你可能在任何一个目录下打开vim,然而在每个目录下都生成一个tags文件并不是个好主意,那么如何解决呢?方法是在.vimrc中增加一行:
set tags=tags;/
这是告诉vim在当前目录找不到tags文件时请到上层目录查找。
二、需要额外安装的脚本:
1、taglist
下载地址
http://www.vim.org/scripts/script.php?script_id=273
若你下载时地址已改变,请到
www.vim.org
找到正确的地址,这很简单。
用途:
打开后,可以显示源码的整体架构,方便地进行跳转。(用惯source insight的人一定勾起某些回忆了^_^)
用法:
下载插件并安装,使用时在vim中输入命令
:Tlist
即可打开/关闭taglist窗口。
一个简单的方法是设定快捷键,在.vimrc中增加一行:
nnoremap :TlistToggle
这样在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
看看有没有反应?
关于.vimrc
我自己的.vimrc也在不断地完善中,完善的过程中得益于从网络上获取的很多知识,感谢提供信息的朋友,也是他们促使我写下这篇东西。我把自己.vimrc的一部分贴在下面,你可以把这些根据需要加到你的.vimrc里面去。
".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 :copen
nmap :cclose
本文来自CSDN博客,转载请标明出处:
http://blog.csdn.net/zhanghuiliang/archive/2008/10/22/3118426.aspx
V im tags 的使用
ctag 檔案的製作
不講究的話,可以在所解開的 source code 目錄下,下以下指令:
ctags -R *
這樣會有 source code 目錄下產生一個 tags 這個檔(可以使用 -f 選項來指定檔案名),裡頭就包含了整個 source code 的所有檔案的 tags 資訊,包括其下所有的子目錄下的檔案。ctags 已盡可能的做到聰明掃描檔案的能力,會忽略和程式碼無關的檔案。當然 ctags 還有許多精細的參數可以使用,請 man ctags 。
請注意,ctags 預設會將輸出檔排序,因此不必自行另外去排序。有排序有一個好處,那就是 V im 會去使用 binary search 的方式去搜尋,這樣會比較快。
一般的 tag 使用
如果就照上一節的方式產生 tag files,那麼只要在 source code 目錄下由 vim 去開啟檔案的話,會自動載入 tags 這個檔案,無需另行載入,否則要由 :set tags=your.tags 來指定 tags 檔。然後就是照一般使用 V im 線上說明一樣,游標移到識別字或函數名上,按 Ctrl+] ,要回到原處就按 Ctrl+T 。
請注意,V im 啟動時,工作目錄(vim 啟動時的所在目錄)名為 tags 的檔案會自動載入,$VIMRUNTIME/doc 及 $HOME/.vim/doc 目錄下的 tags 檔也會自動載入。而且,凡是載入的 tags 檔裡頭所有標誌文字都可以使用補全鍵來補全,別忘了這個好用的功能。
在目录树中自由使用tag
编辑vim的设置文件(_vimrc或.vimrc),添加两行
set tags=tags;
set autochdir
注意第一个命令里的分号是必不可少的。这个命令让vim首先在当前目录里寻找tags文件,如果没有找到tags文件,或者没有找到对应的目标,就到父目录中查找,一直向上递归。因为tags文件中记录的路径总是相对于tags文件所在的路径,所以要使用第二个设置项来改变vim的当前目录。
使用这两个技巧后,就可以在目录树中自由的浏览源程序了。
让你的vim能够查看函数列表(taglist)
图形界面下的很多编辑软件都可以列出当前编辑文件中的函数列表,以便在编辑的时候能够快速的跳转。vim虽然没有直接提供这种功能,但配合适当的工具和设定,可以完美的实现!
首先还是安装,vim不用装了,ubuntu中自带,用到的ctags需要自己安装一下,不要用旧的那个ctags,新的叫Exuberant Ctags ,apt安装即可:apt-get install exuberant-ctags。
然后按照vim.org上的安装说明 ,下载taglist_45.zip,解压到home目录下的.vim子目录中:
~/.vim$ unzip /home/fwolf/taglist_45.zip
Archive: /home/fwolf/taglist_45.zip
inflating: plugin/taglist.vim
inflating: doc/taglist.txt
进入$HOME/.vim/doc目录,打开VIM 运行“helptags .”命令,注意后面的点不要拉了,这是为了生成ctags帮助文件的索引,然后才能正常使用帮助。(原来vi帮助中的ctrl+]进入链接、ctrl+T返回也是这么来的)
现在,再启动vim,tags功能就可以用了:
大概用法:
在使用vim的时候,使用:Tlist命令切换函数列表的开、关。
按住ctrl键然后按两下w键在正常编辑区域和tags区域中切换。
在tags区域中,把光标移动到变量、函数名称上,然后敲回车,就会自动在正常编辑区域中定位到指定内容了,很方便的。
常用的几项.vimrc设置:
“禁止自动改变当前Vim窗口的大小
let Tlist_Inc_Winwidth=0
“把方法列表放在屏幕的右侧
let Tlist_Use_Right_Window=1
“让当前不被编辑的文件的方法列表自动折叠起来, 这样可以节约一些屏幕空间
let Tlist_File_Fold_Auto_Close=1
本文来自CSDN博客,转载请标明出处:
http://blog.csdn.net/flycofei/archive/2009/05/19/4200642.aspx
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/111390/showart_2168188.html |
|