" Matt Perry's custom vimrc " Available at: http://somewhere.fscked.org " perforce commands command! -nargs=* -complete=file PEdit :!g4 open % command! -nargs=* -complete=file PRevert :!g4 revert % command! -nargs=* -complete=file PDiff :!g4 diff % function! s:CheckOutFile() if filereadable(expand("%")) && ! filewritable(expand("%")) let option = confirm("Readonly file, do you want to checkout from p4?" \, "&Yes\n&No", 1, "Question") if option == 1 PEdit endif edit! endif endfunction au FileChangedRO * nested :call CheckOutFile() " Useful hints: " Comment out a bunch of lines: goto the beginning of the line, do, " d " Complete to next/previous word: and " Maximize the help window: _ " Move to nth column: n| " Open file under cursor: gf " Paste current word in command line: " Convert a dos file to unix: :set ff=unix | :w " Substitute in every buffer without delays: :bufdo :%s/pat/rep/e " Line continuation in source'd files is a backslash on the CONTINUING line " Modelines : specify vim commands at the beginning/end of a file " Selecting stuff: viw, vi(, vi{, etc select words or blocks " Capitalize/Decapitalize selected text: gU or gu " Set color codes just in case we're in a stupid environment if &term =~ "xterm" set t_Co=8 set t_Sf=[3%p1%dm set t_Sb=[4%p1%dm endif if has("syntax") syntax on endif filetype indent on filetype plugin on " SingleMap(modes, lhs, rhs) " map lhs to rhs for the given modes " modes is one or more of: " R=noremap !=map! .=map i=imap n=nmap c=cmap v=vmap function SingleMap(modes, lhs, rhs) let modes = a:modes let sil = "" if match(modes, "s") >= 0 let sil = "" endif let mapper = "map" if match(modes, "R") >= 0 let mapper = "noremap" endif if match(modes, '!') >= 0 exe mapper . "!" sil a:lhs a:rhs endif if match(modes, '\.') >= 0 exe mapper sil a:lhs a:rhs endif if match(modes, 'i') >= 0 exe "i" . mapper sil a:lhs a:rhs endif if match(modes, 'n') >= 0 exe "n" . mapper sil a:lhs a:rhs endif if match(modes, 'c') >= 0 exe "c" . mapper sil a:lhs a:rhs endif if match(modes, 'v') >= 0 exe "v" . mapper sil a:lhs a:rhs endif endfunction " MultiMap(lhs, modes1, rhs1 [, modes2, rhs2 ...]) " maps lhs to rhs1 for modes1, and to rhs2 for modes2, etc " see SingleMap for what modes are command -nargs=+ MultiMap call MultiMap() function MultiMap(lhs, ...) let idx = 1 while idx+1 <= a:0 exe "let modes = a:" . idx exe "let rhs = a:" . (idx+1) call SingleMap(modes, a:lhs, rhs) let idx = idx + 2 endwhile endfunction " C-{ is defined to C-o in insert but nothing in normal, so I can have the " same mapping for normal and insert commands, ie: " imap :echo 'hi' " nmap :echo 'hi' MultiMap Rn Ri " CCommentFormat() " add *'s to each line in a multiline C-style comment. MultiMap nis :CCFormat command CCFormat call CCommentFormat() function CCommentFormat() let oldcol = col(".") let oldline = line(".") " set the cursor to the beginning of the comment if searchpair("/\\*", "", "\\*/", "bW") <= 0 return endif exec "normal 0f*" let col = virtcol('.') while 1 if getline(line(".")) !~ "^[ \t/]*\\*" exec "normal " . col . "|i*" endif if getline(line(".")) =~ "\\*/" break endif exec "normal j" endwhile call cursor(oldline, oldcol) endfunction " MakeMain() " quick command to make a main function command MkMain call MakeMain() function MakeMain() call append(0, "#include ") call append(1, "using namespace std;") call append(2, "") call append(3, "int main()") call append(4, "{") call append(5, "}") endfunction " MakeIncGuard() " put an include guard (#ifndef X_H #define X_H #endif) around a header MultiMap ni :MkGuard command MkGuard call MakeIncGuard() function MakeIncGuard() let basename = substitute(bufname(""), '.*/', '', '') let guard = '_' . substitute(toupper(basename), '\.', '_', "g") call append(0, "#ifndef " . guard) call append(1, "#define " . guard) call append(line("$"), "#endif // header guard") endfunction " MakeLinkTag() " make a link out of a word MultiMap nis :MkLink command MkLink call MakeLinkTag() function MakeLinkTag() let word = substitute(getline('.'), '\(^.* \|^\)\([^ ]*\V' . expand('') . '\m[^ ]*\)\( .*$\|$\)', '\2', "I") let line = substitute(getline('.'), word, '' . word . '', "I") call setline(line('.'), line) endfunction " doesn't quite work yet "MultiMap nis :SmartLeft command SmartLeft call SmartLeft() function SmartLeft() let ch1 = getline('.')[col('.')-1] normal h let ch2 = getline('.')[col('.')-1] if ch1 == ' ' && ch2 == ' ' call search('[^ ]', 'b') normal l endif endfunction " doesn't quite work yet "MultiMap nis :SmartRight command SmartRight call SmartRight() function SmartRight() let ch1 = getline('.')[col('.')-1] normal l let ch2 = getline('.')[col('.')-1] if ch1 == ' ' && ch2 == ' ' normal w endif endfunction " CFoldBrace() " fold function. to use, ':set foldexpr=CFoldBrace()' " folds from { to }, except it includes the line before the { function CFoldBrace() if indent(v:lnum) != 0 return -1 endif if getline(v:lnum) =~ '{' && getline(v:lnum) !~ '}' return 1 endif if getline(v:lnum+1)[0] == '{' && getline(v:lnum+1) !~ '}' return 1 endif if getline(v:lnum)[0] == '}' return '<1' endif return -1 endfunction " MyFoldText() " FirstLine---[## lines] " instead of " +-- (## lines) FirstLine set foldtext=MyFoldText() function MyFoldText() let nlines = v:foldend - v:foldstart + 1 return getline(v:foldstart) . '---[' . nlines . ' lines]' endfunction "command AF exec "write | !autofunc.awk " . bufname("") command -range CC exec . "put! ='/*' | " . . "put ='*/'" " bash style text editing MultiMap R!. MultiMap R!. MultiMap ni " remap the to something else MultiMap Rn " other inoremap x:exe "norm! ky0jPD" MultiMap ni "_dd v "_d MultiMap Y Rn y$ MultiMap Ri MultiMap n == MultiMap Rn i MultiMap Rv I MultiMap nis :nohl MultiMap i Rn :set\ nopastei MultiMap I Rn :set\ pastei MultiMap ; n :set\ invwrap:set\ wrap? MultiMap ni zM MultiMap ni zR MultiMap ni zO MultiMap ni zC MultiMap i ///< " get rid of the annoying Ex mode mapping MultiMap Q ns " compact a bunch of spaces into a single one MultiMap nis :s/\ \ */\ /g:nohl " Remap put-visual-mode to always paste from register 0, so multiple pastes " use the same original buffer. MultiMap p Rv "0p " a.vim plugin: switch from .c to .h and vice versa MultiMap nis :A " easy buffer flipping " Key: n\ -> goto buffer n nnoremap \ " Key: Fn -> goto buffer n " Key: Shift+Fn -> goto buffer n+10 " Key: Ctrl+Fn -> goto buffer n+20 let s:i = 1 while s:i <= 10 exe "MultiMap ni :b" . s:i . "" exe "MultiMap ni :b" . (s:i+10) . "" exe "MultiMap ni :b" . (s:i+20) . "" let s:i = s:i + 1 endwhile " Key: \[a-z] -> goto buffer [1-26] " Key: \[A-Z] -> goto buffer [27-52] "let s:i = 1 "while s:i <= 26 " exe "MultiMap \\" . nr2char(s:i + char2nr('a')-1) . " n :b" . s:i . "" " exe "MultiMap \\" . nr2char(s:i + char2nr('A')-1) . " n :b" . (s:i+26) . "" " let s:i = s:i + 1 "endwhile " map ^[x to for 'a' <= i <= 'z' let s:i = char2nr('a') while s:i <= char2nr('z') exe "MultiMap " . nr2char(s:i) . " ni " let s:i = s:i + 1 endwhile " I will not use abbrevs abbrev #i #include abbrev #d #define " Autocommands au FileType c,cpp,java,perl,awk set foldexpr=CFoldBrace() au FileType c,cpp,java,perl,awk MultiMap {} Ri {}.== au FileType php,html set wrapmargin=0 au FileType php,html set shiftwidth=2 au FileType html let g:html_indent_tags = substitute(g:html_indent_tags, '\(html\|body\|head\)\\|', '', 'g') au BufNewFile *.h,*.hh MkGuard "au BufReadPost * silent! %s/[\r \t]\+$//e au BufReadPost * if &filetype != "help" | silent! exe "norm g'\"" | endif au BufEnter *.sh if getline(1) == "" | call setline(1, '#!/bin/sh') | endif au BufWritePost * if getline(1) =~ "^#!/" | silent exe "!chmod a+x >& /dev/null" | endif au BufEnter SConscript* set filetype=python augroup mySyntaxGroup au FileType python syn match pythonColon /:/ augroup END au BufEnter * call PostCommands() function PostCommands() hi Comment ctermfg=Cyan guifg=Cyan gui=bold hi Function ctermfg=Yellow guifg=Yellow gui=bold hi PreProc ctermfg=Blue guifg=Blue gui=bold hi Constant ctermfg=Magenta guifg=Magenta gui=bold " Silly gui. I have to redo my auto group, because the GUI messes it up " somehow. if has("gui_running") doautoall mySyntaxGroup FileType endif endfunction hi pythonColon ctermfg=Red guifg=Red gui=bold hi Folded ctermbg=DarkBlue ctermfg=Grey guibg=DarkBlue guifg=White hi Normal guibg=Black guifg=White " my font if v:version >= 602 set guifont=Luxi\ Mono\ 9 "else " set guifont=-misc-fixed-medium-r-normal-*-*-130-*-*-c-*-iso10646-1 endif set title if has("gui_running") set titlestring=[gvim\ %n]\ %f%(\ (%R%M)%) set winaltkeys=no " toolbar and menu are useless set guioptions-=m set guioptions-=T set lines=999 else set titlestring=[vim\ %n]\ %f%(\ (%R%M)%) if &term == "screen" set t_ts=_ set t_fs=\\ endif endif " favorite settings set nocompatible set autowrite set background=dark set backspace=2 set noerrorbells set esckeys set hlsearch set ignorecase set smartcase set incsearch set joinspaces set list set listchars=tab:\ \ ,extends:$ set magic set modeline set modelines=1 set ruler set showcmd set showmatch set showmode set wrapmargin=2 set nowritebackup set shiftwidth=4 set tabstop=4 set softtabstop=0 set smarttab set expandtab set nowrap set wildmode=list:longest set whichwrap=b,s,h,l,<,>,[,] set foldnestmax=1 "set foldmethod=expr set foldminlines=3 set foldlevel=1 set visualbell set viminfo+=f1,% set sessionoptions-=options set hidden set history=1000 "set timeoutlen=10 " cindent options: " g0=no indenting for public:/etc " :0=no indent for case x: " l1=align braces with case label, not with braces " (0=don't indent extra within an unclosed paren " t0=don't indent function prototypes if on multiple lines set cinoptions=g0,:0,l1,(0,t0 " google options set shiftwidth=2 set tabstop=8 set softtabstop=2 "set tags+=~/.vim/systags " Plugin settings " a.vim: when switching from .h files, check in this order. let g:alternateExtensions_h = "cc,cpp,cxx,c" " manpageview.vim: open manpage in current window let g:manpageview_winopen = "reuse" if filereadable('.vimextra') source .vimextra endif " fun with braces " at the end so as not to mess up sytax highlighting. MultiMap [] Rv `>a]` MultiMap {} Rv `>a}`=a{ MultiMap () Rv `>a)`