" Rainux's .vimrc file " " Maintainer: Rainux " Last change: 2007 Oct 03 " " To use it, copy it to " for Unix and OS/2: ~/.vimrc " for Amiga: s:.vimrc " for MS-DOS and Win32: $HOME\.vimrc or $VIM\_vimrc " for OpenVMS: sys$login:.vimrc " ---------------------------------------------------------------------------- " Multi-encoding setting, MUST BE IN THE BEGINNING OF .vimrc! " function! s:MultiEncodingSetting() if has("multi_byte") " When 'fileencodings' starts with 'ucs-bom', don't do this manually "set bomb set fileencodings=ucs-bom,utf-8,chinese " CJK environment detection and corresponding setting if v:lang =~ "^zh_CN" " Simplified Chinese, on Unix euc-cn, on MS-Windows cp936 set encoding=chinese set termencoding=chinese if &fileencoding == '' set fileencoding=chinese endif elseif v:lang =~ "^zh_TW" " Traditional Chinese, on Unix euc-tw, on MS-Windows cp950 set encoding=taiwan set termencoding=taiwan if &fileencoding == '' set fileencoding=taiwan endif elseif v:lang =~ "^ja_JP" " Japanese, on Unix euc-jp, on MS-Windows cp932 set encoding=japan set termencoding=japan if &fileencoding == '' set fileencoding=japan endif elseif v:lang =~ "^ko" " Korean on Unix euc-kr, on MS-Windows cp949 set encoding=korea set termencoding=korea if &fileencoding == '' set fileencoding=korea endif endif " Detect UTF-8 locale, and override CJK setting if needed if v:lang =~ "utf8$" || v:lang =~ "UTF-8$" set encoding=utf-8 set fileencoding=utf-8 set ambiwidth=double endif else echoerr 'Sorry, this version of (g)Vim was not compiled with "multi_byte"' endif endfunction " ---------------------------------------------------------------------------- " Initialization " Commands only execute in Vim initialization " function! s:VimInit() " Use Vim settings, rather then Vi settings (much better!). " This must be first, because it changes other options as a side effect. set nocompatible call s:MultiEncodingSetting() source $VIMRUNTIME/mswin.vim behave xterm " Restore CTRL-A to increase number instead of Select All unmap set shiftwidth=4 set softtabstop=4 "set tags+=~/.vim/systags endfunction " When Vim is starting, the winpos information is not available if getwinposx() == -1 call s:VimInit() endif " ---------------------------------------------------------------------------- " Primary settings " " Backup only not in VMS and backup directory present if has("vms") set nobackup " do not keep a backup file, use versions instead else if isdirectory("./.backup") || isdirectory($HOME . "/.backup") set backup set backupdir=./.backup,~/.backup else set nobackup endif endif " Switch syntax highlighting on, when the terminal has colors " Also switch on highlighting the last used search pattern. if &t_Co > 2 || has("gui_running") syntax on set hlsearch endif " Only do this part when compiled with support for autocommands. if has("autocmd") " Enable file type detection. " Use the default filetype settings, so that mail gets 'tw' set to 72, " 'cindent' is on in C files, etc. " Also load indent files, to automatically do language-dependent indenting. filetype plugin indent on " Put these in an autocmd group, so that we can delete them easily. augroup vimrcEx au! " For all text files set 'textwidth' to 78 characters. autocmd FileType text setlocal textwidth=78 autocmd FileType c compiler gcc autocmd FileType css setlocal noexpandtab softtabstop=8 shiftwidth=8 autocmd FileType delphi setlocal shiftwidth=2 softtabstop=2 autocmd FileType delphi compiler borland autocmd FileType html setlocal softtabstop=2 shiftwidth=2 autocmd FileType nsis setlocal shiftwidth=2 softtabstop=2 autocmd FileType php compiler php autocmd FileType ruby,eruby set softtabstop=2 shiftwidth=2 omnifunc=rubycomplete#Complete " Change to directory of current file automatically when current file is not " on remote server nor inside an archive file like .zip/.tgz autocmd BufEnter * \ if bufname('%') !~ '::\|//' | \ lcd %:p:h | \ endif | " Apply file template if it exists autocmd BufNewFile * \ set ff=unix | \ let s:Template = expand('~/.template/template.' . substitute(bufname("%"), '.\{-\}\.*\([^.]*\)$', '\1', '')) | \ if filereadable(s:Template) | \ execute "0read " . s:Template | \ normal Gdd | \ endif | " When editing a file, always jump to the last known cursor position. " Don't do it when the position is invalid or when inside an event handler " (happens when dropping a file on gvim). autocmd BufReadPost * \ if line("'\"") > 0 && line("'\"") <= line("$") | \ execute "normal g`\"" | \ endif | autocmd BufLeave * \ setlocal expandtab softtabstop=4 shiftwidth=4 | augroup END else set autoindent " always set autoindenting on endif " has("autocmd") " ---------------------------------------------------------------------------- " My customizations " " Set options set autoindent set backspace=indent,eol,start set completeopt=menuone,preview set directory=~/tmp set expandtab set grepprg=grep\ -nH\ $* set guioptions=gmrLtT set helplang=CN set history=50 set ignorecase smartcase smartcase set incsearch set laststatus=2 set linebreak set list if v:version < 700 set listchars=tab:>-,trail:- else set listchars=tab:>-,trail:-,nbsp:% endif set number set ruler set sbr=>> set shellslash set showcmd set smartindent set ve=block set viminfo=!,'1000,<100,c,f1,h,s10,rA:,rB:,n~/.viminfo set wildmenu set diffopt=filler,vertical set diffexpr=MyDiff() function! MyDiff() let opt = ' -a --binary ' if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif let arg1 = v:fname_in if arg1 =~ ' ' | let arg1 = '"' . arg1 . '"' | endif let arg2 = v:fname_new if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif let arg3 = v:fname_out if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif let diffprg = $VIMRUNTIME . "/diff" if !executable(diffprg) let diffprg = "diff" endif silent execute '!' . diffprg . opt . arg1 . ' ' . arg2 . ' > ' . arg3 endfunction " Force read viminfo try rviminfo ~/.viminfo catch endtry " GUI version settings if has("gui_running") if has("gui_gtk2") && v:version >= 700 " Define a font list for GTK2 with corresponding winsize and winpos arguments list let s:GuiFontList = [ \"Monaco\\ 11", \"Bitstream\\ Vera\\ Sans\\ Mono\\ 11", \"DejaVu\\ Sans\\ Mono\\ 11", \"Inconsolat1\\ 11", \] let s:WinSizeList = [ \[160, 45], \[160, 45], \[160, 45], \[160, 45] \] let s:WinPosList = [ \[9, -4], \[9, -4], \[9, -4], \[9, -4] \] end if has("gui_win32") && v:version >= 700 " Define a font list for Win32 with corresponding winsize and winpos arguments list let s:GuiFontList = [ \"Monaco:h10", \"NSimSun:h11", \"Courier_New:h12", \"Bitstream_Vera_Sans_Mono:h11", \"Fixedsys:h11", \"Lucida_Console:h11", \"Terminal:h12:cGB2312" \] let s:WinSizeList = [ \[256, 64], \[256, 64], \[256, 64], \[256, 64], \[256, 64], \[256, 64], \[256, 64] \] let s:WinPosList = [ \[9, -4], \[9, -4], \[9, -4], \[9, -4], \[9, -4], \[6, -4], \[9, -4] \] end " Set default index to 0 if !exists("g:CUR_FONT_INDEX") || g:CUR_FONT_INDEX < 0 || g:CUR_FONT_INDEX >= len(s:GuiFontList) let g:CUR_FONT_INDEX = 0 endif " Set GUI font by index function! s:SetGuiFont(Index) try execute 'set guifont=' . s:GuiFontList[a:Index] catch return 1 endtry endfunction function! s:SetWinSize(Index) execute 'winsize ' . s:WinSizeList[a:Index][0] . ' ' . \ s:WinSizeList[a:Index][1] endfunction " Set window pos by index function! s:SetWinPos(Index) let WinPosX = s:WinPosList[a:Index][0] let WinPosY = s:WinPosList[a:Index][1] if WinPosX != getwinposx() || WinPosY != getwinposy() execute 'winpos ' . WinPosX . ' ' . WinPosY endif endfunction " Initialize GUI font and window settings call s:SetGuiFont(g:CUR_FONT_INDEX) call s:SetWinSize(g:CUR_FONT_INDEX) " Only effect when reload .vimrc call s:SetWinPos(g:CUR_FONT_INDEX) " Add to some event so they can auto execute when need if has("autocmd") autocmd GUIEnter * call s:SetWinPos(g:CUR_FONT_INDEX) autocmd WinEnter * call s:SetWinPos(g:CUR_FONT_INDEX) endif color desert else " Console version settings set background=dark endif " Customizing some default highlight function! s:CustomHighlight() if !exists("g:colors_name") || g:colors_name == "default" highlight LineNr gui=NONE guifg=#666666 guibg=#e8e8e8 cterm=NONE ctermfg=DarkGray ctermbg=LightGray " Status line highlight User1 gui=bold guifg=#000000 guibg=#f3f3f3 cterm=NONE ctermfg=Black ctermbg=LightGray highlight User2 gui=bold guifg=#888888 guibg=#f3f3f3 cterm=NONE ctermfg=DarkGray ctermbg=LightGray highlight User3 gui=bold guifg=#0000ff guibg=#f3f3f3 cterm=NONE ctermfg=Blue ctermbg=LightGray highlight User4 gui=bold guifg=#ff0000 guibg=#f3f3f3 cterm=NONE ctermfg=Red ctermbg=LightGray endif endfunction call s:CustomHighlight() if has("autocmd") autocmd CursorHold * call s:CustomHighlight() autocmd FocusGained * call s:CustomHighlight() autocmd VimEnter * call s:CustomHighlight() endif " Customizing status line function! CustomStatusLineBufSize() let BufSize = line2byte(line("$") + 1) - 1 if BufSize < 0 let BufSize = 0 endif " Add commas let Remain = BufSize let BufSize = "" while strlen(Remain) > 3 let BufSize = "," . strpart(Remain, strlen(Remain) - 3) . BufSize let Remain = strpart(Remain, 0, strlen(Remain) - 3) endwhile let BufSize = Remain . BufSize let BufSize = BufSize . ' byte' return BufSize endfunction if has("gui_running") execute 'set statusline=%<%1*%f\ %h%m%r%2*\|' . \ '%3*%{&ff}%2*:%3*%{&fenc}%2*:%3*%{&ft}%2*\|%{CustomStatusLineBufSize()}' . \ '%=%b\ 0x%B\ \ \|' . \ '%1*sts%2*:%3*%{&sts}%2*:%1*sw%2*:%3*%{&sw}%2*:' . \ '%1*ts%2*:%3*%{&ts}%2*:%1*tw%2*:%3*%{&tw}%2*\|' . \ '%06(%l%),%03(%v%)\ %1*%4.4P' else execute 'set statusline=%<%1*%f\ %h%m%r%2*\|' . \ '%3*%{&ff}%2*:%3*%{&fenc}%2*:%3*%{&ft}%2*\|%{CustomStatusLineBufSize()}' . \ '%=%b\ 0x%B\ \ ' . \ '%06(%l%),%03(%v%)\ %1*%4.4P' endif " ---------------------------------------------------------------------------- " Key mappings " " Up & Down is display line upward & downward map gk map gj imap a imap a " F2 is Toggle wrap map :call ToggleGuiOption("b"):set wrap! imap a function! s:ToggleGuiOption(option) " If a:option is already set in guioptions, then we want to remove it if match(&guioptions, "\\C" . a:option) > -1 exec "set go-=" . a:option else exec "set go+=" . a:option endif " if has("gui_running") " call s:SetWinPos(g:CUR_FONT_INDEX) " endif endfunction " F3 is Reverse hlsearch map :set hlsearch! imap a " F4 is Toggle Tag List " map :TlistToggle " imap a " F5 is Toggle Mini Buffer Explorer map :TMiniBufExplorer imap a " F6 is Hex view map :%!xxd imap a " F7 is Toggle spell check map :set spell! imap a " F8 is Change GUI font if has("gui_running") map :call ChangeGuiFont(0) map :call ChangeGuiFont(1) imap a imap a function! s:ChangeGuiFont(Inverse) let OldIndex = g:CUR_FONT_INDEX if a:Inverse == 0 let g:CUR_FONT_INDEX = g:CUR_FONT_INDEX + 1 if g:CUR_FONT_INDEX >= len(s:GuiFontList) let g:CUR_FONT_INDEX = 0 endif else let g:CUR_FONT_INDEX = g:CUR_FONT_INDEX - 1 if g:CUR_FONT_INDEX < 0 let g:CUR_FONT_INDEX = len(s:GuiFontList) - 1 endif endif " Here we use &tenc that set above to ensure the system locale if s:SetGuiFont(g:CUR_FONT_INDEX) == 0 call s:SetWinSize(g:CUR_FONT_INDEX) call s:SetWinPos(g:CUR_FONT_INDEX) echo iconv("\rGUI font has changed to \"" . \ s:GuiFontList[g:CUR_FONT_INDEX] . '"', &tenc, &enc) else call s:SetWinSize(OldIndex) call s:SetWinPos(OldIndex) echo iconv("\rError changing GUI font. Maybe selected font \"" . \ s:GuiFontList[g:CUR_FONT_INDEX] . "\" not exists.", &tenc, &enc) endif endfunction endif " F9 is Compile and Run map :call Run() imap a function! s:Run() let ExeFile = substitute(bufname('%'), '\.[^.]\+$', '.exe', '') if filereadable(ExeFile) if has("win32") execute '!.\"' . ExeFile . '"' else execute '!./"' . ExeFile . '"' endif else if has("win32") execute '!.\"' . bufname('%') . '"' else execute '!./"' . bufname('%') . '"' endif endif endfunction " CTRL-F9 is Compile map :make " CTRL-Tab is Next buffer " Mini Buffer Explorer already provided this feature "-------------------------------------------------- " map :bnext! " imap :bnext! " cmap :bnext! "-------------------------------------------------- " CTRL-SHIFT-Tab is Previous buffer " Mini Buffer Explorer already provided this feature " map :bNext! " imap :bNext! " cmap :bNext! " CTRL-H is help for PHP nmap "hyiw:call ShowPHPHelp(@h) function! s:ShowPHPHelp(function) execute '!start "hh.exe" "N:\CompDoc\php_manual_zh\php_manual_zh.chm::/_function.html\#' . a:function endfunction " ,* is Substitute(Replace) nmap ,* :%s// " ,f is format code nmap ,f gg=G:%s/\s\+$//ge " ,c is clean code nmap ,c :%s/\s\+$//ge " Make it easy to update/reload .vimrc nmap ,s :source $HOME/.vimrc nmap ,v :e $HOME/.vimrc " ,> ,< is next or prev error nmap ,> :cnext nmap ,< :cNext " \date \time Insert current date & time nmap \date :call InsertDate(0) nmap \time :call InsertDate(1) function! s:InsertDate(Also_Time) let Fmt = '%x' if a:Also_Time let Fmt .= ' %X' endif let Time = strftime(Fmt) execute 'normal a' . Time endfunction " Don't use Ex mode, use Q for formatting nmap Q gq " Map .. to ->, that's very usefull in PHP OOP " inoremap .. -> " Emacs-style editing on the command-line " start of line :cnoremap " back one character :cnoremap " delete character under cursor :cnoremap " end of line :cnoremap " forward one character :cnoremap " recall newer command-line :cnoremap " recall previous (older) command-line :cnoremap " back one word :cnoremap " forward one word :cnoremap " ---------------------------------------------------------------------------- " Configurations for plugins " " Mini Buffer Explorer configurations let g:miniBufExplSplitBelow = 1 let g:miniBufExplMapWindowNavVim = 1 let g:miniBufExplMapWindowNavArrows = 1 let g:miniBufExplMapCTabSwitchBufs = 1 let g:miniBufExplModSelTarget = 1 " std_c let c_syntax_for_h = 1 let c_C94 = 1 let c_C99 = 1 let c_cpp = 1 let c_warn_8bitchars = 1 let c_warn_multichar = 1 let c_warn_digraph = 1 let c_warn_trigraph = 1 let c_no_octal = 1 let c_comment_strings = 1 let c_comment_numbers = 1 let c_comment_types = 1 let c_comment_date_time = 1 " EnhCommentify let g:EnhCommentifyFirstLineMode = 'yes' let g:EnhCommentifyUseAltKeys = 'yes' let g:EnhCommentifyUseSyntax = 'yes' let g:EnhCommentifyPretty = 'yes' " Twitter let g:twitterusername = 'rainux' let g:twitterpassword = '' let g:twitterencoding = 'gb2312' nmap tp :let g:twitterpassword=inputsecret('password? ') nmap tw :execute 'TwitterStatusUpdate ' . inputdialog('Enter a Twitter status message:') nmap tf :TwitterFriendsTimeline " PHP highlighting let g:php_baselib = 1 let g:php_oldStyle = 1 let g:php_folding = 1 " rubycomplete let g:rubycomplete_buffer_loading = 1 let g:rubycomplete_classes_in_global = 1 let g:rubycomplete_rails = 1 let g:rubycomplete_rails_proactive = 1 " rails.vim " let g:rails_default_database = 'sqlite3' " Rails Tag List " " Central additions (also add the functions below) command! RTlist call CtagAdder("app/models","app/controllers","app/views","public") map :RTlist " Optional, handy TagList settings nnoremap :Tlist inoremap a let Tlist_Compact_Format = 1 let Tlist_File_Fold_Auto_Close = 1 let Tlist_Use_Right_Window = 1 let Tlist_Exit_OnlyWindow = 1 let Tlist_WinWidth = 40 " Function that gets the dirtrees for the provided dirs and feeds " them to the TlAddAddFiles function below function! CtagAdder(...) let index = 1 let s:dir_list = '' while index <= a:0 let s:dir_list = s:dir_list . TlGetDirs(a:{index}) let index = index + 1 endwhile call TlAddAddFiles(s:dir_list) wincmd p exec "normal =" wincmd p endfunc " Adds *.rb, *.rhtml and *.css files to TagList from a given list " of dirs function! TlAddAddFiles(dir_list) let dirlist = a:dir_list let s:olddir = getcwd() while strlen(dirlist) > 0 let curdir = substitute (dirlist, '|.*', "", "") let dirlist = substitute (dirlist, '[^|]*|\?', "", "") exec "cd " . g:base_dir exec "TlistAddFiles " . curdir . "/*.rb" exec "TlistAddFiles " . curdir . "/*.rhtml" exec "TlistAddFiles " . curdir . "/*.css" exec "TlistAddFiles " . curdir . "/*.js" endwhile exec "cd " . s:olddir endfunc " Gets all dirs within a given dir, returns them in a string, " separated by '|''s function! TlGetDirs(start_dir) let s:olddir = getcwd() exec "cd " . g:base_dir . '/' . a:start_dir let dirlist = a:start_dir . '|' let dirlines = glob ('*') let dirlines = substitute (dirlines, "\n", '/', "g") while strlen(dirlines) > 0 let curdir = substitute (dirlines, '/.*', "", "") let dirlines = substitute (dirlines, '[^/]*/\?', "", "") if isdirectory(g:base_dir . '/' . a:start_dir . '/' . curdir) let dirlist = dirlist . TlGetDirs(a:start_dir . '/' . curdir) endif endwhile exec "cd " . s:olddir return dirlist endfunc " ---------------------------------------------------------------------------- " Extra: Automatic update the "Last change" date of this file " function! s:VimrcUpdateLastChange() let LineNr = search('^"\s*Last change:\s\+.*', 'nw') let Line = getline(LineNr) let Saved_Lang = v:lc_time lang time C let Time = strftime('%Y %b %d') execute 'lang time ' . Saved_Lang let Line = substitute(Line, '\(^"\s*Last change:\s\+\)\(.*$\)', '\1' . \ Time, '') call setline(LineNr, Line) endfunction if has("autocmd") autocmd BufWritePre [._]vimrc call s:VimrcUpdateLastChange() endif function! s:UpdateLastChange() let LineNr = search('^.*Last change:\s\+.*', 'nw') let Line = getline(LineNr) let Saved_Lang = v:lc_time lang time C let Time = strftime('%Y %b %d') execute 'lang time ' . Saved_Lang let Line = substitute(Line, '\(^.*Last change:\s\+\)\(.*$\)', '\1' . \ Time, '') call setline(LineNr, Line) endfunction if has("autocmd") "autocmd BufWritePre * call s:UpdateLastChange() endif " vim: sts=2 sw=2