安装vundle
在home文件夹下创建.vim .vim/bundle,修改权限
rick@ubuntu:~$sudo mkdir -p .vim/bundle
rick@ubuntu:~$ ls -al .vim
total 12
drwxr-xr-x 3 root root 4096 7月 29 16:41 .
drwxr-xr-x 22 rick rick 4096 7月 29 16:41 ..
drwxr-xr-x 2 root root 4096 7月 29 16:41 bundle
rick@ubuntu:~$ sudo chmod 777 .vim -R
rick@ubuntu:~$ ls -al .vim
total 12
drwxrwxrwx 3 root root 4096 7月 29 16:41 .
drwxr-xr-x 22 rick rick 4096 7月 29 16:41 ..
drwxrwxrwx 2 root root 4096 7月 29 16:41 bundle
vundle 会接管 .vim/ 下的所有原生目录,所以先清空该目录,再通过如下命令安装 vundle:
rick@ubuntu:~$ git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
rick@ubuntu:~$ ls -al .vim/bundle/
total 12
drwxrwxrwx 3 root root 4096 7月 29 16:45 .
drwxrwxrwx 3 root root 4096 7月 29 16:41 ..
drwxrwxr-x 8 rick rick 4096 7月 29 16:45 Vundle.vim
将.vimrc文件复制到home文件夹下
rick@ubuntu:~/work/vim$ cp .vimrc ../..
打开配置文件
rick@ubuntu:~$ vim .vimrc
在下面添加所需要的插件
...
call vundle#begin()
Plugin 'VundleVim/Vundle.vim'
Plugin 'altercation/vim-colors-solarized'
Plugin 'tomasr/molokai'
Plugin 'vim-scripts/phd'
Plugin 'Lokaltog/vim-powerline'
Plugin 'octol/vim-cpp-enhanced-highlight'
Plugin 'nathanaelkane/vim-indent-guides'
Plugin 'derekwyatt/vim-fswitch'
Plugin 'kshenoy/vim-signature'
Plugin 'vim-scripts/BOOKMARKS--Mark-and-Highlight-Full-Lines'
Plugin 'majutsushi/tagbar'
Plugin 'vim-scripts/indexer.tar.gz'
Plugin 'vim-scripts/DfrankUtil'
Plugin 'vim-scripts/vimprj'
Plugin 'dyng/ctrlsf.vim'
Plugin 'terryma/vim-multiple-cursors'
Plugin 'scrooloose/nerdcommenter'
Plugin 'vim-scripts/DrawIt'
Plugin 'SirVer/ultisnips'
Plugin 'derekwyatt/vim-protodef'
Plugin 'scrooloose/nerdtree'
Plugin 'fholgado/minibufexpl.vim'
Plugin 'gcmt/wildfire.vim'
Plugin 'sjl/gundo.vim'
Plugin 'Lokaltog/vim-easymotion'
Plugin 'suan/vim-instant-markdown'
Plugin 'lilydjwg/fcitx.vim'
Plugin 'vim-scripts/cscope.vim'
Plugin 'vim-scripts/taglist.vim'
call vundle#end()
…
安装插件,vim打开任意文件,执行以下命令
:PluginUpdate
:PluginInstall
:PluginClean
rick@ubuntu:~$ sudo apt-get install ctags
rick@ubuntu:~$ ctags --help
Exuberant Ctags 5.9~svn20110310, Copyright (C) 1996-2009 Darren Hiebert
Compiled: Oct 7 2014, 13:52:34
Addresses: .sourceforge.net>, http://ctags.sourceforge.net
Optional compiled features: +wildcards, +regex
Usage: ctags [options] [file(s)]
…
进入内核目录执行ctags -R
rick@ubuntu:~$ cd work/linux/src/linux-2.6.22/
rick@ubuntu:~/work/linux/src/linux-2.6.22$ ctags -R
目录下生成tags文件
rick@ubuntu:~/work/linux/src/linux-2.6.22$ ll tags
-rw-rw-r-- 1 rick rick 78414446 10月 7 18:45 tags
在vim中执行
: set tags=/home/work/linux/src/linux-2.6.22/tags
把光标定位到某一函数名上或者宏上, 按下 Ctrl + ], vim就可以自动切换到该函数定义处!
要返回只需要按下Ctrl + o 或者 Ctrl + t.
打开init/main.c文件
光标移到max_cpus处,
按下 Ctrl + ],查看定义
按下Ctrl + o,返回
rick@ubuntu:~$ sudo apt-get install cscope
rick@ubuntu:~$ cscope --help
Usage: cscope [-bcCdehklLqRTuUvV] [-f file] [-F file] [-i file] [-I dir] [-s dir]
[-p number] [-P path] [-[0-8] pattern] [source files]
…
进入内核目录,执行
rick@ubuntu:~/work/linux/src/linux-2.6.22$ cscope -Rbkq
rick@ubuntu:~/work/linux/src/linux-2.6.22$ ll cscope*
-rw-rw-r-- 1 rick rick 294201 10月 7 14:37 cscope.files
-rw-rw-r-- 1 rick rick 15589376 10月 7 14:38 cscope.in.out
-rw-rw-r-- 1 rick rick 125349471 10月 7 14:38 cscope.out
-rw-rw-r-- 1 rick rick 92668236 10月 7 14:38 cscope.po.out
将数据库文件添加到.vimrc中
...
"cscope
nnoremap <leader>fa :call cscope#findInteractive(expand('<cword>'))<CR>
nnoremap <leader>l :call ToggleLocationList()<CR>
" s: Find this C symbol
nnoremap <leader>fs :call cscope#find('s', expand('<cword>'))<CR>
" g: Find this definition
nnoremap <leader>fg :call cscope#find('g', expand('<cword>'))<CR>
" d: Find functions called by this function
nnoremap <leader>fd :call cscope#find('d', expand('<cword>'))<CR>
" c: Find functions calling this function
nnoremap <leader>fc :call cscope#find('c', expand('<cword>'))<CR>
" t: Find this text string
nnoremap <leader>ft :call cscope#find('t', expand('<cword>'))<CR>
" e: Find this egrep pattern
nnoremap <leader>fe :call cscope#find('e', expand('<cword>'))<CR>
" f: Find this file
nnoremap <leader>ff :call cscope#find('f', expand('<cword>'))<CR>
" i: Find files #including this file
nnoremap <leader>fi :call cscope#find('i', expand('<cword>'))<CR>
if filereadable("cscope.out")
cs add csope.out
elseif $CSCOPE_DB != ""
cs add $CSCOPE_DB
endif
…
fs s: Find this C symbol
fg g: Find this definition
fd d: Find functions called by this function
fc c: Find functions calling this function
ft t: Find this text string
fe e: Find this egrep pattern
ff f: Find this file
fi i: Find files #including this file
打开init/main.c文件
按下;fs,查看所有max_cpus出现的地方
taglist 由vundle管理
打开.vimrc文件,将taglist添加到vundle插件列表中
Plugin 'vim-scripts/taglist.vim'
执行安装
:PluginInstall
在配置文件.vimrc中,配置taglist
nmap <F7> :TlistToggle
let Tlist_Ctags_Cmd="/usr/bin/ctags"
let Tlist_Inc_Winwidth=0
let Tlist_Exit_OnlyWindow=0
F7打开或隐藏tag list
工程文件浏览插件
NERDTree由vundle管理
打开.vimrc文件,将nerdtree添加到vundle插件列表中
Plugin 'scrooloose/nerdtree'
执行安装
:PluginInstall
在配置文件.vimrc中,配置NERDTree
...
" 工程文件浏览
" 使用 NERDTree 插件查看工程文件。设置快捷键,速记:file list
nmap fl :NERDTreeToggle
" 设置 NERDTree 子窗口宽度
let NERDTreeWinSize=22
" 设置 NERDTree 子窗口位置
let NERDTreeWinPos="right"
" 显示隐藏文件
let NERDTreeShowHidden=1
" NERDTree 子窗口中不显示冗余帮助信息
let NERDTreeMinimalUI=1
" 删除文件时自动删除文件对应 buffer
let NERDTreeAutoDeleteBuffer=1
…
;fl,打开隐藏NERDTree
ctrl+Tab 正向遍历buffer
ctrl+shift+Tab 逆向遍历buffer
;+hw 跳转左侧window
;+lw 跳转右侧window
;+kw 跳转上侧window
;+jw 跳转下侧window
像 IDE 一样使用 vim
使用 vim + ctags + cscope + taglist 阅读源码