VIM使用系列之一——配置VIM下C/C++編程環境

做者:gnuhpc from http://blog.csdn.net/gnuhpc
本文環境:ubuntu 9.10

1.基本配置
創建三個目錄:mkdir ~/.vim/{plugin,doc,syntax} –p
創建配置文件:touch ~/.vimrc

vimrc做爲vim的主要配置文件,咱們在配置VIM時首先對它進行基本的設置。打開你home目錄下的.vimrc文件。進行以下配置:php

"基本配置
set wildmenu
"加強模式中的命令行自動完成操做
set foldmethod=manual
"設定摺疊方式爲手動
set helplang=cn
"設置幫助的語言爲中文
set cin    
"實現C程序的縮進
set sw=4   
"設計(自動) 縮進使用4個空格
set sta    
"插入時使用'shiftwidth'
set backspace=2
"指明在插入模式下可使用刪除光標前面的字符
syntax enable
"設置高亮關鍵字顯示
set nocompatible
"去掉討厭的有關vi一致性模式,避免之前版本的一些bug和侷限
set number
"顯示行號
filetype on
"檢測文件的類型
map :q
set history=1000
""記錄歷史的行數
set background=dark
"背景使用黑色
syntax on
"語法高亮度顯示
set autoindent
set smartindent
"上面兩行在進行編寫代碼時,在格式對起上頗有用;
"第一行,vim使用自動對起,也就是把當前行的對起格式應用到下一行;
"第二行,依據上面的對起格式,智能的選擇對起方式,對於相似C語言編寫上頗有用
"第一行設置tab鍵爲4個空格,第二行設置當行之間交錯時使用4個空格
set tabstop=4
set shiftwidth=4
set showmatch
"設置匹配模式,相似當輸入一個左括號時會匹配相應的那個右括號
set ruler
"在編輯過程當中,在右下角顯示光標位置的狀態行
set incsearch
"查詢時很是方便,如要查找book單詞,當輸入到/b時,會自動找到第一
"個b開頭的單詞,當輸入到/bo時,會自動找到第一個bo開頭的單詞,依
"次類推,進行查找時,使用此設置會快速找到答案,當你找要匹配的單詞
"時,別忘記回車。
 set enc=chinese
"設置編碼爲中文
set winaltkeys=no
"Alt組合鍵不映射到菜單上

保存文件,退出。

2.安裝中文幫助手冊
下載地址:http://vimcdoc.sourceforge.net/
安裝:
$tar xvf vimcdoc-1.7.0.tar.gz

$ cd vimcdoc-1.7.0/

$ ./vimcdoc.sh –i

而後在~/.vimrc 加上set helplang=cn 這句話代表幫助文檔爲中文。

3.安裝C-support--加速C/Cpp開發
下載地址:http://www.vim.org/scripts/script.php?script_id=213

安裝:
將下載的文件放到~/.vim目錄中html

$ cd ~/.vim





$ unzip cvim.zip

在.vimrc中添加 filetype plugin on 使能該插件。
使用方法:
1)自動添加文件頭:
添加你本身的信息: vim ~/.vim/c-support/templates/Templates
~/.vim/c-support/templates 這個目錄下還有許多模板,你能夠本身定製。
如今試着vim test.c看一看效果。
2)使用熱鍵
主要是一些模板的插入,種類幾乎覆蓋C/C++開發中大多數須要手動輸入的東西。不過我不是很喜歡它的默認模板的代碼風格,不過能夠經過上述的目錄中的文件去定製。這個功能在後邊的code complete插件中也提供了。
另一部分是編譯運行的快捷鍵。
還有一個把另外一個文件內容直接灌入當前文件的功能。
有關使用熱鍵的方法見一個文檔:http://lug.fh-swf.de/vim/vim-c/c-hotkeys.pdf
3)編譯文件
快捷鍵:
修改c.vim文件,查找f9,將包含link的那一行註釋掉,而且對save and compile 那一行從A-F9修改成F9
       F9     編譯
  Ctrl-F9   執行
 Shift-F9   設置命令行參數

4.安裝Exuberant Ctags + Tag list + WinManager——分析源碼中Tags(function, class, variable, etc) 而且列出—— 在VIM中自由的查看源代碼
Exuberant Ctags 下載安裝:sudo apt-get install exuberant-ctags
Teh List下載地址:http://www.vim.org/scripts/download_script.php?src_id=7701
安裝:解壓到home目錄下的.vim子目錄中。
WinManager(調整窗口布局)下載地址:http://www.vim.org/scripts/script.php?script_id=95
安裝:解壓到home目錄下的.vim子目錄中。
配置:
進入$HOME/.vim/doc打開 Vim 運行 ":helptags ." 這個是要保證taglist幫助能夠正常使用。其實平時也不太會用到。
生成系統API的tags:(具體爲何採用這樣的參數請參考ctags命令行幫助)
ctags -R -f ~/.vim/systags --c-kinds=+p -- c++ - kinds= + px -- fields= + iaS -- extra= + q /usr/include /usr/local/include
隨後在vimrc中添加以下幾行:java

"禁止自動改變當前Vim窗口的大小
let Tlist_Inc_Winwidth=0
"把方法列表放在屏幕的右側
let Tlist_Use_Right_Window=1
"讓當前不被編輯的文件的方法列表自動摺疊起來, 這樣能夠節約一些屏幕空間
let Tlist_File_Fold_Auto_Close=1
"映射F4爲功能鍵調出tages瀏覽器
"映射F3爲功能鍵調出winmanager的文件瀏覽器
"let g:winManagerWindowLayout='FileExplorer|TagList' "你要是喜歡這種佈局能夠註釋掉這一行
map :TlistToggle
map :WMToggle
"將當前的工程的tags導入
set tags=./tags
"將系統已經生成的tags導入
set tags+=~/.vim/systags

使用方法:按F4 調出函數、變量等tags,按F3出現文件管理器。 例如:把光標移動到某個元素上,CTRL+],就會跳轉到對應的定義

5.安裝cscope——升級的Ctags
下載 安裝:sudo apt-get install cscope
在vimrc中添加c++

map :call Do_CsTag()//之後按F12就能夠對一個工程進行tags索引創建而且引入了
nmap s :cs find s =expand("")   
nmap g :cs find g =expand("")   
nmap c :cs find c =expand("")   
nmap t :cs find t =expand("")   
nmap e :cs find e =expand("")   
nmap f :cs find f =expand("")   
nmap i :cs find i ^=expand("")$
nmap d :cs find d =expand("")
function Do_CsTag()
    if(executable('cscope') && has("cscope") )
        if(g:iswindows!=1)
            silent! execute "!find . -name '*.h' -o -name '*.c' -o -name '*.cpp' -o -name '*.java' -o -name '*.cs' -o -name '*.cxx' -o -name '*.hxx'> cscope.files"
        else
            silent! execute "!dir /b *.c,*.cpp,*.h,*.java,*.cs >> cscope.files"
        endif
        silent! execute "!cscope -bq"
        if filereadable("cscope.out")
            execute "cs add cscope.out"
        endif
    endif
endf


使用:F12生成這個目錄及其子目錄的索引,而後將光標指向一些函數名或者變量名,使用Crtl+/+如下字母
shell

  • s: 查找C語言符號,即查找函數名、宏、枚舉值等出現的地方
  • g: 查找函數、宏、枚舉等定義的位置,相似ctags所提供的功能
  • d: 查找本函數調用的函數
  • c: 查找調用本函數的函數
  • t: 查找指定的字符串
  • e: 查找egrep模式,至關於egrep功能,但查找速度快多了
  • f: 查找並打開文件,相似vim的find功能
  • i: 查找包含本文件的文件


6.安裝A——同名頭文件和源文件切換
下載:http://www.vim.org/scripts/script.php?script_id=3 1

將a.vim 放到 ~/.vim/plugin 文件夾中
使用: 在源文件或者頭文件中輸入:A就能夠切換了。

7.安裝code_complete——函數、模板自動補全
下載地址:http://www.vim.org/scripts/script.php?script_id=1764
把插件文件中的tab都改成C-b防止與後來咱們要安裝的supertab衝突。
將下邊這一段加入到template段中以添加bash模板補全功能ubuntu

-----------------------------------------------
"Bash Templates
"Bash Structure by warmbupt@gmail.com
let g:template['sh'] = {}
let g:template['sh']['sif'] = "if ".g:rs."...".g:re." ; then/".g:rs."...".g:re."/fi"
let g:template['sh']['sife'] = "if ".g:rs."...".g:re." ; then/".g:rs."...".g:re."/else/".g:rs."...".
            /g:re."/fi"
let g:template['sh']['scase'] = "case ".g:rs."...".g:re." in/".g:rs."...".g:re.")//" .g:rs."...".g:re.";;//".
            /g:rs."...".g:re.")//" .g:rs."...".g:re.";;///esac/"
let g:template['sh']['swhile'] = "while ".g:rs."...".g:re." /do/".g:rs."...".g:re."/done"
let g:template['sh']['suntil'] = "until ".g:rs."...".g:re." /do/".g:rs."...".g:re."/done"
let g:template['sh']['sfor'] = "for ".g:rs."...".g:re." in " .g:rs."...".g:re."/do/".g:rs."...".g:re."/done"
let g:template['sh']['sselect'] = "select ".g:rs."...".g:re." in " .g:rs."...".g:re."/do/".g:rs."...".g:re."/done"

使用:在輸入函數時能夠出現原型提示是安裝這個插件的主要目的。Crtl+b是萬能的,好比輸入pthread_cre,而後Crtl+b就能夠看到效果了。

8.安裝supertab+snipMate+echofunc——超級補全
下載地址:
http://www.vim.org/scripts/script.php?script_id=1643
http://www.vim.org/scripts/script.php?script_id=2540
http://www.vim.org/scripts/script.php?script_id=1735
安裝:用vim打開supertab.vba ,而後使用:so % 進行安裝。後二者直接解壓在.vim目錄裏便可。
使用:使用tab就能夠完成一些文件中已經出現的詞彙的補全和常見結構的補全了,這個功能與code_complete的結構快速補充有所重疊,不過是使 用tab鍵,你還能夠在.vim/snippets目錄裏本身定製。對於函數原型提示和補全,你能夠試着輸入select(就能夠看到了下邊的原型提示 了),shift+tab轉換對應原型,而且在結合code_complete的Ctrl+b還能夠進行直接補全。

9.安裝astyle——代碼格式整理
下載安裝:$sudo apt-get install astyle

在vimrc中寫入:小程序

"定義源代碼格式化
map :call FormartSrc()
"定義FormartSrc()
func FormartSrc()
exec "w"
"C程序,Perl程序,Python程序
if &filetype == 'c'
exec "!astyle --style=gnu --suffix=none %"
exec "e! %"
elseif &filetype == 'cpp'
exec "!astyle --style=gnu --suffix=none %"
exec "e! %"
elseif &filetype == 'perl'
exec "!astyle --style=gnu --suffix=none %"
exec "e! %"
elseif &filetype == 'py'
exec "!astyle --style=gnu --suffix=none %"
exec "e! %"
"Java程序
elseif &filetype == 'java'
exec "!astyle --style=java --suffix=none %"
exec "e! %"
elseif &filetype == 'jsp'
exec "!astyle --style=gnu --suffix=none %"
exec "e! %"
elseif &filetype == 'xml'
exec "!astyle --style=gnu --suffix=none %"
exec "e! %"
elseif &filetype == 'html'
exec "!astyle --style=gnu --suffix=none %"
exec "e! %"
elseif &filetype == 'htm'
exec "!astyle --style=gnu --suffix=none %"
exec "e! %"
endif
endfunc
"結束定義FormartSrc

使用:F11直接格式化源代碼

10.安裝NERD_tree——多tab支持
下載地址:http://www.vim.org/scripts/script.php?script_id=1658
安裝:解壓到.vim目錄便可
"設置tab的快捷鍵
"Shift+t爲新建一個標籤,按兩次Shirt+t爲跳轉標籤
map :tabnew .
map :tabnext
使用:Shift+t就能夠新建一個標籤,連按Shfit+t兩次就能夠切換標籤。

11.生成STL庫tags——
下載地址:http://www.vim.org/scripts/script.php?script_id=2358
解壓後運行ctags -R --c++-kinds=+p --fields=+iaS --extra=+q --language-force=C++ cpp_src

將其移動到.vim目錄:mv tags ../.vim/stltags
而後在vimrc中添加:set tags+=~/.vim/stltags
另外還須要omnicppcomplete  下載地址:http://www.vim.org/scripts/script.php?script_id=1520
這樣就ok了。

12.定義一些經常使用的功能:vim

"設置創建新行可是不插入
map o
"設置全選快捷鍵
map ggVG
"設置快速不保存退出快捷鍵
map :q!:q!:q!

"替換
map :L1,L2s/src/tar/g
imap

"設置快速註釋鍵
"F10是在該行後邊添加註釋,Shift+F10爲註釋掉該行,兩次Shift+F10是去掉改行的註釋,而後將光標置於下一行
map I//
map! I//
map 02x
map! 02x
map $a//
map! $a//

"設置編譯快捷鍵
"F5爲make,兩次F5爲make clean
"F6爲quickfix,查看詳細信息, 兩次F6關閉詳細信息
"F7爲下一個結果,兩次F7爲上一個結果
"F8爲編譯和運行單個程序、兩次F8爲調試
map :make
map :make clean
map :cw
map :ccl
map :cn
map :cp
map :call CompileRun()
map :call Debug()


"定義CompileRun函數,用來對不用外接庫的小程序進行編譯和運行,本身能夠根據文件名擴展或實際狀況修改參數
func CompileRun()
exec "w"
"C程序
if &filetype == 'c'
exec "!gcc % -g -o %<"
exec "!.//%<"
endif
endfunc
"結束定義CompileRun

"定義Debug函數,用來調試小程序
func Debug()
exec "w"
"C程序
if &filetype == 'c'
exec "!rm %<"
exec "!gcc % -g -o %<"
exec "!gdb %<"
elseif &filetype == 'cpp'
exec "!rm %<"
exec "!g++ % -g -o %<"
exec "!gdb %<"
"Java程序
exec "!rm %<.class"
elseif &filetype == 'java'
exec "!javac %"
exec "!jdb %<"
endif
endfunc
"定義dubug結束

autocmd BufNewFile *.cc,*.sh,*.java exec ":call SetTitle()"
"新建.cc,.java,.sh,
"定義函數SetTitle,自動插入文件頭
func SetTitle()
"若是文件類型爲.sh文件
if &filetype == 'sh'
call setline(1, "/#!/bin/bash")
call append(line("."), "/#########################################################################")
call append(line(".")+1, "/# Author: gnuhpc(http://blog.csdn.net/gnuhpc)")
call append(line(".")+2, "/# Created Time: ".strftime("%c"))
call append(line(".")+3, "/# File Name: ".expand("%"))
call append(line(".")+4, "/# Description: ")
call append(line(".")+5, "/#########################################################################")
call append(line(".")+6, "")
else
call setline(1, "/*************************************************************************")
call append(line("."), " Author: gnuhpc(http://blog.csdn.net/gnuhpc)")
call append(line(".")+1, " Created Time: ".strftime("%c"))
call append(line(".")+2, " File Name: ".expand("%"))
call append(line(".")+3, " Description: ")
call append(line(".")+4, " ************************************************************************/")
call append(line(".")+5, "")
endif
endfunc

"自動將shell腳本設置爲可執行權限
au BufWritePost * if getline(1) =~ "^#!" | if getline(1) =~ "/bin/" | silent !chmod a+x | endif | endif
"保存上次打開的位置
if has("autocmd")



   autocmd BufRead *.txt set tw=78



   autocmd BufReadPost *



      / if line("'/"") > 0 && line ("'/"") <= line("$") |



      /   exe "normal g'/"" |



      / endif



endif

總結一下如今可使用的快捷鍵:windows

F3                   文件瀏覽器
F4                   Tags瀏覽器
F5                   make
2*F5               make clean
F6                   quickfix查看詳細信息
2*F6               關閉詳細信息
F7                   下一個結果
2*F7               上一個結果
F8                   編譯和運行單個程序
2*F8               調試單個程序
F9                   編譯
Ctrl+F9            執行
Shift+F9          設置命令行參數               
F12                 在該行後邊添加註釋
Shift+F12        註釋掉該行
2*(Shift+F12) 去掉改行的註釋
F11                 格式化代碼
F10                 創建目錄及子目錄下的索引Tags
Shift+t            新建標籤
2*(Shift+t)     標籤間跳轉
Shift+o           創建新行可是不插入
Ctrl+a             全選
2*(Shift+q)     快速不保存
Ctrl+h             替換



Tags相關:Ctrl+/+字母瀏覽器

    * s: 查找C語言符號,即查找函數名、宏、枚舉值等出現的地方
    * g: 查找函數、宏、枚舉等定義的位置,相似ctags所提供的功能
    * d: 查找本函數調用的函數
    * c: 查找調用本函數的函數
    * t: 查找指定的字符串
    * e: 查找egrep模式,至關於egrep功能,但查找速度快多了
    * f: 查找並打開文件,相似vim的find功能
    * i: 查找包含本文件的文件

TagList操做:

          跳到光標下tag所定義的位置,用鼠標雙擊此tag功能也同樣
o             在一個新打開的窗口中顯示光標下tag
       顯示光標下tag的原型定義
u             更新taglist窗口中的tag
s             更改排序方式,在按名字排序和按出現順序排序間切換
x             taglist窗口放大和縮小,方便查看較長的tag
+             打開一個摺疊,同zo
-             將tag摺疊起來,同zc
*             打開全部的摺疊,同zR
=             將全部tag摺疊起來,同zM
[[            跳到前一個文件
]]            跳到後一個文件
q             關閉taglist窗口
          顯示幫助
在瀏覽代碼時Taglist經常使用操做爲:
gd 轉到當前光標所指的局部變量的定義
* 轉到當前光標所指的單詞下一次出現的地方
# 轉到當前光標所指的單詞上一次出現的地方
ctrl+] 轉到函數定義的地方
ctrl+o 回退到函數調用的地方

Winmanager文件操做技巧:


顯示幫助

若是光標下是目錄, 則進入該目錄; 若是光標下文件, 則打開該文件
-
返回上級目錄
c
切換vim 當前工做目錄正在瀏覽的目錄
d
建立目錄
D
刪除目錄或文件
i
切換顯示方式
R
文件或目錄重命名
s
選擇排序方式
x
定製瀏覽方式, 使用你指定的程序打開該文件


C-Support常見補全結構與快捷鍵

-- 經常使用結構 ---------------------------------------------------------
/sd do { } while
/sf for
/sfo for { }
/sif if { }
/sife if { } else { }
/swh while { } (n,v,i)
/ss switch (n,v,i)
/sc case (n,i)
/s{ { } (n,v,i)

-- 預處理命令 -------------------------------------------------------
/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)

-- 經常使用短語 -------------------------------------------------------------
/im main() (n,v,i)
/i0 for( x=0; x
/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)

-- C++ ----------------------------------------------------------------
/+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)


Code Complete經常使用結構:Ctrl +b補全

xt 插入當前時間

        C/C++語言:

cc /*  */
de #define
in #include    ""
is #include    <>
ff #ifndef 
ife  if...els

       BASH:

sif               if
sife             if...els
scase          case
swhile         while
suntil          until
sfor             for
sselect        select

參考文獻:

http://www.vimer.cn/
http://qixinglu.com/archives/vim_key_mapping
http://easwy.com/blog/archives/advanced-vim-skills-cscope/
http://blog.csdn.net/wooin/archive/2007/10/31/1858917.aspx

附上述vimrc配置文件:

"基本配置
set wildmenu
"加強模式中的命令行自動完成操做
set foldmethod=manual
"設定摺疊方式爲手動
set helplang=cn
"設置幫助的語言爲中文
set cin   
"實現C程序的縮進
set sw=4  
"設計(自動) 縮進使用4個空格
set sta   
"插入<tab>時使用'shiftwidth'
set backspace=2
"指明在插入模式下可使用<BS>刪除光標前面的字符
syntax enable
"設置高亮關鍵字顯示
set nocompatible
"去掉討厭的有關vi一致性模式,避免之前版本的一些bug和侷限
set number
"顯示行號
filetype on
"檢測文件的類型
map <fe> :q
set history=1000
""記錄歷史的行數
set background=dark
"背景使用黑色
syntax on
"語法高亮度顯示
set autoindent
set smartindent
"上面兩行在進行編寫代碼時,在格式對起上頗有用;
"第一行,vim使用自動對起,也就是把當前行的對起格式應用到下一行;
"第二行,依據上面的對起格式,智能的選擇對起方式,對於相似C語言編寫上頗有用
"第一行設置tab鍵爲4個空格,第二行設置當行之間交錯時使用4個空格
set tabstop=4
set shiftwidth=4
set showmatch
"設置匹配模式,相似當輸入一個左括號時會匹配相應的那個右括號
set ruler
"在編輯過程當中,在右下角顯示光標位置的狀態行
set incsearch
"查詢時很是方便,如要查找book單詞,當輸入到/b時,會自動找到第一
"個b開頭的單詞,當輸入到/bo時,會自動找到第一個bo開頭的單詞,依
"次類推,進行查找時,使用此設置會快速找到答案,當你找要匹配的單詞
"時,別忘記回車。
 set enc=chinese
"設置編碼爲中文
set winaltkeys=no
"Alt組合鍵不映射到菜單上

set helplang=cn

filetype plugin on

"禁止自動改變當前Vim窗口的大小
let Tlist_Inc_Winwidth=0
"把方法列表放在屏幕的右側
let Tlist_Use_Right_Window=1
"讓當前不被編輯的文件的方法列表自動摺疊起來, 這樣能夠節約一些屏幕空間
let Tlist_File_Fold_Auto_Close=1
"映射F4爲功能鍵調出tages瀏覽器
"映射F3爲功能鍵調出winmanager的文件瀏覽器
"let g:winManagerWindowLayout='FileExplorer|TagList' "你要是喜歡這種佈局能夠註釋掉這一行
map <F4> :TlistToggle<cr>
map <F3> :WMToggle<cr>
"將當前的工程的tags導入
set tags=./tags
"將系統已經生成的tags導入
set tags+=~/.vim/systags

    map <F10> :call Do_CsTag()<CR>//之後按F10就能夠對一個工程進行tags索引創建而且引入了
    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>
    function Do_CsTag()
        if(executable('cscope') && has("cscope") )
            if(g:iswindows!=1)
                silent! execute "!find . -name '*.h' -o -name '*.c' -o -name '*.cpp' -o -name '*.java' -o -name '*.cs' -o -name '*.cxx' -o -name '*.hxx'> cscope.files"
            else
                silent! execute "!dir /b *.c,*.cpp,*.h,*.java,*.cs >> cscope.files"
            endif
            silent! execute "!cscope -bq"
            if filereadable("cscope.out")
                execute "cs add cscope.out"
            endif
        endif
    endf

"定義源代碼格式化
map <F11>:call FormartSrc()<CR>
"定義FormartSrc()
func FormartSrc()
exec "w"
"C程序,Perl程序,Python程序
if &filetype == 'c'
exec "!astyle --style=gnu --suffix=none %"
exec "e! %"
elseif &filetype == 'cpp'
exec "!astyle --style=gnu --suffix=none %"
exec "e! %"
elseif &filetype == 'perl'
exec "!astyle --style=gnu --suffix=none %"
exec "e! %"
elseif &filetype == 'py'
exec "!astyle --style=gnu --suffix=none %"
exec "e! %"
"Java程序
elseif &filetype == 'java'
exec "!astyle --style=java --suffix=none %"
exec "e! %"
elseif &filetype == 'jsp'
exec "!astyle --style=gnu --suffix=none %"
exec "e! %"
elseif &filetype == 'xml'
exec "!astyle --style=gnu --suffix=none %"
exec "e! %"
elseif &filetype == 'html'
exec "!astyle --style=gnu --suffix=none %"
exec "e! %"
elseif &filetype == 'htm'
exec "!astyle --style=gnu --suffix=none %"
exec "e! %"
endif
endfunc
"結束定義FormartSrc

"設置tab的快捷鍵
"Shift+t爲新建一個標籤,按兩次Shirt+t爲跳轉標籤
map <S-t> :tabnew .<CR>
map <S-t><S-t> <ESC>:tabnext<CR>

"設置創建新行可是不插入
map <S-o> o<ESC><CR>
"設置全選快捷鍵
map <C-a> ggVG
"設置快速不保存退出快捷鍵
map <S-q><S-q> :q!<CR>:q!<CR>:q!<CR>

"替換
map <C-H> :L1,L2s/src/tar/g
imap <C-H> <Esc><C-H>

"設置快速註釋鍵
"F12是在該行後邊添加註釋,Shift+F12爲註釋掉該行,兩次Shift+F12是去掉改行的註釋,而後將光標置於下一行
map <S-F12> <ESC>I//<ESC><CR>
map! <S-F12> <ESC>I//<ESC><CR>
map <S-F12><S-F12> 02x<ESC><CR>
map! <S-F12><S-F12> <ESC>02x<ESC><CR>
map <F12> <ESC>$a//
map! <F12> <ESC>$a//

"設置編譯快捷鍵
"F5爲make,兩次F5爲make clean
"F6爲quickfix,查看詳細信息, 兩次F6關閉詳細信息
"F7爲下一個結果,兩次F7爲上一個結果
"F8爲編譯和運行單個程序、兩次F8爲調試
map <F5> :make<CR>
map <F5><F5> :make clean<CR>
map <F6> :cw<CR>
map <F6><F6> :ccl<CR>
map <F7> :cn<CR>
map <F7><F7> :cp<CR>
map <F8> :call CompileRun()<CR>
map <F8><F8> :call Debug()<CR>


"定義CompileRun函數,用來對不用外接庫的小程序進行編譯和運行,本身能夠根據文件名擴展或實際狀況修改參數
func CompileRun()
exec "w"
"C程序
if &filetype == 'c'
exec "!gcc % -g -o %<"
exec "!.//%<"
endif
endfunc
"結束定義CompileRun

"定義Debug函數,用來調試小程序
func Debug()
exec "w"
"C程序
if &filetype == 'c'
exec "!rm %<"
exec "!gcc % -g -o %<"
exec "!gdb %<"
elseif &filetype == 'cpp'
exec "!rm %<"
exec "!g++ % -g -o %<"
exec "!gdb %<"
"Java程序
exec "!rm %<.class"
elseif &filetype == 'java'
exec "!javac %"
exec "!jdb %<"
endif
endfunc
"定義dubug結束

autocmd BufNewFile *.cc,*.sh,*.java exec ":call SetTitle()"
"新建.cc,.java,.sh,
"定義函數SetTitle,自動插入文件頭
func SetTitle()
"若是文件類型爲.sh文件
if &filetype == 'sh'
call setline(1, "/#!/bin/bash")
call append(line("."), "/#########################################################################")
call append(line(".")+1, "/# Author: gnuhpc(http://blog.csdn.net/gnuhpc)")
call append(line(".")+2, "/# Created Time: ".strftime("%c"))
call append(line(".")+3, "/# File Name: ".expand("%"))
call append(line(".")+4, "/# Description: ")
call append(line(".")+5, "/#########################################################################")
call append(line(".")+6, "")
else
call setline(1, "/*************************************************************************")
call append(line("."), " Author: gnuhpc(http://blog.csdn.net/gnuhpc)")
call append(line(".")+1, " Created Time: ".strftime("%c"))
call append(line(".")+2, " File Name: ".expand("%"))
call append(line(".")+3, " Description: ")
call append(line(".")+4, " ************************************************************************/")
call append(line(".")+5, "")
endif
endfunc

"自動將shell腳本設置爲可執行權限
au BufWritePost * if getline(1) =~ "^#!" | if getline(1) =~ "/bin/" | silent !chmod a+x <afile> | endif | endif
"保存上次打開的位置
if has("autocmd")



   autocmd BufRead *.txt set tw=78



   autocmd BufReadPost *



      / if line("'/"") > 0 && line ("'/"") <= line("$") |



      /   exe "normal g'/"" |



      / endif



endif
相關文章
相關標籤/搜索