" vim:expandtab shiftwidth=2 tabstop=8 textwidth=72 " Wu Yongwei's _vimrc for Vim 7 " Last Change: 2016-04-12 20:51:15 +0800 if v:version < 700 echoerr 'This _vimrc requires Vim 7 or later.' quit endif if has('autocmd') " Remove ALL autocommands for the current group au! endif if has('gui_running') " Always show file types in menu let do_syntax_sel_menu=1 endif if has('multi_byte') " Legacy encoding is the system default encoding let legacy_encoding=&encoding endif if has('gui_running') && has('multi_byte') " Set encoding if $LANG !~ '\.' || $LANG =~? '\.UTF-8$' set encoding=utf-8 else let &encoding=matchstr($LANG, '\.\zs.*') let legacy_encoding=&encoding endif endif set nocompatible source $VIMRUNTIME/vimrc_example.vim if has('gui_running') source $VIMRUNTIME/mswin.vim unmap | " for Redo is kept in insert mode iunmap | " for Select-All is kept in normal mode " Key mapping to switch tabs quickly nmap :tabn imap :tabn nmap :tabp imap :tabp " Key mapping to switch windows quickly nmap w imap w nmap W imap W endif set autoindent set nobackup set formatoptions+=mM set fileencodings=ucs-bom,utf-8,default,latin1 " default value set grepprg=grep\ -nH set statusline=%<%f\ %h%m%r%=%k[%{(&fenc==\"\")?&enc:&fenc}%{(&bomb?\",BOM\":\"\")}]\ %-14.(%l,%c%V%)\ %P set dictionary+=~\vimfiles\words set tags+=~\vimfiles\systags " help ft-c-omni set directory=~\AppData\Local\Temp set path=., \, if has("directx") && $VIM_USE_DIRECTX != '0' set renderoptions=type:directx let s:use_directx=1 endif " Personal setting for working with Windows (requires tee in path) if &shell =~? 'cmd' "set shellxquote=\" set shellpipe=2>&1\|\ tee endif " Quote shell if it contains space and is not quoted if &shell =~? '^[^"].* .*[^"]' let &shell='"' . &shell . '"' endif " Set British spelling convention for International English if has('syntax') set spelllang=en_gb endif " Use persistent undo if has('persistent_undo') set undodir=~\AppData\Local\Temp\vim_undo_files set undofile endif if has('eval') " Function to find the absolute path of a runtime file function! FindRuntimeFile(filename, ...) if a:0 != 0 && a:1 =~ 'w' let require_writable=1 else let require_writable=0 endif let runtimepaths=&runtimepath . ',' while strlen(runtimepaths) != 0 let filepath=substitute(runtimepaths, ',.*', '', '') . '/' . a:filename if filereadable(filepath) if !require_writable || filewritable(filepath) return filepath endif endif let runtimepaths=substitute(runtimepaths, '[^,]*,', '', '') endwhile return '' endfunction " Function to display the current character code in its 'file encoding' function! EchoCharCode() let char_enc=matchstr(getline('.'), '.', col('.') - 1) let char_fenc=iconv(char_enc, &encoding, &fileencoding) let i=0 let len=len(char_fenc) let hex_code='' while i < len let hex_code.=printf('%.2x',char2nr(char_fenc[i])) let i+=1 endwhile echo '<' . char_enc . '> Hex ' . hex_code . ' (' . \(&fileencoding != '' ? &fileencoding : &encoding) . ')' endfunction " Key mapping to display the current character in its 'file encoding' nmap gn :call EchoCharCode() " Function to switch the cursor position between the first column and the " first non-blank column function! GoToFirstNonBlankOrFirstColumn() let cur_col=col('.') normal! ^ if cur_col != 1 && cur_col == col('.') normal! 0 endif return '' endfunction " Key mappings to make Home go to first non-blank column or first column nmap :call GoToFirstNonBlankOrFirstColumn() imap =GoToFirstNonBlankOrFirstColumn() " Function to insert the current date function! InsertCurrentDate() let curr_date=strftime('%Y-%m-%d', localtime()) return curr_date endfunction " Key mapping to insert the current date imap =InsertCurrentDate() endif " Key mappings to ease browsing long lines noremap gj noremap gk inoremap g0 inoremap g$ " Key mappings for quick arithmetic inside Vim (requires a calcu in path) nnoremap ma yypV:!calcu "k$ vnoremap ma yopV:!calcu "k$ nnoremap mr yyV:!calcu "$ vnoremap mr ygvmaomb:r !calcu ""ay$dd`bv`a"ap " Key mappings for indenting xnoremap < >gv " Key mapping for quick substitution nmap S :%s//g xmap S :s//g " Key mapping for confirmed exiting nnoremap ZX :confirm qa " Key mapping for opening the clipboard (Vim script #1014) to avoid " conflict with the NERD Commenter (Vim script #1218) nmap co ClipBrdOpen " Key mapping to stop the search highlight nmap :nohlsearch imap :nohlsearch " Key mappings to fold line according to syntax nmap :set fdl=1 fdm=syntaxsyn sync fromstart nmap zv nmap zc " Key mapping to toggle the display of tabs and trailing spaces nmap :if &list == 0 \exec "set listchars=tab:\uBB\u2014,trail:\uB7,nbsp:~" \set list \echo "Display of TAB and trailing SPACE turned on" \else \set nolist \echo \endif " Key mapping to toggle the background nmap :if &background == 'light' \colorscheme bandit \set background=dark \echo \else \colorscheme default \set background=light \echo \endif " Key mapping to toggle the display of status line for the last window nmap :if &laststatus == 1 \set laststatus=2 \echo \else \set laststatus=1 \endif " Key mapping for the taglist.vim plug-in (Vim script #273) nmap :Tlist imap :Tlist " Key mappings for quickfix commands, tags, and buffers nmap :cn nmap :cp nmap :copen nmap :cclose nmap :tn nmap :tp nmap :n nmap :prev " Key mappings for Damian Conway's listtrans.vim " nmap l :call ListTrans_toggle_format() vmap l :call ListTrans_toggle_format('visual') " Key mappings for Damian Conway's vmath.vim " " Warning: One must remove the "gv" in his script, otherwise the result " cannot be displayed. xmap ++ VMATH_YankAndAnalyse() nmap ++ vip++ " Key mappings for Damian Conway's dragvisuals.vim " if has('gui_running') xmap DVB_Drag('left') xmap DVB_Drag('right') xmap DVB_Drag('down') xmap DVB_Drag('up') else xmap DVB_Drag('left') xmap DVB_Drag('right') xmap DVB_Drag('down') xmap DVB_Drag('up') endif " Key mapping for clang-format " function! UseClangFormat() map :pyf C:/Program\ Files\ (x86)/LLVM/share/clang/clang-format.py imap :pyf C:/Program\ Files\ (x86)/LLVM/share/clang/clang-format.pyi endfunction " Function to turn each paragraph to a line (to work with, say, MS Word) function! ParagraphToLine() normal! ma if &formatoptions =~ 'w' let reg_bak=@" normal! G$vy if @" =~ '\s' normal! o endif let @"=reg_bak silent! %s/\(\S\)$/\1\r/e else normal! Go endif silent! g/\S/,/^\s*$/j silent! %s/\s\+$//e normal! `a endfunction " Non-GUI setting if !has('gui_running') " English messages only language messages en " Do not increase the window width in the taglist.vim plugin if has('eval') let Tlist_Inc_Winwidth=0 endif " Set text-mode menu if has('wildmenu') set wildmenu set cpoptions-=< set wildcharm= nmap :emenu imap :emenu endif " Change encoding according to the current console code page if &termencoding != '' && &termencoding != &encoding let &encoding=&termencoding let &fileencodings='ucs-bom,utf-8,' . &encoding endif endif " Display window width and height in GUI if has('gui_running') && has('statusline') let &statusline=substitute( \&statusline, '%=', '%=%{winwidth(0)}x%{winheight(0)} ', '') set laststatus=2 endif " Set up language and font in GUI if has('gui_running') && has('multi_byte') function! UTF8_East() exec 'language messages ' . s:lang_east . '.UTF-8' set ambiwidth=double set encoding=utf-8 let s:utf8_east_mode=1 endfunction function! UTF8_West() exec 'language messages ' . s:lang_west . '.UTF-8' set ambiwidth=single set encoding=utf-8 let s:utf8_east_mode=0 endfunction function! UTF8_SwitchMode() if exists('b:utf8_east_mode') unlet b:utf8_east_mode endif if s:utf8_east_mode call UTF8_West() call UTF8_SetFont() else call UTF8_East() call UTF8_SetFont() endif endfunction function! UTF8_SetFont() if &encoding != 'utf-8' return endif if &fileencoding == 'cp936' || \&fileencoding == 'gbk' || \&fileencoding == 'euc-cn' let s:font_east=s:font_schinese elseif &fileencoding == 'cp950' || \&fileencoding == 'big5' || \&fileencoding == 'euc-tw' let s:font_east=s:font_tchinese elseif &fileencoding == 'cp932' || \&fileencoding == 'sjis' || \&fileencoding == 'euc-jp' let s:font_east=s:font_japanese elseif &fileencoding == 'cp949' || \&fileencoding == 'euc-kr' let s:font_east=s:font_korean endif if !exists('s:use_directx') && ( \(g:legacy_encoding == 'cp936' && s:font_east == s:font_schinese) || \(g:legacy_encoding == 'cp950' && s:font_east == s:font_tchinese) || \(g:legacy_encoding == 'cp932' && s:font_east == s:font_japanese) || \(g:legacy_encoding == 'cp949' && s:font_east == s:font_korean)) " The system default (bitmap) font, FixedSys, can display ASCII " characters slightly better than the Far East fonts. However, " when DirectX is enabled, only TrueType fonts can be used, the " automatic font mapping is not quite predictable, and the result " looks small and bad to me. So we will stick to the Far East " font then. let s:font_east='' endif if exists('b:utf8_east_mode') && s:utf8_east_mode != b:utf8_east_mode let s:utf8_east_mode=b:utf8_east_mode let &ambiwidth=(s:utf8_east_mode ? 'double' : 'single') endif if s:utf8_east_mode exec 'set guifont=' . s:font_east set guifontwide= else exec 'set guifont=' . s:font_west exec 'set guifontwide=' . s:font_east endif endfunction function! UTF8_CheckAndSetFont() if &fileencoding == 'cp936' || \&fileencoding == 'gbk' || \&fileencoding == 'euc-cn' || \&fileencoding == 'cp950' || \&fileencoding == 'big5' || \&fileencoding == 'euc-tw' || \&fileencoding == 'cp932' || \&fileencoding == 'sjis' || \&fileencoding == 'euc-jp' || \&fileencoding == 'cp949' || \&fileencoding == 'euc-kr' let b:utf8_east_mode=1 elseif &fileencoding == 'latin1' || \&fileencoding =~ 'iso-8859-.*' || \&fileencoding =~ 'koi8-.' || \&fileencoding == 'macroman' || \&fileencoding == 'cp437' || \&fileencoding == 'cp850' || \&fileencoding =~ 'cp125.' || \&fileencoding =~ 'windows-125.' let b:utf8_east_mode=0 endif if exists('b:utf8_east_mode') && \(b:utf8_east_mode || (!b:utf8_east_mode && s:utf8_east_mode)) && \((s:utf8_east_mode && &guifont == s:font_east) || \(!s:utf8_east_mode && &guifont == s:font_west)) call UTF8_SetFont() endif endfunction function UTF8_SwitchDejaVu() if s:font_west =~ '^Courier_New' let s:font_west='DejaVu_Sans_Mono:h10:cDEFAULT' else let s:font_west='Courier_New:h10:cDEFAULT' endif call UTF8_SetFont() endfunction " Rebuild the menu to make the translations display correctly " -------------------------------------------------------------------- " Uncomment the following code if all of the following conditions " hold: " 1) Unicode support is wanted (enabled by default for gVim in this " _vimrc); " 2) The libintl.dll shipped with gVim for Windows is not updated " with a new one that supports encoding conversion (see also " for issues with this approach); " 3) The environment variable LANG is not manually set to something " like "zh_CN.UTF-8", and the default language is not ASCII-based " (English). " The reason why the code is not enabled by default is because it can " interfere with the localization of menus created by plug-ins. " -------------------------------------------------------------------- " "if $LANG !~ '\.' && v:lang !~? '^\(C\|en\)\(_\|\.\|$\)' " runtime! delmenu.vim "endif " Fonts let s:font_schinese='NSimSun:h12:cDEFAULT' let s:font_tchinese='MingLiU:h12:cDEFAULT' let s:font_japanese='MS_Gothic:h12:cDEFAULT' let s:font_korean='GulimChe:h12:cDEFAULT' if legacy_encoding == 'cp936' let s:font_east=s:font_schinese elseif legacy_encoding == 'cp950' let s:font_east=s:font_tchinese elseif legacy_encoding == 'cp932' let s:font_east=s:font_japanese elseif legacy_encoding == 'cp949' let s:font_east=s:font_korean else let s:font_east=s:font_schinese endif let s:font_west='Courier_New:h10:cDEFAULT' " Extract the current Eastern/Western language settings if v:lang =~? '^\(zh\)\|\(ja\)\|\(ko\)' let s:lang_east=matchstr(v:lang, '^[a-zA-Z_]*\ze\(\.\|$\)') let s:lang_west='en' let s:utf8_east_mode=1 if v:lang=~? '^zh_TW' let s:font_east=s:font_tchinese elseif v:lang=~? '^ja' let s:font_east=s:font_japanese elseif v:lang=~? '^ko' let s:font_east=s:font_korean endif else let s:lang_east='zh_CN' let s:lang_west=matchstr(v:lang, '^[a-zA-Z_]*\ze\(\.\|$\)') let s:utf8_east_mode=0 endif " Set a suitable GUI font and the ambiwidth option if &encoding == 'utf-8' if s:utf8_east_mode && $VIM_USE_DIRECTX == '0' call UTF8_East() else call UTF8_West() endif elseif s:utf8_east_mode exec 'set guifont=' . s:font_east else exec 'set guifont=' . s:font_west endif call UTF8_SetFont() " Key mapping to switch the Eastern/Western UTF-8 mode nmap :call UTF8_SwitchMode() imap :call UTF8_SwitchMode() " Key mapping to switch the use of DejaVu Sans Mono nmap :call UTF8_SwitchDejaVu() imap :call UTF8_SwitchDejaVu() " Key mapping to change the line space nmap :if &linespace == 1 \set linespace=3 \else \set linespace=1 \endif if has('autocmd') " Set the appropriate GUI font according to the fileencoding, but " not if user has manually changed it au BufWinEnter,WinEnter * call UTF8_CheckAndSetFont() endif endif " Key mapping to toggle spelling check if has('syntax') nmap :setlocal spell! imap :setlocal spell! let spellfile_path=FindRuntimeFile('spell/en.' . &encoding . '.add', 'w') if spellfile_path != '' exec 'nmap :sp ' . spellfile_path . '_' endif endif " Common abbreviations iabbrev Br Best regards, iabbrev Btw By the way, iabbrev Yw Yongwei if has('autocmd') function! Timezone() if has('python') python << EOF import time def my_timezone(): is_dst = time.daylight and time.localtime().tm_isdst offset = time.altzone if is_dst else time.timezone (hours, seconds) = divmod(abs(offset), 3600) hours = -hours if offset > 0 else hours minutes = seconds // 60 return '{:+03d}{:02d}'.format(hours, minutes) EOF return ' ' . pyeval('my_timezone()') else return '' endif endfunction function! SetFileEncodings(encodings) let b:my_fileencodings_bak=&fileencodings let &fileencodings=a:encodings endfunction function! RestoreFileEncodings() let &fileencodings=b:my_fileencodings_bak unlet b:my_fileencodings_bak endfunction function! GnuIndent() if &filetype == 'c' || &filetype == 'cpp' setlocal cinoptions=>4,n-2,{2,^-2,:2,=2,g0,h2,p5,t0,+2,(0,u0,w1,m1 setlocal shiftwidth=2 setlocal tabstop=8 endif endfunction function! UpdateLastChangeTime() let last_change_anchor='^\(" Last Change:\s\+\)\d\{4}-\d\{2}-\d\{2} \d\{2}:\d\{2}:\d\{2}\( [-+]\d\{4}\)\?' let last_change_line=search(last_change_anchor, 'n') if last_change_line != 0 let last_change_time=strftime('%Y-%m-%d %H:%M:%S', localtime()) let last_change_text=substitute(getline(last_change_line), last_change_anchor, '\1', '') . last_change_time . Timezone() call setline(last_change_line, last_change_text) endif endfunction function! RemoveTrailingSpace() if $VIM_HATE_SPACE_ERRORS != '0' && \(&filetype == 'c' || &filetype == 'cpp' || &filetype == 'vim') normal! m` silent! :%s/\s\+$//e normal! `` endif endfunction " Set default file encodings to the legacy encoding "exec 'set fileencoding=' . legacy_encoding if legacy_encoding != 'latin1' let &fileencodings=substitute( \&fileencodings, '\', legacy_encoding, '') else let &fileencodings=substitute( \&fileencodings, ',default,', ',', '') endif " Use Google Chrome as the browser (Vim script #293) " let utl_rc_app_browser = 'silent !start ' . glob('~') . '\Local Settings\Application Data\Google\Chrome\Application\chrome.exe %u' " Set the directory to store _vim_mru_files (Vim script #521) let MRU_File=$HOME . '\_vim_mru_files' " Exclude the temporary files from being saved let MRU_Exclude_Files='\\itsalltext\\.*\|\\temp\\.*' " More MRU items let MRU_Max_Entries=30 " Use the legacy encoding for calling system() in VimExplorer let VEConf_systemEncoding=legacy_encoding " Use the legacy encoding for CVS in cvsmenu (Vim script #1245) let CVScmdencoding=legacy_encoding " but the encoding of files in CVS is still UTF-8 let CVSfileencoding='utf-8' " Use C++11 mode for clang-complete (Vim script #3302) let clang_user_options='-std=c++11' " Use automatic encoding detection (Vim script #1708) let $FENCVIEW_TELLENC='tellenc' " See let fencview_autodetect=1 let fencview_auto_patterns='*.c,*.cpp,*.cs,*.h,*.log,*.txt,*.tex,' \.'*.vim,*.htm{l\=},*.asp,' \.'README,CHANGES,INSTALL' let fencview_html_filetypes='html,aspvbs' " File types to use function echoing (Vim script #1735) let EchoFuncLangsUsed=['c', 'cpp'] " Use the standard Leader key let NERDMapleader='c' " Do not use menu for NERD Commenter let NERDMenuMode=0 " Prevent NERD Commenter from complaining about unknown file types let NERDShutUp=1 " Highlight space errors in C/C++ source files (Vim tip #935) if $VIM_HATE_SPACE_ERRORS != '0' let c_space_errors=1 endif " Tune for C highlighting let c_gnu=1 let c_no_curly_error=1 unlet c_comment_strings " Load doxygen syntax file for c/cpp/idl files let load_doxygen_syntax=1 " Use Bitstream Vera Sans Mono as special code font in doxygen, which " is available at " let doxygen_use_bitsream_vera=1 " Let TOhtml output
 and style sheet
  let html_use_css=1

  " Show syntax highlighting attributes of character under cursor (Vim
  " script #383)
  map a :call SyntaxAttr()

  " Automatically find scripts in the autoload directory
  "au FuncUndefined Syn* exec 'runtime autoload/' . expand('') . '.vim'

  " File type related autosetting
  au FileType c,cpp      call UseClangFormat()|
       \setlocal expandtab cinoptions=:0,g0 shiftwidth=4 softtabstop=4 tabstop=4
  au FileType cs         setlocal expandtab shiftwidth=4 softtabstop=4
  au FileType python     setlocal expandtab shiftwidth=4 softtabstop=4
  au FileType diff       setlocal shiftwidth=4 tabstop=4
  au FileType changelog  setlocal textwidth=76
  au FileType cvs        setlocal textwidth=72
  au FileType html,xhtml setlocal indentexpr=
  au FileType mail       setlocal expandtab softtabstop=2 textwidth=70 comments+=fb:* comments-=mb:*
  au FileType markdown   setlocal autoindent expandtab linebreak formatoptions=tcqlmn

  " Detect file encoding based on file type
  au BufReadPre  *.gb               call SetFileEncodings('cp936')
  au BufReadPre  *.big5             call SetFileEncodings('cp950')
  au BufReadPre  *.nfo              call SetFileEncodings('cp437')
  au BufReadPost *.gb,*.big5,*.nfo  call RestoreFileEncodings()

  " Quickly exiting help files
  au BufRead *.txt      if &buftype=='help'|nmap  q c|endif

  " Setting for files following the GNU coding standard
  au BufEnter C:/mingw*             call GnuIndent()

  " Automatically update change time
  au BufWritePre *vimrc,*.vim       call UpdateLastChangeTime()

  " Remove trailing spaces for C/C++ and Vim files
  au BufWritePre *                  call RemoveTrailingSpace()
endif