VIM使用系列:.vimrc配置文件和項目管理腳本

前一文寫到了使用VIM進行項目開發的準備工做,以後進入春節假期,文章也就停了兩個禮拜。今天開始正式上班,繼續寫VIM使用系列的文章,本文主要是貼出本人簡陋的.vimrc配置文件和項目管理腳本,以作參考,拋磚引玉。git

1、.vimrc配置文件

前面說道vim強大的個性定製依賴於它的配置文件和插件機制,而配置文件是保證插件高效使用的基礎,所以很是重要。vim的配置文件一般爲.vimrc,放置於用戶的$HOME主目錄下,它能夠很簡單,甚至沒有(使用默認),也能夠很複雜,上百行的設置,讓人眼花繚亂。下面先貼上本人的.vimrc文件:shell

<!-- lang: shell -->
set nu       "打開行號顯示
set ts=4     "設置tabstop,即tab鍵佔用的空格數,:help tabstop
set sw=4     "設置shiftwidth,即>>一次移動多少空格, :help shiftwidth
set cindent  "C語言智能縮進
set textwidth=80   "設置一行字符寬度
set comments=sl:/*,mb:**,elx:*   "自動補全註釋符號
filetype plugin indent  on    "開啓文件類型自動檢測
set completeopt=longest,menu  "設置單詞自動補全選項
set autoindent   "設置自動縮進
set encoding=utf-8 fileencodings=ucs-bom,utf-8,cp936  "設置編碼,正確的編碼才能夠顯示中文
set fdm=indent   "設置摺疊模式爲依據縮進自動摺疊,:help fdm
set hlsearch    "設置搜索時高亮顯示搜索字,:help hlsearch
set tags+=~/.vim/systags
" Always show the status line
set laststatus=2
" Status line format:
" {buffer number}: {file name, relative path to the current working directory}{modified flag}{readonly flag}
" {help flag}{preview flag} [file type, encoding, format] [current line-total lines, current column][position percentage in file]set statusline=%n:\ %f%m%r%h%w\ [%Y,%{&fileencoding},%{&fileformat}]\ [%l-%L,%v][%p%%]
" change commennt color to baby blue
hi Comment ctermfg=6
"=====================================================================
"taglist option,設置taglist插件的選項,進行定製
"=====================================================================
let Tlist_Show_One_File=1      "只顯示一個文件的tags
let Tlist_Exit_OnlyWindow=1    "當taglist窗口是最後一個窗口時,退出vim
let Tlist_Use_Right_Window=1   "taglist窗口顯示在右側
let mapleader = ","            "修改引導符爲",",默認爲"\",後面都使用修改後的值
noremap <silent> <F6> :TlistToggle<CR>      "至關於定義快捷鍵
noremap <silent> <Leader>tt :TlistToggle<CR>  "定義第二個快捷鍵
"==========================================================================
"BufExplore setting,設置bufexplorer插件的選項,進行定製
"==========================================================================
let g:BufExplorerShowRelativePath=1
let g:BufExplorerSplitRight=0
let g:BufExplorerSplitVertical=1
let g:BufExplorerSplitBelow=0
noremap <silent> <Leader>be :BufExplorer<CR>
noremap <silent> <Leader>bs :BufExplorerHorizontalSplit<CR>
noremap <silent> <Leader>bv :BufExplorerVerticalSplit<CR>

"==============================================================================
"csope settings,設置cscope的參數內容,實現啓動自動添加數據庫
"==============================================================================
if has("cscope")
	set csprg=/usr/local/bin/cscope
	set csto=0
	set cst
	set nocsverb
	" add any database in current directory
	if filereadable("cscope.out")
		cs add cscope.out
	" else add database pointed to by environment
	elseif $CSCOPE_DB != ""
		cs add $CSCOPE_DB
	endif
	set csverb
endif
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>

"=========================================
"NERDTree settings
"=========================================
noremap  <Leader>nt :NERDTree


"===================================================================
" lookupfile setting
"===================================================================
 let g:LookupFile_MinPatLength = 2               "最少輸入2個字符纔開始查找
 let g:LookupFile_PreserveLastPattern = 0        "不保存上次查找的字符串
 let g:LookupFile_PreservePatternHistory = 1     "保存查找歷史
 let g:LookupFile_AlwaysAcceptFirst = 1          "回車打開第一個匹配項目
 let g:LookupFile_AllowNewFiles = 0              "不容許建立不存在的文件
 if filereadable("./lookup.files")                "設置tag文件的名字
 	let g:LookupFile_TagExpr = '"./lookup.files"'
 endif

 nmap <silent> <Leader>lf <Plug>LookupFile<CR>
 nnoremap <silent> <Leader>lb :LUBufs<CR>
 nnoremap <silent> <Leader>lw :LUWalk<CR>
 nnoremap <silent> <Leader>lt :LUTags<CR>

"======================================
"quickfix setting
"======================================
noremap <silent> <Leader>cn :cn<CR>
noremap <silent> <Leader>cp :cp<CR>
noremap <silent> <Leader>cw :cw<CR>
noremap <silent> <Leader>cc :cc<CR>
noremap <silent> <Leader>co :copen<CR>
noremap <silent> <Leader>ce :cclose<CR>

"======================================
"project manager
"======================================
noremap <silent> <Leader>pj :!~/opt/scripts/project<CR>
if filereadable("./cscope.files")
    "silent exec '!ls'
    "exec '!project'
    silent exec '!~/opt/scripts/project'
endif

此配置文件算有點複雜了,75行,還好沒到百行。其實配置文件中主要的內容是插件的選項設置和快捷鍵的定義(命令的重映射),這些內容正是體現個性定製的根本,把他們設置修改爲你喜歡、熟悉,使用效率高的內容。數據庫

vim自身的選項參數有不少,多到讓我以爲也許只有做者才知道全部的選項~,可是咱們只需取所需的就行了,並且全部的選項均可以經過在線幫助去理解使用,正如上面註釋中寫到的:help fdm。而插件的選項、參數、映射,若是你已經作好了前文的準備工做,那麼也能夠直接經過:help命令去查找和了解其詳細的內容。若是你們徹底參照前文的步驟進行使用,那麼此配置文件能夠徹底複製使用。vim

2、項目管理腳本

前文提到cscope工具須要經過索引源文件來創建tag的數據庫,ctags工具也須要進行源文件的索引關係,還有lookupfile插件須要進行項目中全部文件的查找,全部這些都須要咱們提取項目的文件列表,以做爲工具、插件的輸入源,來獲得須要的輸出內容。bash

還有一個重要的點就是,在進行項目開發時,文件的數量和內容確定是不斷增長和修改的,這也就意味着須要不斷地進行相關數據、tags的同步更新,以保證能索引到添加的符號和文件,能進行正確的跳轉等。這些工做經過編寫一個簡單的腳本顯然能夠更好的勝任,下面是個人腳本project.sh:svn

<!-- lang: shell -->
#! /bin/bash

cscope_file="cscope.files"     #cscope的輸入源,文件列表,名字可自定義
lookup_file="lookup.files"     #lookupfile插件的索引文件列表,名字可自定義,但在.vimrc中用到了,須要保持一致,見上面內容

#C語言項目的源文件列表
find . -name \*.c -o -name \*.h > $cscope_file
#cscope經過文件列表創建數據庫
cscope -bRq -i $cscope_file 2&>/dev/null

#ctags工具遞歸創建整個項目的tags文件,默認文件名爲tags
ctags -R * 2&>/dev/null

#創建lookupfile插件的文件索引列表,如下內容能夠在幫助文檔中找到,:help lookupfile-tags
#(echo "!_TAG_FILE_SORTED       2       /2=foldcase/";(find . -type f -printf "%f\t%p\t1\n" | \
#         sort -f)) > ./filenametags
echo "!_TAG_FILE_SORTED 2       /2=foldcase/" > $lookup_file
find . \( -name .git -o -name .svn -o -path ./classes \) -prune -o -not -iregex '.*\.\(jar\|gif\|jpg\|class\|exe\|dll\|pdd\|sw[op]\|xls\|doc\|pdf\|zip\|tar\|ico\|ear\|war\|dat\).*' -type f -printf "%f\t%p\t1\n"| sort -f >> $lookup_file

個人項目只用到了C語言進行開發,所以源文件會相對簡單,若是項目使用別的開發語言,須要對腳本進行相應的修改。在每次開發過程結束後或中間,手動運行腳原本保證數據庫、tags文件的一致,從而保證開發工做高效正確的進行。工具

相關文章
相關標籤/搜索