Mac vim配置

" vimrc by Godson@ustcbbs
" Last Update: 2010-02-08

let mapleader = ";"    " 比較習(xí)慣用;作為命令前綴,右手小拇指直接能按到
" 把空格鍵映射成:
nmap <space> :

" 快捷打開編輯vimrc文件的鍵盤綁定
map <silent> <leader>ee :e $HOME/.vimrc<cr>
autocmd! bufwritepost *.vimrc source $HOME/.vimrc

" 保存文件
nmap <leader>ww :w!<cr>
" ^z快速進(jìn)入shell
nmap <C-Z> :shell<cr>
" 判斷操作系統(tǒng)
if (has("win32") || has("win64") || has("win32unix"))
    let g:isWin = 1
else
    let g:isWin = 0
endif

" 判斷是終端還是gvim
if has("gui_running")
    let g:isGUI = 1
else
    let g:isGUI = 0
endif

set nocompatible    " 關(guān)閉兼容模式
syntax enable       " 語法高亮
filetype plugin on  " 文件類型插件
filetype indent on
set autoindent
autocmd BufEnter * :syntax sync fromstart
set nu              " 顯示行號(hào)
set showcmd         " 顯示命令
set lz              " 當(dāng)運(yùn)行宏時(shí),在命令執(zhí)行完成之前,不重繪屏幕
set hid             " 可以在沒有保存的情況下切換buffer
set backspace=eol,start,indent 
set whichwrap+=<,>,h,l " 退格鍵和方向鍵可以換行
set incsearch       " 增量式搜索
set hlsearch        " 高亮搜索
set ignorecase      " 搜索時(shí)忽略大小寫
set magic           " 額,自己:h magic吧,一行很難解釋
set showmatch       " 顯示匹配的括號(hào)
set nobackup        " 關(guān)閉備份
set nowb
set noswapfile      " 不使用swp文件,注意,錯(cuò)誤退出后無法恢復(fù)
set lbr             " 在breakat字符處而不是最后一個(gè)字符處斷行
set ai              " 自動(dòng)縮進(jìn)
set si              " 智能縮進(jìn)
set cindent         " C/C++風(fēng)格縮進(jìn)
set wildmenu        
set nofen
set fdl=10

" Java tab轉(zhuǎn)化為4個(gè)字符
"autocmd FileType java set expandtab
"autocmd FileType java set smarttab
"autocmd FileType java set shiftwidth=4
"autocmd FileType java set tabstop=4

autocmd FileType xml set expandtab
autocmd FileType xml set smarttab
autocmd FileType xml set shiftwidth=4
autocmd FileType xml set tabstop=4

" 不使用beep或flash
set vb t_vb=

set background=dark
colorscheme desert
"set t_Co=256

set history=400  " vim記住的歷史操作的數(shù)量,默認(rèn)的是20
set autoread     " 當(dāng)文件在外部被修改時(shí),自動(dòng)重新讀取
set mouse=a      " 在所有模式下都允許使用鼠標(biāo),還可以是n,v,i,c等

"在gvim中高亮當(dāng)前行
if (g:isGUI)
    set cursorline
    hi cursorline guibg=#333333
    hi CursorColumn guibg=#333333
    set guifont=Consolas\ 14
    set guifontwide=Consolas\ 14
endif

" 設(shè)置字符集編碼,默認(rèn)使用utf8
if (g:isWin)
    let &termencoding=&encoding " 通常win下的encoding為cp936
    set fileencodings=utf8,cp936,ucs-bom,latin1
else
    set encoding=utf8
    set fileencodings=utf8,gb2312,gb18030,ucs-bom,latin1
endif

" 狀態(tài)欄
set laststatus=2      " 總是顯示狀態(tài)欄
highlight StatusLine cterm=bold ctermfg=yellow ctermbg=blue
" 獲取當(dāng)前路徑,將$HOME轉(zhuǎn)化為~
function! CurDir()
    let curdir = substitute(getcwd(), $HOME, "~", "g")
    return curdir
endfunction
set statusline=[%n]\ %f%m%r%h\ \|\ \ pwd:\ %{CurDir()}\ \ \|%=\|\ %l,%c\ %p%%\ \|\ ascii=%b,hex=%b%{((&fenc==\"\")?\"\":\"\ \|\ \".&fenc)}\ \|\ %{$USER}\ @\ %{hostname()}\

" 第80列往后加下劃線
"au BufWinEnter * let w:m2=matchadd('Underlined', '\%>' . 80 . 'v.\+', -1)

" 根據(jù)給定方向搜索當(dāng)前光標(biāo)下的單詞,結(jié)合下面兩個(gè)綁定使用
function! VisualSearch(direction) range
    let l:saved_reg = @"
    execute "normal! vgvy"
    let l:pattern = escape(@", '\\/.*$^~[]')
    let l:pattern = substitute(l:pattern, "\n$", "", "")
    if a:direction == 'b'
        execute "normal ?" . l:pattern . "<cr>"
    else
        execute "normal /" . l:pattern . "<cr>"
    endif
    let @/ = l:pattern
    let @" = l:saved_reg
endfunction
" 用 */# 向 前/后 搜索光標(biāo)下的單詞
vnoremap <silent> * :call VisualSearch('f')<CR>
vnoremap <silent> # :call VisualSearch('b')<CR>

" 在文件名上按gf時(shí),在新的tab中打開
"map gf :tabnew <cfile><cr>

" 用c-j,k在buffer之間切換
nn <C-J> :bn<cr>
nn <C-K> :bp<cr>

" Bash(Emacs)風(fēng)格鍵盤綁定
imap <C-e> <END>
imap <C-a> <HOME>
"imap <C-u> <esc>d0i
"imap <C-k> <esc>d$i  " 與自動(dòng)補(bǔ)全中的綁定沖突


" 恢復(fù)上次文件打開位置
set viminfo='10,\"100,:20,%,n~/.viminfo
au BufReadPost * if line("'\"") > 0|if line("'\"") <= line("$")|exe("norm '\"")|else|exe "norm $"|endif|endif

" 刪除buffer時(shí)不關(guān)閉窗口
command! Bclose call <SID>BufcloseCloseIt()
function! <SID>BufcloseCloseIt()
    let l:currentBufNum = bufnr("%")
    let l:alternateBufNum = bufnr("#")

    if buflisted(l:alternateBufNum)
        buffer #
    else
        bnext
    endif

    if bufnr("%") == l:currentBufNum
        new
    endif

    if buflisted(l:currentBufNum)
        execute("bdelete! ".l:currentBufNum)
    endif
endfunction


" 快捷輸入
" 自動(dòng)完成括號(hào)和引號(hào)
inoremap <leader>1 ()<esc>:let leavechar=")"<cr>i
inoremap <leader>2 []<esc>:let leavechar="]"<cr>i
inoremap <leader>3 {}<esc>:let leavechar="}"<cr>i
inoremap <leader>4 {<esc>o}<esc>:let leavechar="}"<cr>O
inoremap <leader>q ''<esc>:let leavechar="'"<cr>i
inoremap <leader>w ""<esc>:let leavechar='"'<cr>i

" 縮寫
iab idate <c-r>=strftime("%Y-%m-%d")<CR>
iab itime <c-r>=strftime("%H:%M")<CR>
iab imail Peng Wang <wangpeng05@gmail.com>
iab iumail pengwang@mail.ustc.edu.cn
iab igmail wangpeng05@gmail.com
iab iname Peng Wang

" 插件窗口的寬度,如TagList,NERD_tree等,自己設(shè)置
let s:PlugWinSize = 30

" ShowFunc.vim  <-------- 暫時(shí)沒有使用
" http://www.vim.org/scripts/script.php?script_id=397
" F2打開ShowFunc TagList窗口,顯示C/C++函數(shù)原型
" map <F2> <Plug>ShowFunc
" map! <F2> <Plug>ShowFunc

" taglist.vim
" http://www.vim.org/scripts/script.php?script_id=273
" <leader>t 打開TagList窗口,窗口在右邊
nmap <silent> <leader>t :TlistToggle<cr>
"let Tlist_Ctags_Cmd = '/usr/bin/ctags'
let Tlist_Show_One_File = 0
let Tlist_Exit_OnlyWindow = 1 
let Tlist_Use_Right_Window = 1
let Tlist_File_Fold_Auto_Close = 1
let Tlist_GainFocus_On_ToggleOpen = 0
let Tlist_WinWidth = s:PlugWinSize
let Tlist_Auto_Open = 0
let Tlist_Display_Prototype = 0
"let Tlist_Close_On_Select = 1

" minibufexpl.vim
" http://www.vim.org/scripts/script.php?script_id=159
" 不需要配置


" OmniCppComplete.vim
" http://www.vim.org/scripts/script.php?script_id=1520
set completeopt=menu
let OmniCpp_ShowPrototypeInAbbr = 1 
let OmniCpp_DefaultNamespaces = ["std"]     " 逗號(hào)分割的字符串
let OmniCpp_MayCompleteScope = 1 
let OmniCpp_ShowPrototypeInAbbr = 0 
let OmniCpp_SelectFirstItem = 2 
" c-j自動(dòng)補(bǔ)全,當(dāng)補(bǔ)全菜單打開時(shí),c-j,k上下選擇
imap <expr> <c-j>      pumvisible()?"\<C-N>":"\<C-X><C-O>"
imap <expr> <c-k>      pumvisible()?"\<C-P>":"\<esc>"
" f:文件名補(bǔ)全,l:行補(bǔ)全,d:字典補(bǔ)全,]:tag補(bǔ)全
imap <C-]>             <C-X><C-]>
imap <C-F>             <C-X><C-F>
imap <C-D>             <C-X><C-D>
imap <C-L>             <C-X><C-L> 

" NERD_commenter.vim
" http://www.vim.org/scripts/script.php?script_id=1218
" Toggle單行注釋/“性感”注釋/注釋到行尾/取消注釋
map <leader>cc ,c<space>
map <leader>cs ,cs
map <leader>c$ ,c$
map <leader>cu ,cu

" NERD tree
" http://www.vim.org/scripts/script.php?script_id=1658
let NERDTreeShowHidden = 1
let NERDTreeWinPos = "right"
let NERDTreeWinSize = s:PlugWinSize 
nmap <leader>n :NERDTreeToggle<cr>

" DoxygenToolkit.vim
" http://www.vim.org/scripts/script.php?script_id=987
" 暫時(shí)沒有使用

" 更新ctags和cscope索引
" href: http://www.vimer.cn/2009/10/把vim打造成一個(gè)真正的ide2.html
" 稍作修改,提取出DeleteFile函數(shù),修改ctags和cscope執(zhí)行命令
map <F12> :call Do_CsTag()<cr>
function! Do_CsTag()
    let dir = getcwd()

    "先刪除已有的tags和cscope文件,如果存在且無法刪除,則報(bào)錯(cuò)。
    if ( DeleteFile(dir, "tags") ) 
        return 
    endif
    if ( DeleteFile(dir, "cscope.files") ) 
        return 
    endif
    if ( DeleteFile(dir, "cscope.out") ) 
        return 
    endif

    if(executable('ctags'))
        silent! execute "!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q ."
    endif
    if(executable('cscope') && has("cscope") )
        if(g:isWin)
            silent! execute "!dir /s/b *.c,*.cpp,*.h,*.java,*.cs >> cscope.files"
        else
            silent! execute "!find . -iname '*.[ch]' -o -name '*.cpp' > cscope.files"
        endif
        silent! execute "!cscope -b"
        execute "normal :"
        if filereadable("cscope.out")
            execute "cs add cscope.out"
        endif
    endif
    " 刷新屏幕
    execute "redr!"
endfunction

function! DeleteFile(dir, filename)
    if filereadable(a:filename)
        if (g:isWin)
            let ret = delete(a:dir."\\".a:filename)
        else
            let ret = delete("./".a:filename)
        endif
        if (ret != 0)
            echohl WarningMsg | echo "Failed to delete ".a:filename | echohl None
            return 1
        else
            return 0
        endif
    endif
    return 0
endfunction

" cscope 綁定
if has("cscope")
    set csto=1
    set cst
    set nocsverb
    if filereadable("cscope.out")
        cs add cscope.out
    endif
    set csverb
    " s: C語言符號(hào)  g: 定義     d: 這個(gè)函數(shù)調(diào)用的函數(shù) c: 調(diào)用這個(gè)函數(shù)的函數(shù)
    " t: 文本       e: egrep模式    f: 文件     i: include本文件的文件
    nmap <leader>ss :cs find s <C-R>=expand("<cword>")<CR><CR>:copen<CR>
    nmap <leader>sg :cs find g <C-R>=expand("<cword>")<CR><CR>
    nmap <leader>sc :cs find c <C-R>=expand("<cword>")<CR><CR>:copen<CR>
    nmap <leader>st :cs find t <C-R>=expand("<cword>")<CR><CR>:copen<CR>
    nmap <leader>se :cs find e <C-R>=expand("<cword>")<CR><CR>:copen<CR>
    nmap <leader>sf :cs find f <C-R>=expand("<cfile>")<CR><CR>:copen<CR>
    nmap <leader>si :cs find i ^<C-R>=expand("<cfile>")<CR>$<CR>:copen<CR>
    nmap <leader>sd :cs find d <C-R>=expand("<cword>")<CR><CR>:copen<CR>
endif

" Quick Fix 設(shè)置
map <leader>cw :cw<cr>
map <F3> :cp<cr>
map <F4> :cn<cr>


" lookup file
" http://www.vim.org/scripts/script.php?script_id=1581
" 增加一行(line 295): let pattern = '\c' . a:pattern
" 按F5或者;ff打開文件搜索窗口
let g:LookupFile_MinPatLength = 0
let g:LookupFile_PreserveLastPattern = 0
let g:LookupFile_PreservePatternHistory = 0
let g:LookupFile_AlwaysAcceptFirst = 1
let g:LookupFile_AllowNewFiles = 0
if filereadable("./filenametags")
  let g:LookupFile_TagExpr = '"./filenametags"'
endif
nmap <silent> <leader>ff :LookupFile<cr>

function! LookupFile_IgnoreCaseFunc(pattern)
    let _tags = &tags
    try
        let &tags = eval(g:LookupFile_TagExpr)
        let newpattern = '\c' . a:pattern
        let tags = taglist(newpattern)
    catch
        echohl ErrorMsg | echo "Exception: " . v:exception | echohl NONE
        return ""
    finally
        let &tags = _tags
    endtry

    " Show the matches for what is typed so far.
    let files = map(tags, 'v:val["filename"]')
    return files
endfunction
let g:LookupFile_LookupFunc = 'LookupFile_IgnoreCaseFunc' 

" Buffers Explorer (需要genutils.vim)
" http://vim.sourceforge.net/scripts/script.php?script_id=42
" http://www.vim.org/scripts/script.php?script_id=197
let g:bufExplorerDefaultHelp=0       " Do not show default help.
let g:bufExplorerShowRelativePath=1  " Show relative paths.
let g:bufExplorerSortBy='mru'        " Sort by most recently used.
let g:bufExplorerSplitRight=0        " Split left.
let g:bufExplorerSplitVertical=1     " Split vertically.
let g:bufExplorerSplitVertSize = s:PlugWinSize  " Split width
let g:bufExplorerUseCurrentWindow=1  " Open in new window.
autocmd BufWinEnter \[Buf\ List\] setl nonumber
nmap <silent> <Leader>b :BufExplorer<CR>


set tags=tags;
set autochdir
"nmap <silent> <leader>p :ProjectTree<cr>
nmap <silent> <leader>p :set mouse=c<cr>

copy上面內(nèi)容,放到 ~/.vimrc文件中即可,沒有該文件就新建一個(gè)吧

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容