最近由於公司編程的需要,我發現鍵盤流的很多好處,因爲由於沒有過於繁瑣的GUI 的限制,速度也就可以提升,更何況在linux下工作,把自己打造成一個鍵盤流還是有很多好處的。
1、 準備工作,首先可以查看一下在linux機器上的vim的版本,建議大家用新的Vim 7.2版本,據說這個版本還是比較穩定的。
如果沒有安裝vim7.2可以到vim 的官方網站上下載一個版本。http://www.vim.org/ 這個網站很實用,所以的和Vim相關的plugin都可以通
過這個網站搜索到。這篇文章沒有專門介紹如何在linux上安裝vim的知識,以後補上。
1.1) 下載安裝c.vim
下載c.vim http://www.vim.org/scripts/download_script.php?src_id=9679
mkdir ~/.vim 所有的文件都要安裝在這個目錄下
cd ~/.vim
unzip /usr/src/cvim.zip
解壓到~/.vim的目錄下,如果想解壓到一個專門的目錄下可以加上 -d 的參數。
啓用這一插件:
vim ~/.vimrc
創建一個.vimrc的文件作爲你的整體vim環境的配置。
filetype plugin on
我們後面還要用到這個文件,這裏只是寫一句這樣的腳本,讓cvim可以按照C的習慣工作。
下面費點時間講一下c.vim 的使用,本來我以爲linux下面的編程工具和windows 的visual studio的功能,特別再加上一個assistant 就更不能比了,但是用過之後發現還是vim更加好用,鍵指如飛的感覺。
下面的內容摘錄自網上的一些文章,這裏我都已經試過了,相當好用。
Feature 1: Add Automatic Header to *.c file
當你新建一個擴展名.c的文件時候,自動在文件頭部增加頭部註釋
$ vim myprogram.c /* * =================================================
* Filename: myprogram.c * * Description: * * Version: 1.0
* Created: 01/19/09 20:23:25 * Revision: none * Compiler: gcc * * Author: Dr. Fritz Mehner (mn), [email protected] * Company: FH Südwestfalen, Iserlohn
* * ================================================= */
其中如果你要改變 AUTHOR 和 COMPANY的值, 要修改模板文件 ~/.vim/c-support/templates/Templates
$ vim ~/.vim/c-support/templates/Templates |AUTHOR| = geekstuff |AUTHORREF| = gk |EMAIL| = subscribe@geekstuff |COMPANY| = thegeekstuff.com
現在編輯一個新文件,看看你的作者和公司名稱改變了沒有。
$ vim myprogram.c /* * ================================================= * * Filename: myprogram.c * * Description: * * Version: 1.0 * Created: 01/19/09 20:26:43 * Revision: none * Compiler: gcc * * Author: geekstuff (gk), subscribe@geekstuff * Company: thegeekstuff.com * * ================================================= */
Feature 2: 用/if 添加c函數
鍵入/if 加函數名稱就會在文件中自動完成函數的定義
Feature 3: 用/im 添加main函數
Feature 4: 用 /cfu 添加函數註釋
Feature 5: 用/cfr添加註釋框架
Feature 6: 用/p<包含頭文件
Type /p< in the normal mode, which will include the text “#include <>”, and places the cursor in the < symbol in Insert mode where you can type the header file name.
Feature 7: 保存編譯文件.
保存並編譯文件 /rc.
運行 /rr.
Feature 8: 用/nr 插入預定義的代碼片段
The plugin comes with few pre-defined code snippets that you can insert into your code. Following are the default code snippets that comes with the plugin.
$ ls ~/.vim/c-support/codesnippets Makefile calloc_double_matrix.c main.c print_double_array.c.noindent Makefile.multi-target.template calloc_int_matrix.c main.cc print_int_array.c.noindent
For example, if you want to create a function that will Allocate a dynamic int-matrix of size rows*columns; return a pointer, you can re-use it from the existing code snippets. Following is the content of the calloc_int_matrix.c pre-defined code snippets.
/* * === FUNCTION ====================================================================== * Name: calloc_int_matrix * Description: Allocate a dynamic int-matrix of size rows*columns; return a pointer. * ===================================================================================== */ int** calloc_int_matrix ( int rows, int columns ) { int i; int **m; m = calloc ( rows, sizeof(int*) ); /* allocate pointer array */ assert( m != NULL ); /* abort if allocation failed */ *m = calloc ( rows*columns, sizeof(int) ); /* allocate data array */ assert(*m != NULL ); /* abort if allocation failed */ for ( i=1; i m[i] = m[i-1] + columns; return m; } /* ———- end of function calloc_int_matrix ———- */
如果要插入這段代碼,可以鍵入/nr 文件名稱,代碼就會自動插入
1.2) 下載Ctags
到http://ctags.sourceforge.net/下載ctags源碼
ctags-5.6.tar.gz http://prdownloads.sourceforge.net/ctags/ctags-5.6.tar.gz
然後解壓並且安裝
tar zxvf ctags-5.6.tar.gz
cd ctags-5.6
./configure && make && make install
1.3) 下載安裝taglist
taglist 依賴於ctags 插件,所以要安裝ctags 插件,然後才能安裝taglist,否則不能用。
到http://vim.sourceforge.net/scripts/script.php?script_id=273
下載taglist_42.zip
這裏可能有更新的版本,所以要根據文檔的說明,配合vim的版本纔可以安裝。
解壓文件:unzip -d taglist taglist_42.zip -d 表示要把文件解壓到某個目錄下。
解壓得到兩個文件。./taglist/doc/taglist.txt; ./taglist/plugin/taglist.vim
分別複製到各自的目錄下:
cp ./taglist/doc/taglist.txt /usr/share/vim/vim72/doc/
cp ./taglist/plugin/taglist.vim /usr/share/vim/vim72/plugin/
但是我認爲這兩步複製操作不是必要的。
不過下面兩步操作是必要的。
cp ./taglist/doc/taglist.txt ~/.vim/doc/
cp ./taglist/plugin/taglist.vim ~/.vim/plugin/
這兩步的目的是讓taglist和ctag聯繫起來。
至此cview 和 ctag 加上 taglist的工具基本環境已經搭好了,下面可以用這些工具生成代碼的tag然後方便瀏覽,有了這些工具,假以時日,練習熟練,加上vim和linux shell的強大功能就可以方便的工作了。
2、使用Ctags 和 Taglist
:helptags ~/.vim/doc 生成幫助標籤
:help taglist.txt 查看幫助文檔
配置 ~/.vimrc文件。
filetype plugin on
let Tlist_Ctags_Cmd = '/usr/local/bin/ctags' “設置ctags命令目錄
let Tlist_Show_One_File = 1 "不同時顯示多個文件的tag,只顯示當前文件的
let Tlist_Exit_OnlyWindow = 1 "如果taglist窗口是最後一個窗口,則退出vim
let Tlist_Use_Right_Window = 1 “讓taglist窗口在右側顯示
命令與用法:
在Taglist窗口按
回車鍵:跳到光標所在的標記的定義處(如將光標移到main函數,按回車鍵)
o:新建一個窗口,跳到標記定義處
p:預覽標記定義(仍然在taglist窗口)
空格:顯示標記的原型(如函數原型)
u:更新標記列表(比如源文件新增了一個函數,並在保存後,可在taglist窗口按u)
s:選擇排序字段
d:刪除光標所在的taglist文件(如用vi打開了兩個文件f1.c,f2.c可以刪除f1.c的標記)
x:放大/縮小taglist窗口
+:展開(指標記)
-:摺疊
*:全部展開
=:全部摺疊
[[:將光標移到前一個文件的起點
]]:將光標移到後一個文件的起點
q:退出taglist窗口
F1:關閉幫助
注意:運行vim的時候,必須在"tags"文件所在的目錄下運行。否則,運行vim的時候還要用":set tags="命令設定"tags"文件的路徑,這樣vim才能找到"tags"文件。