VI的使用方法這裏就不羅嗦了,本文假設大家已花了20分鐘學習過了vitutor。 ^_^
本節我們主要講一下Ctags這個程序。儘管ctags也可以支持其它編輯器,但是它正式支持的只有VIM。並且VIM中已經默認安裝了Ctags,它可以幫助程序員很容易地瀏覽源代碼。
熟練的使用ctags僅需記住下面七條命令:(很簡單吧,呵呵)
1. $ ctags –R * ($ 爲Linux系統Shell提示符)
2. $ vi –t tag (請把tag替換爲您欲查找的變量或函數名)
3. :ts (ts 助記字:tags list, “:”開頭的命令爲VI中命令行模式命令)
4. :tp (tp 助記字:tags preview)---此命令不常用,可以不用記
5. :tn (tn 助記字:tags next) ---此命令不常用,可以不用記
6. Ctrl + ]
7. Ctrl + T
下面我們逐條的解釋上面的命令:
“$ ctags –R *”:“-R”表示遞歸創建,也就包括源代碼根目錄(當前目錄)下的所有子目錄。“*”表示所有文件。這條命令會在當前目錄下產生一個“tags”文件,當用戶在當前目錄中運行vi時,會自動載入此tags文件。Tags文件中包括這些對象的列表:
用#define定義的宏
枚舉型變量的值
函數的定義、原型和聲明
名字空間(namespace)
類型定義(typedefs)
變量(包括定義和聲明)
類(class)、結構(struct)、枚舉類型(enum)和聯合(union)
類、結構和聯合中成員變量或函數
VIM用這個“tags”文件來定位上面這些做了標記的對象。
剩下的命令就是定位這些對象的方法:
“$ vi –t tag” :在運行vim的時候加上“-t”參數,例如:
[/usr/src]$ vim -t main
這個命令將打開定義“main”(變量或函數或其它)的文件,並把光標定位到這一行。如果這個變量或函數有多處定義,在VI命令行模式“:ts”命令就能列出一個列表供用戶選擇。 “:tp”爲上一個tag標記文件,“:tn”爲下一個tag標記文件。當然,若當前tags文件中用戶所查找的變量或函數名只有一個,“:tp,:tn”命令不可用。 :(
最方便的方法是把光標移到變量名或函數名上,然後按下“Ctrl+]”,這樣就能直接跳到這個變量或函數定義的源文件中,並把光標定位到這一行。用“Ctrl+t”可以退回原來的地方。即使用戶使用了N次“Ctrl+]”查找了N個變量,按N次“Ctrl+t”也能回到最初打開的文件,它會按原路返回 。 ^_^
注意:運行vim的時候,必須在“tags”文件所在的目錄下運行。否則,運行vim的時候還要用“:settags=”命令設定“tags”文件的路徑,這樣vim才能找到“tags”文件。在完成編碼時,可以手工刪掉tags文件(帚把不到,灰塵不會自己跑
在Ubuntu中vim的配置文件存放在/etc/vim目錄中,配置文件名爲Ubuntu vimrc
前提:已經安裝了vim,建議安裝vim-full,即:sudoapg-get install vim-full
1.ctags:
下載ctags,可從官網上下載,如:ctags-5.7.tag.gz
tarxzvf ctags-5.7.tag.gz
cdctags-5.7
./configure
make
sudomake install
具體操作時目錄可能不一樣
2.taglist:
cd~
mkdir.vim
cd.vim
mkdirplugin
unzip(path自定)/taglist_45.zip
ls
//解壓成功會在.vim下生成兩個文件夾,doc, plugin
測試一下插件是否安裝好,可以運行一下命令
1、ctags--version,有信息則說明安裝好了
2、進入一個原文件,vitest.c,進入命令模式,輸入命令":Tlist",如果出現左右兩欄分欄顯示,則說明OK
把ctags關聯到taglist
vi~/.vim/plugin/taglist.vim
在命令行模式下輸入命令查抄":/loaded_taglist"
找到後,在該行前面加上如下行:
letTlist_Ctags_Cmd="/usr/bin/ctags""目錄可能有變,或許是/usr/local/bin/ctags
OK
到你想瀏覽的目錄下
ctags-R
vitest.c
:syntaxon //打開語法高亮
:setcindent //打開自動縮進
:Tlist//OK
3.cscope
sudo apt-get install cscope
安裝好開發工具後,配置一下.vimrc文件在用戶的主目錄下:
我的.vimrc如下:
set hlsearch
set ai
syntax on
set number
set nocompatible
set columns=80
filetype on
set history=1000
set autoindent
set smartindent
set tabstop=4
set shiftwidth=4
set showmatch
set guioptions-=T
set vb t_vb=
set ruler
set nohls
set noexpandtab
set ignorecase
set mouse=a
set cindent
set incsearch
set fencs=utf-8,cp936
set showcmd
"Tlist的內部變量。函數列表。
let Tlist_Use_Right_Window=1
let Tlist_File_Fold_Auto_Close=1
" "函數和變量列表
map <F4> :TlistToggle<CR>
" "全能補全
inoremap <F8> <C-x><C-o>
" " 沒事,鼠標畫線玩的。
noremap <F9> :call ToggleSketch()<CR>
set wildmenu
" " 啓動函數變量快速瀏覽的時間設置
set updatetime=100
map <F2> :split<CR>
map <F3> :vsplit<CR>
map <F5> :q!<CR>
map <F6> :w!<CR>
map <F7> :wq!<CR>
autocmd FileType python set omnifunc=pythoncomplete#Complete
set hlsearch
set ai
syntax on
set number
set nocompatible
set columns=80
filetype on
set history=1000
set autoindent
set smartindent
set tabstop=4
set shiftwidth=4
set showmatch
set guioptions-=T
set vb t_vb=
set ruler
set nohls
set noexpandtab
set ignorecase
set mouse=a
set cindent
set incsearch
set fencs=utf-8,cp936
set showcmd
"Tlist的內部變量。函數列表。
let Tlist_Use_Right_Window=1
let Tlist_File_Fold_Auto_Close=1
" "函數和變量列表
map <F4> :TlistToggle<CR>
" "全能補全
inoremap <F8> <C-x><C-o>
" " 沒事,鼠標畫線玩的。
noremap <F9> :call ToggleSketch()<CR>
set wildmenu
" " 啓動函數變量快速瀏覽的時間設置
set updatetime=100
map <F2> :split<CR>
map <F3> :vsplit<CR>
map <F5> :q!<CR>
map <F6> :w!<CR>
map <F7> :wq!<CR>
autocmd FileType python set omnifunc=pythoncomplete#Complete
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" CSCOPE settings for vim
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
" This file contains some boilerplate settings for vim's cscope interface,
" plus some keyboard mappings that I've found useful.
"
" USAGE:
" -- vim 6: Stick this file in your ~/.vim/plugin directory (or in a
" 'plugin' directory in some other directory that is in your
" 'runtimepath'.
"
" -- vim 5: Stick this file somewhere and 'source cscope.vim' it from
" your ~/.vimrc file (or cut and paste it into your .vimrc).
"
" NOTE:
" These key maps use multiple keystrokes (2 or 3 keys). If you find that vim
" keeps timing you out before you can complete them, try changing your timeout
" settings, as explained below.
"
" Happy cscoping,
"
" Jason Duell [email protected] 2002/3/7
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" This tests to see if vim was configured with the '--enable-cscope' option
" when it was compiled. If it wasn't, time to recompile vim...
if has("cscope")
""""""""""""" Standard cscope/vim boilerplate
" use both cscope and ctag for 'ctrl-]', ':ta', and 'vim -t'
set cscopetag
" check cscope for definition of a symbol before checking ctags: set to 1
" if you want the reverse search order.
set csto=0
" add any cscope database in current directory
if filereadable("cscope.out")
cs add cscope.out
" else add the database pointed to by environment variable
elseif $CSCOPE_DB != ""
cs add $CSCOPE_DB
endif
" show msg when any other cscope db added
set cscopeverbose
""""""""""""" My cscope/vim key mappings
"
" The following maps all invoke one of the following cscope search types:
"
" 's' symbol: find all references to the token under cursor
" 'g' global: find global definition(s) of the token under cursor
" 'c' calls: find all calls to the function name under cursor
" 't' text: find all instances of the text under cursor
" 'e' egrep: egrep search for the word under cursor
" 'f' file: open the filename under cursor
" 'i' includes: find files that include the filename under cursor
" 'd' called: find functions that function under cursor calls
"
" Below are three sets of the maps: one set that just jumps to your
" search result, one that splits the existing vim window horizontally and
" diplays your search result in the new window, and one that does the same
" thing, but does a vertical split instead (vim 6 only).
"
" I've used CTRL-\ and CTRL-@ as the starting keys for these maps, as it's
" unlikely that you need their default mappings (CTRL-\'s default use is
" as part of CTRL-\ CTRL-N typemap, which basically just does the same
" thing as hitting 'escape': CTRL-@ doesn't seem to have any default use).
" If you don't like using 'CTRL-@' or CTRL-\, , you can change some or all
" of these maps to use other keys. One likely candidate is 'CTRL-_'
" (which also maps to CTRL-/, which is easier to type). By default it is
" used to switch between Hebrew and English keyboard mode.
"
" All of the maps involving the <cfile> macro use '^<cfile>$': this is so
" that searches over '#include <time.h>" return only references to
" 'time.h', and not 'sys/time.h', etc. (by default cscope will return all
" files that contain 'time.h' as part of their name).
" To do the first type of search, hit 'CTRL-\', followed by one of the
" cscope search types above (s,g,c,t,e,f,i,d). The result of your cscope
" search will be displayed in the current window. You can use CTRL-T to
" go back to where you were before the search.
"
nmap <C-\>s :cs find s <C-R>=expand("<cword>")<CR><CR>
nmap <C-\>g :cs find g <C-R>=expand("<cword>")<CR><CR>
nmap <C-\>c :cs find c <C-R>=expand("<cword>")<CR><CR>
nmap <C-\>t :cs find t <C-R>=expand("<cword>")<CR><CR>
nmap <C-\>e :cs find e <C-R>=expand("<cword>")<CR><CR>
nmap <C-\>f :cs find f <C-R>=expand("<cfile>")<CR><CR>
nmap <C-\>i :cs find i ^<C-R>=expand("<cfile>")<CR>$<CR>
nmap <C-\>d :cs find d <C-R>=expand("<cword>")<CR><CR>
" Using 'CTRL-spacebar' (intepreted as CTRL-@ by vim) then a search type
" makes the vim window split horizontally, with search result displayed in
" the new window.
"
" (Note: earlier versions of vim may not have the :scs command, but it
" can be simulated roughly via:
" nmap <C-@>s <C-W><C-S> :cs find s <C-R>=expand("<cword>")<CR><CR>
nmap <C-@>s :scs find s <C-R>=expand("<cword>")<CR><CR>
nmap <C-@>g :scs find g <C-R>=expand("<cword>")<CR><CR>
nmap <C-@>c :scs find c <C-R>=expand("<cword>")<CR><CR>
nmap <C-@>t :scs find t <C-R>=expand("<cword>")<CR><CR>
nmap <C-@>e :scs find e <C-R>=expand("<cword>")<CR><CR>
nmap <C-@>f :scs find f <C-R>=expand("<cfile>")<CR><CR>
nmap <C-@>i :scs find i ^<C-R>=expand("<cfile>")<CR>$<CR>
nmap <C-@>d :scs find d <C-R>=expand("<cword>")<CR><CR>
" Hitting CTRL-space *twice* before the search type does a vertical
" split instead of a horizontal one (vim 6 and up only)
"
" (Note: you may wish to put a 'set splitright' in your .vimrc
" if you prefer the new window on the right instead of the left
nmap <C-@><C-@>s :vert scs find s <C-R>=expand("<cword>")<CR><CR>
nmap <C-@><C-@>g :vert scs find g <C-R>=expand("<cword>")<CR><CR>
nmap <C-@><C-@>c :vert scs find c <C-R>=expand("<cword>")<CR><CR>
nmap <C-@><C-@>t :vert scs find t <C-R>=expand("<cword>")<CR><CR>
nmap <C-@><C-@>e :vert scs find e <C-R>=expand("<cword>")<CR><CR>
nmap <C-@><C-@>f :vert scs find f <C-R>=expand("<cfile>")<CR><CR>
nmap <C-@><C-@>i :vert scs find i ^<C-R>=expand("<cfile>")<CR>$<CR>
nmap <C-@><C-@>d :vert scs find d <C-R>=expand("<cword>")<CR><CR>
""""""""""""" key map timeouts
"
" By default Vim will only wait 1 second for each keystroke in a mapping.
" You may find that too short with the above typemaps. If so, you should
" either turn off mapping timeouts via 'notimeout'.
"
"set notimeout
"
" Or, you can keep timeouts, by uncommenting the timeoutlen line below,
" with your own personal favorite value (in milliseconds):
"
"set timeoutlen=4000
"
" Either way, since mapping timeout settings by default also set the
" timeouts for multicharacter 'keys codes' (like <F1>), you should also
" set ttimeout and ttimeoutlen: otherwise, you will experience strange
" delays as vim waits for a keystroke after you hit ESC (it will be
" waiting to see if the ESC is actually part of a key code like <F1>).
"
"set ttimeout
"
" personally, I find a tenth of a second to work well for key code
" timeouts. If you experience problems and have a slow terminal or network
" connection, set it higher. If you don't set ttimeoutlen, the value for
" timeoutlent (default: 1000 = 1 second, which is sluggish) is used.
"
"set ttimeoutlen=100
endif
注:
“U”命令自己改變自己,“u”命令撤銷操作,CTRL-R命令重做操作。這有點亂,但不用
擔心,用“u”和CTRL-R命令你可以切換到任何狀態。
流行的文本編輯器通常都有前進和後退功能,可以在文件中曾經瀏覽過的位置之間來回移動。在 vim 中使用 Ctrl-O 執行後退,使用 Ctrl-I 執行前進。
cscope -Rbq Ctrl+d退出cscope
在Ubuntu中vim的配置文件存放在/etc/vim目錄中,配置文件名爲Ubuntu vimrc
vim d 刪除行 x刪除字符 gf轉到頭文件 Ctrl+k系統函數幫助,或者K,gd 轉到變量定義。
從別的編輯器裏粘貼到vim裏的代碼經常由於不正常的縮進變得格式混亂。
格式化全文: gg=G (注意不要在命令行裏輸入)
自動縮進當前行: ==
:help gg
:help =
:help G
:help ‘formatprg’
:help C-indenting
ctrl +p 代碼補全。
vim gnome