`

vim配置

vim 
阅读更多

"=========================================================================
"                       第一部分
"=========================================================================
   "配置颜色
colorscheme murphy
"  colorscheme 256-jungle

   " 设置显示字体
    if has("win32")
        set guifont=Courier_New:h11:cANSI
        "set guifont=YaHei\ Mono:h11
        "set guifontwide=Microsoft\ Yahei\ Monotype:h11
        "set guifont=YaHei\ Consolas\ Hybrid:h12
    endif
    " 不要使用vi的键盘模式,而是vim自己的
    set nocompatible
    
    " 加载配置。
    behave mswin
    
    "设置快速编辑.vimrc文件 ,e 编辑.vimrc
    map <silent> <leader>e :call SwitchToBuf("~/_vimrc")<cr>
    
    "保存.vimrc文件后会自动调用新的.vimrc
    autocmd! bufwritepost .vimrc source ~/_vimrc
 
    " 设定解码
    if has("multi_byte")
        " When 'fileencodings' starts with 'ucs-bom', don't do this manually
        "set bomb
        set fileencodings=ucs-bom,utf-8,chinese,taiwan,japan,korea,latin1
        " CJK environment detection and corresponding setting
        if v:lang =~ "^zh_CN"
            " Simplified Chinese, on Unix euc-cn, on MS-Windows cp936
            set encoding=utf-8
            set termencoding=utf-8
            if &fileencoding == ''
                set fileencoding=utf-8
            endif
        elseif v:lang =~ "^zh_TW"
            " Traditional Chinese, on Unix euc-tw, on MS-Windows cp950
            set encoding=euc-tw
            set termencoding=euc-tw
            if &fileencoding == ''
                set fileencoding=euc-tw
            endif
        elseif v:lang =~ "^ja_JP"
            " Japanese, on Unix euc-jp, on MS-Windows cp932
            set encoding=euc-jp
            set termencoding=euc-jp
            if &fileencoding == ''
                set fileencoding=euc-jp
            endif
        elseif v:lang =~ "^ko"
            " Korean on Unix euc-kr, on MS-Windows cp949
            set encoding=euc-kr
            set termencoding=euc-kr
            if &fileencoding == ''
                set fileencoding=ecu-kr
            endif
        endif
        " Detect UTF-8 locale, and override CJK setting if needed
        if v:lang =~ "utf8$" || v:lang =~ "UTF-8$"
            set encoding=utf-8
        endif
    else
        echoerr 'Sorry, this version of (g)Vim was not compiled with "multi_byte"'
    endif
    
    
    " browsedir设置
    set browsedir=buffer
    
    " 自动格式化设置
    filetype indent on
    set autoindent
    set smartindent
    
    " 设置备份及备份目录。
    set backspace=indent,eol,start
    set backupdir=D:\vim_back_files
    set autochdir
    
    " history文件中需要记录的行数,恢复必须用到。
    set history=1024
    

 
  " 配置多语言环境
if has("multi_byte")
    " UTF-8 编码
    set encoding=utf-8
    set termencoding=utf-8
    set formatoptions+=mM
    set fencs=utf-8,gbk
    if v:lang =~? '^\(zh\)\|\(ja\)\|\(ko\)'
        set ambiwidth=double
    endif
    if has("win32")
        source $VIMRUNTIME/delmenu.vim
        source $VIMRUNTIME/menu.vim
        language messages zh_CN.utf-8
    endif
else
    echoerr "Sorry, this version of (g)vim was not compiled with +multi_byte"
endif
    
    
    " 在处理未保存或只读文件的时候,弹出确认
    set confirm
    
    " 共享外部剪贴板
    set clipboard+=unnamed
   
    " 选中状态下 Ctrl+c 复制
  vmap <C-c> "+y
    
    " 显示未完成命令
    set showcmd
    " 侦测文件类型
    filetype on
    
    " 载入文件类型插件
    filetype plugin on
    
    " 为特定文件类型载入相关缩进文件
    filetype indent on
    
    " 保存全局变量
    set viminfo+=!
    
    " 带有如下符号的单词不要被换行分割
    set iskeyword+=_,$,@,%,#,-
    
    " 语法高亮
    syntax on
    
    " 设置不兼容模式
    set nocp
   
      
    " 设置鼠标支持
    set mouse=a
    
    " 不要生成swap文件
    setlocal noswapfile
    
    " 当buffer被丢弃时隐藏
    set bufhidden=hide
    
    " 保存窗口大小
    set sessionoptions+=resize
    
    " C++头文件识别
    au BufEnter /usr/include/c++/* setf cpp
    au BufEnter /usr/include/g++-3/* setf cpp
    
    " GNU标准
    au BufEnter /usr/* call GnuIndent()
    
    " 显示行号
    set number
    
    " tab宽度
    set tabstop=4
    set cindent shiftwidth=4
    set autoindent shiftwidth=4
    
    " C/C++注释
    set comments=://
    
    " 修正自动C式样注释功能 <2005/07/16>
    set comments=s1:/*,mb:*,ex0:/
    
    " 增强检索功能
    set tags=./tags,./../tags,./**/tags
    
    " 保存文件格式
    set fileformats=unix,dos
    
    " 文件被其他程序修改时自动载入
    set autoread
    
    " 高亮字符,让其不受100列限制
    :highlight OverLength ctermbg=red ctermfg=white guibg=red guifg=white
    :match OverLength '\%101v.*'
    
    " 状态行颜色
    " highlight StatusLine guifg=SlateBlue guibg=Yellow
    highlight StatusLine guifg=SlateBlue guibg=White
    highlight StatusLineNC guifg=Gray guibg=White
    
    " 我的状态行显示的内容(包括文件类型和解码)
    set statusline=[%n]%<%f%y%h%m%r%=[%b\ 0x%B]\ %l\ of\ %L,%c%V\ Page\ %N\ %P
    
    " 设置命令行高度为2行
    set cmdheight=1
""=========================自动补全功能设置=================   
    " 命令行补全
    set wildmenu
    " 自动补全括号,包括大括号
    :inoremap ( ()<ESC>i
    :inoremap ) <c-r>=ClosePair(')')<CR>
    :inoremap { {}<ESC>i
    :inoremap } <c-r>=ClosePair('}')<CR>
    :inoremap [ []<ESC>i
    :inoremap ] <c-r>=ClosePair(']')<CR>
    ":inoremap < <><ESC>i
    ":inoremap > <c-r>=ClosePair('>')<CR>
   
    " 自动补全引号
:inoremap " ""<ESC>i
:inoremap ' ''<ESC>i
function ClosePair(char)
if getline('.')[col('.') - 1] == a:char
return "\<Right>"
else
return a:char
endif
endf

" 为C程序提供自动缩进
set smartindent
"代码补全
set completeopt=preview,menu
"自动补全
:inoremap ( ()<ESC>i
:inoremap ) <c-r>=ClosePair(')')<CR>
:inoremap { {<CR>}<ESC>O
:inoremap } <c-r>=ClosePair('}')<CR>
:inoremap [ []<ESC>i
:inoremap ] <c-r>=ClosePair(']')<CR>
:inoremap " ""<ESC>i
":inoremap " <c-r>=ClosePair('"')<CR>
:inoremap ' ''<ESC>i
":inoremap ' <c-r>=ClosePair('\'')<CR>
function! ClosePair(char)
if getline('.')[col('.') - 1] == a:char
return "\<Right>"
else
return a:char
endif
endfunction
filetype plugin indent on
"打开文件类型检测, 加了这句才可以用智能补全
set completeopt=longest,menu

" 增强模式中的命令行自动完成操作
set wildmenu




autocmd BufNewFile *.py,*.cc,*.sh,*.java exec ":call SetTitle()"       
"新建.py,.cc,.java,.sh, 
""定义函数SetTitle,自动插入文件头 
func SetTitle() 
        "如果文件类型为.sh文件 
        if &filetype == 'python' 
                call setline(1, "\#-*- encoding: utf-8 -*- ") 
                call setline(2, "\"\"\"") 
                call setline(3, "\# @Author: elizhongyang") 
                call setline(4, "\# Created Time : ".strftime("%c")) 
                call setline(5, "") 
                call setline(6, "\# File Name: ".expand("%")) 
                call setline(7, "\# Description:") 
                call setline(8, "") 
                call setline(9, "\"\"\"") 
                call setline(10,"") 
        endif 
        if &filetype == 'java' 
                call setline(1, "//coding=utf8") 
                call setline(2, "/*************************************************************************") 
                call setline(3, "\ @Author: elizhongyang") 
                call setline(4, "\ @Created Time : ".strftime("%c")) 
                call setline(5, "") 
                call setline(6, "\ @File Name: ".expand("%")) 
                call setline(7, "\ @Description:") 
                call setline(8, "") 
                call setline(9, " ************************************************************************/") 
                call setline(10,"") 
        endif 
endfunc 



"取消VIM的自动备份功能
set nobackup



    
  ""=========================自动补全功能设置完毕=================      
    " 打开文件时,总是跳到退出之前的光标处
    autocmd BufReadPost *
    \ if line("'\"") > 0 && line("'\"") <= line("$") |
    \   exe "normal! g`\"" |
    \ endif
    
    " 用浅色高亮当前行
    if has("gui_running")
        autocmd InsertLeave * se nocul
        autocmd InsertEnter * se cul
    endif
    
    filetype plugin on      "允许使用ftplugin目录下的文件类型特定脚本
    filetype indent on      "允许使用indent目录下的文件类型缩进

   
"==========================代码折叠========================
    
    " 设置以缩进的方式自动折叠和设置快捷方式
    set foldmethod=indent
    
    map <F3> zO " 打开折叠
    map <F4> zc " 关闭折叠
    map <F8> zR    " 打开所有折叠
    map <F9> zM    " 关闭所有折叠
    
  set foldenable              " 开始折叠
  set foldmethod=syntax       " 设置语法折叠

"==========================配置完毕========================   
    
    " 更改Leader为","
    let g:C_MapLeader = ','
    
    " 不要闪烁
    set novisualbell
    
    " 能够漂亮地显示.NFO文件
    set encoding=utf-8
    function! SetFileEncodings(encodings)
        let b:myfileencodingsbak=&fileencodings
        let &fileencodings=a:encodings
    endfunction
    function! RestoreFileEncodings()
        let &fileencodings=b:myfileencodingsbak
        unlet b:myfileencodingsbak
    endfunction
    
    au BufReadPre *.nfo call SetFileEncodings('cp437')|set ambiwidth=single
    au BufReadPost *.nfo call RestoreFileEncodings()
   
  
    " 隐藏掉菜单和工具条。
    set guioptions-=m
    set guioptions-=T
    map <silent> <F2> :if &guioptions =~# 'T' <Bar>
            \set guioptions-=T <Bar>
            \set guioptions-=m <bar>
        \else <Bar>
            \set guioptions+=T <Bar>
            \set guioptions+=m <Bar>
        \endif<CR>
    
    " 标签页设置
    if has("gui_running")
        set showtabline=2
        map! tn tabnew
        nmap <C-c> :tabclose<CR>
    endif
    
    " 标签页只显示文件名
    function ShortTabLabel ()
        let bufnrlist = tabpagebuflist (v:lnum)
        let label = bufname (bufnrlist[tabpagewinnr (v:lnum) -1])
        let filename = fnamemodify (label, ':t')
        return filename
    endfunction
    
    set guitablabel=%{ShortTabLabel()}
    
    " 使回格键(backspace)正常处理indent, eol, start等
    set backspace=eol,start,indent
    
    " 允许backspace和光标键跨越行边界
    " set whichwrap+=<,>,h,l
    
    " 可以在buffer的任何地方使用鼠标(类似office中在工作区双击鼠标定位)
    set mouse=a
    set selection=exclusive
    set selectmode=mouse,key
    
    " 启动的时候不显示那个援助索马里儿童的提示
    set shortmess=atI
    
    " 通过使用: commands命令,告诉我们文件的哪一行被改变过
    set report=0
    
    " 不让vim发出讨厌的滴滴声
    set noerrorbells
    
    " 在被分割的窗口间显示空白,便于阅读
    set fillchars=vert:\ ,stl:\ ,stlnc:\

    """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    "                              搜索和匹配                                     
    """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
   " 高亮显示匹配的括号
    set showmatch
    
    " 匹配括号高亮的时间(单位是十分之一秒)
    set matchtime=3
    
    " 在搜索的时候忽略大小写
    set ignorecase
    
    " 不要高亮被搜索的句子(phrases)
    " set nohlsearch
    
    " 在搜索时,输入的词句的逐字符高亮(类似firefox的搜索)
    set incsearch
    
    " 输入:set list命令是应该显示些啥?
    set listchars=tab:\|\ ,trail:.,extends:>,precedes:<,eol:$
    " Tab补全时忽略这些忽略这些
    set wildignore=*.o,*.obj,*.bak,*.exe
    " 光标移动到buffer的顶部和底部时保持3行距离
    set scrolloff=3
    
    "搜索出之后高亮关键词
    set hlsearch
    
    nmap <silent> <leader><cr> :noh<cr>
   
    :set incsearch                       "``ncomplete)在输入单词的同时显示搜索结果

    """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    "                              CTags的设定                                    
    """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
  set tags=tags;
    " 按照名称排序
    let Tlist_Sort_Type = "name"
    
    " 在右侧显示窗口
    let Tlist_Use_Right_Window = 1
    
    " 压缩方式
    let Tlist_Compart_Format = 1
    
    " 如果只有一个buffer,kill窗口也kill掉buffer
    let Tlist_Exist_OnlyWindow = 1
    
    " 不要关闭其他文件的tags
    let Tlist_File_Fold_Auto_Close = 0
    
    " 不要显示折叠树
   
    let Tlist_Enable_Fold_Column = 0
   
   
 
   
    
    """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    "                        脚本内部用到的自定义函数                             
    """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
  "
    "函数后面加上!是防止vimrc文件重新载入时报错
    "实现光标位置自动交换:) -->  ):
    function! Swap()
        if getline('.')[col('.') - 1] =~ ")"
            return "\<ESC>la:"
        else
            return ":"
        endif
    endf
    
    """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"               python配置
    """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

"SwitchToBuf()实现它在所有标签页的窗口中查找指定的文件名,如果找到这样一个窗口,
    "就跳到此窗口中;否则,它新建一个标签页来打开vimrc文件
    "上面自动编辑.vimrc文件用到的函数
    function! SwitchToBuf(filename)
        let bufwinnr = bufwinnr(a:filename)
        if bufwinnr != -1
        exec bufwinnr . "wincmd w"
            return
        else
            " find in each tab
            tabfirst
            let tab = 1
            while tab <= tabpagenr("$")
                let bufwinnr = bufwinnr(a:filename)
                if bufwinnr != -1
                    exec "normal " . tab . "gt"
                    exec bufwinnr . "wincmd w"
                    return
                endif
                tabnext
                let tab = tab + 1
            endwhile
            " not exist, new tab
            exec "tabnew " . a:filename
        endif
    endfunction
    
    """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    "Last change用到的函数,返回时间,能够自动调整位置
    function! TimeStamp(...)
        let sbegin = ''
        let send = ''
        if a:0 >= 1
            let sbegin = a:1.'\s*'
        endif
        if a:0 >= 2
            let send = ' '.a:2
        endif
        let pattern =  'Last Change: .\+'
            \. send
        let pattern = '^\s*' . sbegin . pattern . '\s*$'
        let now = strftime('%Y-%m-%d %H:%M:%S',
            \localtime())
        let row = search(pattern, 'n')
        if row  == 0
            let now = a:1 .  ' Last Change:  '
                \. now . send
            call append(2, now)
        else
            let curstr = getline(row)
            let col = match( curstr , 'Last')
            let spacestr = repeat(' ',col - 1)
            let now = a:1 . spacestr . 'Last Change:  '
                \. now . send
            call setline(row, now)
        endif
    endfunction


"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
 
                  "第一部分完
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""


""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
 
                     "第二部分
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""



""=================================Vim的格式化代码功能======================

"打开对文件类型插件的支持
filetype plugin on
filetype plugin indent on
"设置制表符宽度,默认为
set tabstop=6
set cindent shiftwidth=6
set autoindent shiftwidth=6
"Fortran 语法
let fortran_more_precise=1
let fortran_do_enddo=1
let fortran_if_elseif_endif=1
"设置无备份文件
set writebackup
set nobackup
""=============================================Vim的格式化代码功能 完毕================


""=========================" NERDTree 进行项目管理===============

"重启 vim 后,按下 F10 键,就可以在左侧看到一个目录树了。在目录树窗口中按下 "? 键可以查看详细的帮助信息。
"最常用的操作键有:
"C(大写 C 键)将光标所在目录设置为根目录
"u(小写 u 键)转到上一级目录
"o(小写 o 键,不是“零”)展开(或折叠)光标所在目录的子目录。如果光标所在""位置是一个文件,则在编辑窗口中打开该文件

map <F10> :NERDTreeToggle<CR>

""====================taglist的插件配置===========================

    let Tlist_Ctags_Cmd = '"'.$VIMRUNTIME.'/ctags.exe"'


" taglist 是vim的插件,用来在屏幕左边开窗口显示代码的全局变量、函数名称
let Tlist_Auto_Highlight_Tag = 1
let Tlist_Auto_Open = 0
let Tlist_Auto_Update = 1
let Tlist_Close_On_Select = 0
let Tlist_Compact_Format = 0
let Tlist_Display_Prototype = 0
let Tlist_Display_Tag_Scope = 1
let Tlist_Enable_Fold_Column = 0
let Tlist_Exit_OnlyWindow = 1
let Tlist_File_Fold_Auto_Close = 1
let Tlist_GainFocus_On_ToggleOpen = 1
let Tlist_Hightlight_Tag_On_BufEnter = 1
let Tlist_Inc_Winwidth = 0
let Tlist_Max_Submenu_Items = 1
let Tlist_Max_Tag_Length = 30
let Tlist_Process_File_Always = 0
let Tlist_Show_Menu = 0
let Tlist_Show_One_File = 1
let Tlist_Sort_Type = "order"
let Tlist_Use_Horiz_Window = 0
let Tlist_Use_Right_Window = 1
let Tlist_WinWidth = 20
let tlist_php_settings = 'php;c:class;i:interfaces;d:constant;f:function'

" Taglist 快捷键
    map <F5> :TlistToggle<CR>
    let Tlist_WinWidth = 20
    let Tlist_Use_Right_Window = 1
    let Tlist_Use_SingleClick = 1
    "map <F8> :! ctags -R .<CR>

""=================NERDTree配置============================
nnoremap <silent> <F12> :A<CR>

":A 在新Buffer中切换到c/h文件
":AS 横向分割窗口并打开c/h文件
":AV 纵向分割窗口并打开c/h文件
":AT 新建一个标签页并打开c/h文件

  " NERDTree 快捷键
    "map <F2> :NERDTreeMirror<CR>
    "map <F3> :NERDTreeToggle<CR>
    "<10>打开文件树

"===============================================



" 打开文件时光标自动到上次退出该文件时的光标所在位置
autocmd BufReadPost * if line("'\"") && line("'\"") <= line("$") | exe "normal`\"" | endif

"====================java配置=====================


"=========================cscope快捷键==============
set cscopetag!
    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>


"-- omnicppcomplete setting --
set completeopt=menu,menuone
let OmniCpp_MayCompleteDot = 1 " autocomplete with .
let OmniCpp_MayCompleteArrow = 1 " autocomplete with ->
let OmniCpp_MayCompleteScope = 1 " autocomplete with ::
let OmniCpp_SelectFirstItem = 2 " select first item (but don't insert)
let OmniCpp_NamespaceSearch = 2 " search namespaces in this and included files
let OmniCpp_ShowPrototypeInAbbr = 1 " show function prototype  in popup window
let OmniCpp_GlobalScopeSearch=1
let OmniCpp_DisplayMode=1
let OmniCpp_DefaultNamespaces=["std"]
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics