VIM 多文件跳转

转自:http://blog.chinaunix.net/space.php?uid=22247208&do=blog&id=2655609

windows下很多人都使用source insight 编写和查看代码。linux下可以使用VIM,刚开始会觉得VIM像windows下的记事本,而如果使用得当,它并不比source insight 逊色。
在这里,我会尽我所能细致地讲清楚如何把vim变成source insight, 然而你仍然需要积极地思考,并且必须自己去摸索一些东西。
为了避免过于罗嗦,我把基础的部分放在后面,如果你越看越觉得太简单了,那么本文并不适合你;如果看完前面的仍有疑问或者看不懂前面说的是什么东西,不用担心,后面会有一些必备的知识介绍。

一、用好系统自带软件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 <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
看看有没有反应?

关于.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 : l a n g =   " U T F 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

猜你喜欢

转载自blog.csdn.net/houxiaoni01/article/details/80102975
今日推荐