分享一下我的vim配置

    這是我長時間使用vim總結的一些配置,分享給有需要的人。可以支持編譯一鍵編譯並執行C、C++和Java(需要安裝編譯器並配置環境變量),具體的使用方法,在下面的配置文件中講的很詳細。另外,在Linux上默認使用的clang的編譯器,這種編譯器可以兼容gcc的大部分選項,並且編譯速度更快,提示更加友好,您也可以根據自己的需要改成自己喜歡的編譯器。

    我是在Windows7下測試的,所以Windows7系統直接可用。其中的插件與配置文件在 Linux 下同樣適用。如果您是XP的用戶,可能會出現Consolas字體找不到的情況,可以從Windows7系統拷貝,或者到去下載。對於其他系統(UNIX,MAC)的用戶直接將下載到的文件解壓,把插件目錄和配置文件覆蓋掉原來的文件即可。在Linux下,需要把插件目錄重新命名爲 .vim,配置文件重新命名爲 .vimrc。建議您全新安裝vim,僅僅拷貝配置文件和插件目錄。

    使用vim已經有快4年的時間了,從最初的排斥到現在的愛不釋手,這段心路歷程非常奇特。vim是一個非常奇特、精緻的編輯器,是一個可以最大限度發揮人的智慧的編輯器。通過使用恰當的插件,完全可以秒殺其他昂貴而又笨重的IDE。然而最重要的是,它是免費的。這種免費充滿了理想主義色彩,更難能可貴的是它有非常實用,這或許就是理想與現實的結合吧。

    分享一下我使用vim的心得:對於一個初學者,不要過於深入的去鑽研的vim配置,而最重要的就是要把vim用起來。所以,一開始,儘可以去網上找一些別人的配置文件爲己所用,不要去深究這些配置具體的原理。我最一開始就是這樣做的。作爲一個軟件,最大的價值就是可以提高人的效率,而如果在使用的同時還充滿了快樂,那不是更好了嗎?  

下載連接:http://pan.baidu.com/s/1kT9WTvT

 

" -----------------     Author: Light
" -----------------     Email: [email protected]
" -----------------     Date: 2015-05-04 00:02:05
"********************************encoding**************************************
"默認爲 UTF-8 編碼
set encoding=utf-8
set fileencoding=utf-8
set fileencodings=ucs-bom,utf-8,cp936,big5,euc-jp,euc-kr,latin1
set fileencoding=utf-8 
let &termencoding=&encoding

"********************************ui**************************************
"字體/配色
"以下爲解決中文顯示問題,以及相應帶來的提示及菜單亂碼問題
set encoding=utf-8 " 設置vim內部使用的字符編碼,原來是cp936
lang messages zh_CN.UTF-8 " 解決consle輸出亂碼 
"解決菜單亂碼 
source $VIMRUNTIME/delmenu.vim 
source $VIMRUNTIME/menu.vim

colorscheme molokai
set guifont=Source\ Code\ Pro:h11.5,Consolas:h16

"去掉工具條、菜單欄、滾動條
"set go=aAce              
"set guioptions-=m 
set shm+=I
set guioptions-=T

"高亮光標所在的行
set cursorline

" 自動最大化窗口
if has('gui_running')
    au GUIEnter * simalt ~x
    "給 Win32 下的 gVim 窗口設置透明度
    au GUIEnter * call libcallnr("vimtweak.dll", "SetAlpha", 237)
endif

"********************************language**************************************

"語言中文
language chinese
language messages zh_CN.utf-8



"********************************system**************************************
"保留Ctrl+A/c/v操作
"source $VIMRUNTIME/mswin.vim

"與windows共享剪貼板
set clipboard+=unnamed

"保留歷史記錄
set history=500

"導入刪除菜單腳本,刪除亂碼的菜單
source $VIMRUNTIME/delmenu.vim

"導入正常的菜單腳本
source $VIMRUNTIME/menu.vim 
 
set nocompatible

"Diff 模式的時候鼠標同步滾動 for Vim7.3
if has('cursorbind')
    set cursorbind
end

"********************************setting**************************************
" 行控制
set linebreak
set textwidth=80
set wrap

"帶如下字符不換行
set iskeyword+=_,$,@,%,#,-

"標籤頁
set tabpagemax=9
set showtabline=2

"控制檯響鈴
set noerrorbells
set novisualbell
set t_vb= "close visual bell
"行號和標尺
set number
set ruler
set rulerformat=%15(%c%V\ %p%%%)

"命令行於狀態行
set ch=1
set ls=2 " 始終顯示狀態行
set wildmenu "命令行補全以增強模式運行

"高亮、實時搜索
set hlsearch  
set magic     
set showmatch 
set mat=2    
set incsearch
set ignorecase

"製表符
set tabstop=4
set expandtab
set smarttab
set shiftwidth=4
set softtabstop=4

"狀態欄顯示目前所執行的指令
set showcmd 

"縮進
set autoindent
set smartindent

"自動重新讀入
set autoread

"插入模式下使用 <BS>、<Del> <C-W> <C-U>
set backspace=indent,eol,start

"設定在任何模式下鼠標都可用
set mouse=a

"自動改變當前目錄
set autochdir

"備份和緩存
set nobackup
set noswapfile

"自動完成
set complete=.,w,b,k,t,i
set completeopt=longest,menu

"定義 <Leader> 爲逗號
let mapleader = ","
let maplocalleader = ","

"保證語法高亮
syntax enable
syntax on

filetype plugin indent on


"********************************mapping**************************************

"標籤頁操作
nmap nt :tabnew<cr>:NERDTreeToggle<cr><c-w>w
nmap tg :tabprevious<cr>
nmap nc :tabclose<cr>

"去掉查找後的高亮
nmap nh :noh<cr>

"轉換文件類型
nmap 'h :set filetype=html<cr>
nmap 'j :set filetype=javascript<cr>
nmap 'c :set filetype=css<cr>

"在行尾添加;
nmap ; $A;<ESC>

"在行尾添加,
nmap <leader><leader> $A,<ESC>

"另存爲
nmap ss :browse confirm saveas<cr>

"切換窗口
nmap tt <c-w>w

"新窗口打開配置文件
nmap <leader>e :tabnew $VIM/_vimrc<cr>

" 按下 Q 不進入 Ex 模式,而是退出
nmap Q :x<cr>

"Tabularize插件對齊快捷鍵
nmap <Leader>te :Tabularize /=<CR>
nmap <Leader>tm :Tabularize /:<CR>
"排序註釋
nmap <Leader>tc :Tabularize /\/\/<CR>

"搜索
nmap <Leader>n :cnext<cr>
nmap <Leader>p :cprev<cr>
nmap <Leader>l :clist<cr>
nmap <Leader>w :cw<cr>

"********************************plugin**************************************

"插件快捷鍵
nmap ne :NERDTreeToggle<cr>

"NERDTree配置
let NERDTreeDirArrows=1 "目錄箭頭 1 顯示箭頭  0傳統+-|號

"neocomplcache配置
let g:neocomplcache_enable_at_startup = 1 
let g:neocomplcache_enable_camel_case_completion = 1 

"doxygen配置
let g:DoxygenToolkit_authorName="qinjia"
let g:DoxygenToolkit_briefTag_funcName="yes"
let g:doxygen_enhanced_color=1
let g:DoxygenToolkit_paramTag_pre="@Param " 
let g:DoxygenToolkit_returnTag="@Returns "

nmap <Leader>d :Dox<cr>
nmap <Leader>b :DoxAuthor<cr>
"********************************syntax**************************************

" JavaScript 語法高亮
au FileType html,javascript let g:javascript_enable_domhtmlcss = 1
au BufRead,BufNewFile *.js set syntax=jquery

" 給各語言文件添加 Dict
let s:dict_dir = $VIM.'\vimfiles\dict\'
let s:dict_dir = "setlocal dict+=".s:dict_dir
au FileType html exec s:dict_dir."html.dict"
au FileType css,html exec s:dict_dir."css.dict"
au FileType javascript,html exec s:dict_dir."javascript.dict"
au FileType javascript set dictionary+=$VIM.'\vimfiles\dict\node.dict'

" 將指定文件的換行符轉換成 dos 格式
au FileType php,javascript,html,xml,json,css,txt,vim,vimwiki set ff=dos


"********************************function**************************************

" 獲取當前目錄
"func! GetPWD()
    "return substitute(getcwd(), "", "", "g")
"endf

 
"------------------------------------------------------------------------------
"  < 判斷操作系統是否是 Windows 還是 Linux >
"------------------------------------------------------------------------------
if(has("win32") || has("win64") || has("win95") || has("win16"))
    let g:iswindows = 1
else
    let g:iswindows = 0
endif
 
"------------------------------------------------------------------------------
"  < 判斷是終端還是 Gvim >
"------------------------------------------------------------------------------
if has("gui_running")
    let g:isGUI = 1
else
    let g:isGUI = 0
endif
 
" -----------------------------------------------------------------------------
"  < 編譯、連接、運行配置 (目前只配置了C、C++、Java語言)>
" -----------------------------------------------------------------------------
" F9 一鍵保存、編譯、連接存並運行
nmap <F9> :call Run()<CR>
imap <F9> <ESC>:call Run()<CR>
 
" Ctrl + F9 一鍵保存並編譯
nmap <c-F9> :call Compile()<CR>
imap <c-F9> <ESC>:call Compile()<CR>
 
" Ctrl + F10 一鍵保存並連接
nmap <c-F10> :call Link()<CR>
imap <c-F10> <ESC>:call Link()<CR>
 
let s:LastShellReturn_C = 0
let s:LastShellReturn_L = 0
let s:ShowWarning = 1
let s:Obj_Extension = '.o'
let s:Exe_Extension = '.exe'
let s:Class_Extension = '.class'
let s:Sou_Error = 0
 
let s:windows_CFlags = 'gcc\ -fexec-charset=gbk\ -Wall\ -g\ -std=c11\ -O0\ -c\ %\ -o\ %<.o'
let s:linux_CFlags = 'clang\ -Wall\ -g\ -O0\ -std=c11\ -c\ %\ -o\ %<.o'
 
let s:windows_CPPFlags = 'g++\ -fexec-charset=gbk\ -Wall\ -g\ -O0\ -c\ %\ -o\ %<.o'
let s:linux_CPPFlags = 'clang++\ -Wall\ -g\ -O0\ -c\ %\ -o\ %<.o'
 
let s:JavaFlags = 'javac\ %'
 
func! Compile()
    exe ":ccl"
    exe ":update"
    let s:Sou_Error = 0
    let s:LastShellReturn_C = 0
    let Sou = expand("%:p")
    let v:statusmsg = ''
    if expand("%:e") == "c" || expand("%:e") == "cpp" || expand("%:e") == "cxx"
        let Obj = expand("%:p:r").s:Obj_Extension
        let Obj_Name = expand("%:p:t:r").s:Obj_Extension
        if !filereadable(Obj) || (filereadable(Obj) && (getftime(Obj) < getftime(Sou)))
            redraw!
            if expand("%:e") == "c"
                if g:iswindows
                    exe ":setlocal makeprg=".s:windows_CFlags
                else
                    exe ":setlocal makeprg=".s:linux_CFlags
                endif
                echohl WarningMsg | echo " compiling..."
                silent make
            elseif expand("%:e") == "cpp" || expand("%:e") == "cxx"
                if g:iswindows
                    exe ":setlocal makeprg=".s:windows_CPPFlags
                else
                    exe ":setlocal makeprg=".s:linux_CPPFlags
                endif
                echohl WarningMsg | echo " compiling..."
                silent make
            endif
            redraw!
            if v:shell_error != 0
                let s:LastShellReturn_C = v:shell_error
            endif
            if g:iswindows
                if s:LastShellReturn_C != 0
                    exe ":bo cope"
                    echohl WarningMsg | echo " compilation failed"
                else
                    if s:ShowWarning
                        exe ":bo cw"
                    endif
                    echohl WarningMsg | echo " compilation successful"
                endif
            else
                if empty(v:statusmsg)
                    echohl WarningMsg | echo " compilation successful"
                else
                    exe ":bo cope"
                endif
            endif
        else
            echohl WarningMsg | echo ""Obj_Name"is up to date"
        endif
    elseif expand("%:e") == "java"
        let class = expand("%:p:r").s:Class_Extension
        let class_Name = expand("%:p:t:r").s:Class_Extension
        if !filereadable(class) || (filereadable(class) && (getftime(class) < getftime(Sou)))
            redraw!
            exe ":setlocal makeprg=".s:JavaFlags
            echohl WarningMsg | echo " compiling..."
            silent make
            redraw!
            if v:shell_error != 0
                let s:LastShellReturn_C = v:shell_error
            endif
            if g:iswindows
                if s:LastShellReturn_C != 0
                    exe ":bo cope"
                    echohl WarningMsg | echo " compilation failed"
                else
                    if s:ShowWarning
                        exe ":bo cw"
                    endif
                    echohl WarningMsg | echo " compilation successful"
                endif
            else
                if empty(v:statusmsg)
                    echohl WarningMsg | echo " compilation successful"
                else
                    exe ":bo cope"
                endif
            endif
        else
            echohl WarningMsg | echo ""class_Name"is up to date"
        endif
    else
        let s:Sou_Error = 1
        echohl WarningMsg | echo " please choose the correct source file"
    endif
    exe ":setlocal makeprg=make"
endfunc
 
func! Link()
    call Compile()
    if s:Sou_Error || s:LastShellReturn_C != 0
        return
    endif
    if expand("%:e") == "c" || expand("%:e") == "cpp" || expand("%:e") == "cxx"
        let s:LastShellReturn_L = 0
        let Sou = expand("%:p")
        let Obj = expand("%:p:r").s:Obj_Extension
        if g:iswindows
            let Exe = expand("%:p:r").s:Exe_Extension
            let Exe_Name = expand("%:p:t:r").s:Exe_Extension
        else
            let Exe = expand("%:p:r")
            let Exe_Name = expand("%:p:t:r")
        endif
        let v:statusmsg = ''
        if filereadable(Obj) && (getftime(Obj) >= getftime(Sou))
            redraw!
            if !executable(Exe) || (executable(Exe) && getftime(Exe) < getftime(Obj))
                if expand("%:e") == "c"
                    setlocal makeprg=gcc\ -o\ %<\ %<.o
                    echohl WarningMsg | echo " linking..."
                    silent make
                elseif expand("%:e") == "cpp" || expand("%:e") == "cxx"
                    setlocal makeprg=g++\ -o\ %<\ %<.o
                    echohl WarningMsg | echo " linking..."
                    silent make
                endif
                redraw!
                if v:shell_error != 0
                    let s:LastShellReturn_L = v:shell_error
                endif
                if g:iswindows
                    if s:LastShellReturn_L != 0
                        exe ":bo cope"
                        echohl WarningMsg | echo " linking failed"
                    else
                        if s:ShowWarning
                            exe ":bo cw"
                        endif
                        echohl WarningMsg | echo " linking successful"
                    endif
                else
                    if empty(v:statusmsg)
                        echohl WarningMsg | echo " linking successful"
                    else
                        exe ":bo cope"
                    endif
                endif
            else
                echohl WarningMsg | echo ""Exe_Name"is up to date"
            endif
        endif
        setlocal makeprg=make
    elseif expand("%:e") == "java"
        return
    endif
endfunc
 
func! Run()
    let s:ShowWarning = 0
    call Link()
    let s:ShowWarning = 1
    if s:Sou_Error || s:LastShellReturn_C != 0 || s:LastShellReturn_L != 0
        return
    endif
    let Sou = expand("%:p")
    if expand("%:e") == "c" || expand("%:e") == "cpp" || expand("%:e") == "cxx"
        let Obj = expand("%:p:r").s:Obj_Extension
        if g:iswindows
            let Exe = expand("%:p:r").s:Exe_Extension
        else
            let Exe = expand("%:p:r")
        endif
        if executable(Exe) && getftime(Exe) >= getftime(Obj) && getftime(Obj) >= getftime(Sou)
            redraw!
            echohl WarningMsg | echo " running..."
            if g:iswindows
                exe ":!%<.exe"
            else
                if g:isGUI
                    exe ":!gnome-terminal -x bash -c './%<; echo; echo 請按 Enter 鍵繼續; read'"
                else
                    exe ":!clear; ./%<"
                endif
            endif
            redraw!
            echohl WarningMsg | echo " running finish"
        endif
    elseif expand("%:e") == "java"
        let class = expand("%:p:r").s:Class_Extension
        if getftime(class) >= getftime(Sou)
            redraw!
            echohl WarningMsg | echo " running..."
            if g:iswindows
                exe ":!java %<"
            else
                if g:isGUI
                    exe ":!gnome-terminal -x bash -c 'java %<; echo; echo 請按 Enter 鍵繼續; read'"
                else
                    exe ":!clear; java %<"
                endif
            endif
            redraw!
            echohl WarningMsg | echo " running finish"
        endif
    endif
endfunc
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章