(為了提高工作效率,必須學(xué)會使用一些工具)
. 查看 key 相關(guān)信息說明的命令 :help keycodes
==============================================================================================================
2. ctags
(1). 幫助手冊查看
:help usr_29
(2). 功能
ctags的功能, 只要在unix/linux環(huán)境編程過的人都知道它的歷害! 我在這也作個小小介紹吧: 對瀏覽代碼非常的方便, 可以在函數(shù), 變量之間跳來跳去等等.(注意: 我這里說到的僅是小小的一部分!).
(3). 安裝
首先, 下載ctags安裝包, 然后解壓并安裝, 命令如下:
$ tar -xzvf ctags-5.6.tar.gz
$ cd ctags-5.6
$ make
# make install // 需要root權(quán)限
或:
$ tar -xzvf ctags-5.7.tar.gz
$ cd ctags-5.7
$ ./configure
# make
# make install
(4). 使用方法
然后去你的源碼目錄, 如果你的源碼是多層的目錄, 就去最上層的目錄, 在該目錄下運(yùn)行命令: ctags -R
我現(xiàn)在以 vim71 的源碼目錄做演示
$ cd /home/nuoerll/vim71
$ ctags -R
此時在/home/nuoerll/vim71目錄下會生成一個 tags 文件, 現(xiàn)在用vim打開 /home/nuoerll/vim71/src/main.c
$ vim /home/nuoerll/vim71/src/main.c
再在vim中運(yùn)行命令:
:set tags=/home/nuoerll/vim71/tags
該命令將tags文件加入到vim中來, 你也可以將這句話放到~/.vimrc中去, 如果你經(jīng)常在這個工程編程的話.
對于經(jīng)常在不同工程間編程, 可以在.vimrc中設(shè)置:
set tags=tags; // ; 不能沒有
set autochdir
(5). 使用例子
把光標(biāo)定位到某一函數(shù)名上, 按下 Ctar + ], vim就可以自動切換到該函數(shù)定義處! 要返回只需要按下Ctrl + t .
更多用法, 在vim命令模式輸入 :help usr_29 查看即可.
==============================================================================================================
3. TagList 插件
(1). 幫助手冊查看
:help taglist.txt
(2). 功能
高效地瀏覽源碼, 其功能就像vc中的workpace, 那里面列出了當(dāng)前文件中的所有宏, 全局變量, 函數(shù)名等.
(3). 安裝
下載taglist壓縮包, 然后把解壓的兩個文件taglist.vim 和 taglist.txt 分別放到 $HOME/.vim/plugin 和 $HOME/.vim/doc 目錄中.
(4). 使用方法
首先請先在你的~/.vimrc文件中添加下面語句:
let Tlist_Ctags_Cmd='/bin/ctags' // 若在windows中應(yīng)寫成: let Tlist_Ctags_Cmd='ctags.exe'
let Tlist_Show_One_File=1
let Tlist_OnlyWindow=1
let Tlist_Use_Right_Window=0
let Tlist_Sort_Type='name'
let Tlist_Exit_OnlyWindow=1
let Tlist_Show_Menu=1
let Tlist_Max_Submenu_Items=10
let Tlist_Max_Tag_length=20
let Tlist_Use_SingleClick=0
let Tlist_Auto_Open=0
let Tlist_Close_On_Select=0
let Tlist_File_Fold_Auto_Close=1
let Tlist_GainFocus_On_ToggleOpen=0
let Tlist_Process_File_Always=1
let Tlist_WinHeight=10
let Tlist_WinWidth=18
let Tlist_Use_Horiz_Window=0
此時用vim打開一個c源文件試試:
$ vim ~/vim/src/main.c
進(jìn)入vim后用下面的命令打開taglist窗口.
:Tlist
為了更方便地使用, 可以在.vimrc文件中加入:
map <silent> <leader>tl :TlistToggle<CR>
這樣就可以用 ",tl" 命令進(jìn)行taglist窗口的打開和關(guān)閉之間方便切換了. // 這里的","是我.vimrc設(shè)置的leader, 你也可以設(shè)置成別的, 在.vimrc中修改即可, 如我的: let mapleader=","
==============================================================================================================
4. WinManager 插件
(1). 幫助手冊查看
:help winmanager
(2). 功能
管理各個窗口, 或者說整合各個窗口.
(3). 安裝
下載WinManager.zip壓縮包, 解壓后把*.vim文件放到 $HOME/.vim/plugin 目錄中, 把*.txt文件放到 $HOME/.vim/doc 目錄中.
(4). 使用方法
在.vimrc中加入如下設(shè)置:
let g:winManagerWindowLayout='FileExplorer|BufExplorer' // 這里可以設(shè)置為多個窗口, 如'FileExplorer|BufExplorer|TagList'
let g:persistentBehaviour=0 // 只剩一個窗口時, 退出vim.
let g:winManagerWidth=20
let g:defaultExplorer=1
nmap <silent> <leader>fir :FirstExplorerWindow<cr>
nmap <silent> <leader>bot :BottomExplorerWindow<cr>
nmap <silent> <leader>wm :WMToggle<cr>
(5). 使用例子
在終端輸入vim啟動vim:
$vim
在正常模式下, 敲入 ",wm" 即可看到, vim的左側(cè)新建了兩個窗口:FileExplorer和BufExplorer, 這樣我們即可在FileExplorer窗口很方便地對目錄進(jìn)行查看, 編輯等操作; 在BufExplorer窗口中查看當(dāng)前vim已經(jīng)打開那些文件.
==============================================================================================================
5. cscope
(1). 幫助手冊查看
:help if_cscop.txt
(2). 功能
用Cscope自己的話說 - "你可以把它當(dāng)做是超過頻的ctags", 其功能和強(qiáng)大程度可見一斑吧, 關(guān)于它的介紹我就不詳細(xì)說了, 如果你安裝好了前文介紹的幫助手冊.
(3). 安裝
如果是在linux環(huán)境中, cscope一般都會隨系統(tǒng)一起安裝了; 在windows環(huán)境中, 則需要下載windows版本的(cscope.exe), 然后把它放到path環(huán)境變量所設(shè)
置的目錄中(如: C:\Program Files\Vim\vim72).
(4). 使用方法
在.vimrc中增加如下設(shè)置, 就可以利用相應(yīng)的快捷鍵進(jìn)行不同的查找了.
if has("cscope")
set cscopetag " 使支持用 Ctrl+] 和 Ctrl+t 快捷鍵在代碼間跳來跳去
" check cscope for definition of a symbol before checking ctags: set to 1
" if you want the reverse search order.
set csto=1
" 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
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>
endif
(5). 使用例子
首先進(jìn)入源碼目錄, 在linux終端中輸入以下命令以創(chuàng)建cscope數(shù)據(jù)庫:
$ find ~/work/..Project/ -name "*.h" -o -name "*.cpp" > cscope.files
$ cscope -bkq -i cscope.files
如果是在windows環(huán)境中, 則換成如下命令:
dir /s /b *.cpp *.h > cscope.files
cscope -b -q -k -i cscope.files
然后, 用vim打開一個源文件(如: vim main.cpp),
打開后, 第一件事就是導(dǎo)入cscope數(shù)據(jù)庫了:
:cs add /home/yourname/workpace/cscope.out /home/yourname/workpace
cscope數(shù)據(jù)庫導(dǎo)入成功后, 就可以利用"步驟(4)"中定義的快捷鍵進(jìn)行相關(guān)的查找, 瀏覽等操作了(當(dāng)然也可以直接利用相關(guān)命令, 嘿嘿).
==============================================================================================================
5. c.vim 插件
(1). 幫助手冊查看
help csupport
(2). 功能
C/C++-IDE for Vim/gVim. 簡單的說, 就是如果安裝配置好后, vim/gvim就是一個c/c++編程的一個IDE, 其功能堪比windows里常用的vc.
(3). 安裝
下載cvim.zip壓縮包后, 把壓縮包copy到 $HOME/.vim 目錄(windows下, copy 到 C:\Program Files\Vim\vimfiles)下解壓, 即可.
unzip cvim.zip // 解壓命令
(4). 使用方法
安裝好后就可以直接用了, 具體例子看"使用例子".
(5). 使用例子
在終端用vim打開一個c文件:
$vim hello.c
進(jìn)入vim, 敲入 "\im" 即可發(fā)現(xiàn)一個main函數(shù)框架就這樣快捷簡單完美地寫出.
我比效常用的的操作有(第一列為命令, 第二列為說明, 第三列為該命令所支持的模式(n:普通模式, v:可視模式, i:插入模式):
-- Help ---------------------------------------------------------------
\hm show manual for word under the cursor (n,i)
\hp show plugin help (n,i)
-- Comments -----------------------------------------------------------
\cl end-of-line comment (n,v,i)
\cj adjust end-of-line comment(s) (n,v,i)
\cs set end-of-line comment column (n)
\c* code -> comment /* */ (n,v)
\cc code -> comment // (n,v)
\co comment -> code (n,v)
\cfr frame comment (n,i)
\cfu function comment (n,i)
\cme method description (n,i)
\ccl class description (n,i)
\cd date (n,v,i)
\ct date \& time (n,v,i)
-- Statements ---------------------------------------------------------
\sd do { } while (n,v,i)
\sfo for { } (n,v,i)
\sife if { } else { } (n,v,i)
\se else { } (n,v,i)
\swh while { } (n,v,i)
\ss switch (n,v,i)
-- Preprocessor -------------------------------------------------------
\p< #include <> (n,i)
\p" #include "" (n,i)
\pd #define (n,i)
\pu #undef (n,i)
\pie #if #else #endif (n,v,i)
\pid #ifdef #else #endif (n,v,i)
\pin #ifndef #else #endif (n,v,i)
\pind #ifndef #def #endif (n,v,i)
\pi0 #if 0 #endif (n,v,i)
\pr0 remove #if 0 #endif (n,i)
\pe #error (n,i)
\pl #line (n,i)
\pp #pragma (n,i)
-- Idioms -------------------------------------------------------------
\if function (n,v,i)
\isf static function (n,v,i)
\im main() (n,v,i)
\i0 for( x=0; x<n; x+=1 ) (n,v,i)
\in for( x=n-1; x>=0; x-=1 ) (n,v,i)
\ie enum + typedef (n,i)
\is struct + typedef (n,i)
\iu union + typedef (n,i)
\ip printf() (n,i)
\isc scanf() (n,i)
\ica p=calloc() (n,i)
\ima p=malloc() (n,i)
\isi sizeof() (n,v,i)
\ias assert() (n,v)
\ii open input file (n,i)
\io open output file (n,i)
-- Snippets -----------------------------------------------------------
\nr read code snippet (n,i)
\nw write code snippet (n,v,i)
\ne edit code snippet (n,i)
\np pick up prototype (n,v,i)
\ni insert prototype(s) (n,i)
\nc clear prototype(s) (n,i)
\ns show prototype(s) (n,i)
\ntl edit local templates (n,i)
\ntg edit global templates (n,i)
\ntr rebuild templates (n,i)
-- C++ ----------------------------------------------------------------
\+co cout << << endl; (n,i)
\+c class (n,i)
\+cn class (using new) (n,i)
\+ci class implementation (n,i)
\+cni class (using new) implementation (n,i)
\+mi method implementation (n,i)
\+ai accessor implementation (n,i)
\+tc template class (n,i)
\+tcn template class (using new) (n,i)
\+tci template class implementation (n,i)
\+tcni template class (using new) impl. (n,i)
\+tmi template method implementation (n,i)
\+tai template accessor implementation (n,i)
\+tf template function (n,i)
\+ec error class (n,i)
\+tr try ... catch (n,v,i)
\+ca catch (n,v,i)
\+c. catch(...) (n,v,i)
-- Run ----------------------------------------------------------------
\rc save and compile (n,i)
\rl link (n,i)
\rr run (n,i)
\ra set comand line arguments (n,i)
\rm run make (n,i)
\rg cmd. line arg. for make (n,i)
\rp run splint (n,i)
\ri cmd. line arg. for splint (n,i)
\rk run CodeCheck (TM) (n,i)
\re cmd. line arg. for CodeCheck (TM) (n,i)
\rd run indent (n,v,i)
\rh hardcopy buffer (n,v,i)
\rs show plugin settings (n,i)
\rx set xterm size (n, only Linux/UNIX & GUI)
\ro change output destination (n,i)
關(guān)于此插件的更多功能和各種說明, 請查看幫助手冊, help csupport.
==============================================================================================================
6. omnicppcoplete 插件
(1). 幫助手冊查看
:help omnicppcoplete
(2). 功能
實現(xiàn)像vc那樣的代碼自動補(bǔ)全功能, 比如 this-><Ctrl+X><Ctrl+O> 后, 將出現(xiàn)一個提示框, 其中包含了this指針?biāo)锌梢越邮盏暮瘮?shù)或數(shù)據(jù)成員等.
(3). 安裝
把下載下來的 omnicppcoplete-0.41.zip 壓縮包copy到 $HOME/.vim/ (windows 復(fù)制到 C:\Program Files\Vim\vimfiles ), 然后解壓, 即可.
(4). 使用方法
在.vimrc中添加以下兩條語句:
set nocp "不兼容vi
filetype plugin on "開啟文件類型識別功能
進(jìn)入c++源碼目錄, 在終端執(zhí)行命令 ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .
(5). 使用例子
編寫c++代碼時, 如要自動補(bǔ)全, 敲入 Ctrl+X Ctrl+O, 即可在出現(xiàn)的提示框中用 Ctrl+N 選擇符合要求的.
==============================================================================================================
7. a.vim插件
(1). 幫助手冊查看
這個插件沒有幫助手冊, 不過大可放心使用, 其提供的功能也不是很多, 就幾條命令, 但是用起來真的是很方便.
(2). 功能
在 .h 和 .c/.cpp 文件中切換. (英文原句 "A few of quick commands to swtich between source files and header files quickly.")
(3). 安裝
把下載到的a.vim插件放到 $HOME/.vim/plugin 目錄下, 即可.
(4). 使用方法
只要在vim中輸入以下命令即可完成相應(yīng)的功能:
:A switches to the header file corresponding to the current file being edited (or vise versa)
:AS splits and switches
:AV vertical splits and switches
:AT new tab and switches
:AN cycles through matches
:IH switches to file under cursor
:IHS splits and switches
:IHV vertical splits and switches
:IHT new tab and switches
:IHN cycles through matches
<Leader>ih switches to file under cursor
<Leader>is switches to the alternate file of file under cursor (e.g. on <foo.h> switches to foo.cpp)
<Leader>ihn cycles through matches
==============================================================================================================
8. VisualMark.vim插件
(1). 幫助手冊查看
這個插件沒有幫助手冊, 不過大可放心使用, 其提供的功能也不是很多, 就幾條命令, 但是用起來真的是很方便.
(2). 功能
高亮?xí)?
(3). 安裝
把下載好的VisualMark.vim插件放到 $HOME/.vim/plugin 目錄下, 即可.
(4). 使用方法
只要在vim中執(zhí)行以下命令即可完成相應(yīng)的功能:
1. For gvim, use "Ctrl + F2" to toggle a visual mark.
For both vim and gvim, use "mm" to toggle a visual mark.
2. Use "F2" to navigate through the visual marks forward in the file.
3. Use "Shift + F2" to navigate backwards.
==============================================================================================================
9. Mark.vim插件
(1). 幫助手冊查看
這個插件沒有幫助手冊, 不過大可放心使用, 其提供的功能也不是很多, 就幾條命令, 但是用起來真的是很方便.
(2). 功能
這個插件與vim中自帶的'*'與'#'非常相像. 不同之處是: vim中的'*'與'#'命令只能同時高亮一個同類詞組(或正則表達(dá)式的搜索結(jié)果), 而Mark.vim插件可以同時高亮多個.
(3). 安裝
把下載好的Mark.vim插件放到 $HOME/.vim/plugin 目錄中, 即可.
(4). 使用方法
\m mark or unmark the word under (or before) the cursor
\r manually input a regular expression. 用于搜索.
\n clear this mark (i.e. the mark under the cursor), or clear all highlighted marks .
\* 把光標(biāo)向前切換到當(dāng)前被Mark的MarkWords中的下一個MarkWord.
\# 把光標(biāo)向后切換到當(dāng)前被Mark的MarkWords中的上一個MarkWord.
\/ 把光標(biāo)向前切換到所有被Mark的MarkWords中的下一個MarkWord.
\? 把光標(biāo)向后切換到所有被Mark的MarkWords中的上一個MarkWord.
說明: 這些命令中的 '\' 是 vim 中的 mapleader, 你也可以設(shè)置為別的: 如, 若要設(shè)置為 ',', 把下面這條語句加到.vimrc文件中, 即可,
let mapleader=","