bairui

vimrc_common

Jul 17th, 2011
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VIM 9.83 KB | None | 0 0
  1. " File: ~/.vim/vimrc_common
  2. " Author: Barry Arthur
  3.  
  4. set nocompatible               " NEVER change this! Use Vim mode, not vi mode
  5. set rtp+=/usr/share/vim/vimfiles
  6. call pathogen#infect('bundle/shared')
  7. filetype plugin indent on      " Enable automatic settings based on file type
  8. syntax on                      " Enable color syntax highlighting
  9.  
  10. source $HOME/.vim/vimrc_${USER}_pre
  11.  
  12. """"""""""
  13. " Options
  14. """"""""""
  15. " Use :help 'option (inclduing the ' character) to learn more about each one.
  16. "
  17. " Buffer (File) Options:
  18. set hidden                     " Edit multiple unsaved files at the same time
  19. set confirm                    " Prompt to save unsaved changes when exiting
  20. " Keep various histories between edits
  21. set viminfo='1000,f1,<500,:100,/100
  22.  
  23. set shortmess=atI              " Limit Vim's "hit-enter" messages
  24.  
  25. " Search Options:
  26. set hlsearch                   " Highlight searches. See below for more.
  27. set ignorecase                 " Do case insensitive matching...
  28. set smartcase                  " ...except when using capital letters
  29. set incsearch                  " Incremental search
  30.  
  31. " Insert (Edit) Options:
  32. set backspace=indent,eol,start " Better handling of backspace key
  33. set autoindent                 " Sane indenting when filetype not recognised
  34. set nostartofline              " Emulate typical editor navigation behaviour
  35. set nopaste                    " Start in normal (non-paste) mode
  36. set pastetoggle=<f11>          " Use <F11> to toggle between 'paste' and 'nopaste'
  37.  
  38. " Status / Command Line Options:
  39. set wildmenu                   " Better commandline completion
  40. set wildmode=list:full         " Expand match on first Tab complete
  41. set showcmd                    " Show (partial) command in status line.
  42. set laststatus=2               " Always show a status line
  43. set cmdheight=2                " Prevent "Press Enter" message after most commands
  44. " Statusline set below after Function definitions
  45.  
  46. " Interface Options:
  47. set number                     " Display line numbers at left of screen
  48. set visualbell                 " Flash the screen instead of beeping on errors
  49. set t_vb=                      " And then disable even the flashing
  50. set mouse=a                    " Enable mouse usage (all modes) in terminals
  51. " Quickly time out on keycodes, but never time out on mappings
  52. set notimeout ttimeout ttimeoutlen=200
  53. set updatetime=300             " Preview window timeout
  54. set previewheight=3
  55.  
  56. " Indentation Options:
  57. set shiftwidth=2               " Number of spaces for each indent level
  58. set softtabstop=2              " Even when using <Tab>'s
  59. set expandtab                  " When pressing <Tab>, replace with spaces
  60.  
  61. """"""""""""
  62. " Functions
  63. """"""""""""
  64.  
  65. function! Var(var, default_val)
  66.   if exists(a:var)
  67.     return eval(a:var)
  68.   else
  69.     return a:default_val
  70.   endif
  71. endfunction
  72.  
  73. function! ToggleRedundantSpaces()
  74.   if exists('b:spacecheck') == 0
  75.     let b:spacecheck = 1
  76.   endif
  77.   if b:spacecheck == 1
  78.     hi clear RedundantSpaces
  79.     let b:spacecheck=0
  80.   else
  81.     hi RedundantSpaces ctermbg=red guibg=red
  82.     let b:spacecheck=1
  83.   endif
  84. endfunction
  85.  
  86. " Use current filetype's comment leader to comment a string
  87. function! CommentString(...)
  88.   let comment = ''
  89.   if a:0 != 0
  90.     let comment = a:1
  91.   endif
  92.   return substitute(&commentstring, '%s.*', '', '') . ' ' . comment
  93. endfunction
  94.  
  95. " Execute current line
  96. function! ExecuteLine()
  97.   let save_reg = @@
  98.   normal ^y$
  99.   exe @@
  100.   let @@ = save_reg
  101. endfunction
  102.  
  103. " TODO: Breaks insert mode!
  104. function! CharCount()
  105.   redir => l:gcount
  106.   silent exe "normal! g\<c-g>"
  107.   redir END
  108.   let l:type = matchstr(l:gcount, '\(Char\|Byte\)')
  109.   return substitute(l:gcount, '.*' . l:type . '\s*\(\d\+\)\s*of\s*\(\d\+\).*', 'Char \1 of \2', '')
  110. endfunction
  111.  
  112. " Show all matches within loaded scripts
  113. let g:scriptnames = []
  114. function! ScriptnamesList()
  115.   let l:sn = ''
  116.   redir => l:sn
  117.   silent scriptnames
  118.   redir END
  119.   let g:scriptnames = split(l:sn, "\n")
  120.   return g:scriptnames
  121. endfunction
  122. command! -nargs=1 SNMatch echo join(filter(ScriptnamesList(), 'v:val =~ "<args>"'), "\n")
  123.  
  124. """""""""""""""""""""""""""""
  125. " Options requiring Functions
  126. """""""""""""""""""""""""""""
  127.  
  128. "set statusline=%f%m%r%h%w\ [%n:%{&ff}/%Y]%=[0x\%04.4B][%03v][%p%%\ line\ %l\ of\ %L,\ %{CharCount()}]
  129. set statusline=%f%m%r%h%w\ [%n:%{&ff}/%Y]%=[0x\%04.4B][%03v][%p%%\ line\ %l\ of\ %L]
  130.  
  131. """""""
  132. " Maps
  133. """""""
  134.  
  135. "--------------
  136. " Function keys
  137. "--------------
  138.  
  139. " F1 to be a context sensitive keyword-under-cursor lookup
  140. " Deliberately left the <CR> off the end to allow modification at call time
  141. nnoremap <F1> :help <c-r><c-w>
  142.  
  143. " F2 to toggle spelling
  144. nnoremap <F2> :set invspell<CR>
  145.  
  146. " TODO: Change this to TagBar
  147. " F6 to Toggle the Tag List
  148. nnoremap <F6> :TlistToggle<CR>
  149.  
  150. " F7 to visually select word under cursor
  151. nnoremap <silent> <F7> :let @*=':help '.expand('<cword>')<CR>
  152.  
  153. " F8 to highlight all occurrences of word under cursor
  154. nnoremap <silent> <F8> :let @/='\<'.expand('<cword>').'\>'<bar>set hls<CR>
  155.  
  156. nnoremap <silent> <F9> :tabn 1 <bar> help function-list<CR>
  157.  
  158. " TODO: Drop Project?
  159. " F12 to toggle project window
  160. nnoremap <silent> <F12> <Plug>ToggleProject
  161.  
  162. "------------
  163. " Leader Maps
  164. "------------
  165.  
  166. " TODO: use g:user_leader and g:user_localleader defined in $HOME/.vim/vimrc_user_pre
  167. " custom mapleader
  168. let mapleader = ","
  169. let maplocalleader = ","
  170.  
  171. " quick buffer management
  172. nnoremap <leader>l :ls<CR>:b<space>
  173.  
  174. " highlight redundant spaces at end of line and tabs
  175. highlight RedundantSpaces ctermbg=red guibg=red
  176. match RedundantSpaces /\s\+$\|\t\+/
  177.  
  178. " toggle RedundantSpaces matching
  179. nnoremap <silent> <leader>S :call ToggleRedundantSpaces()<CR>
  180.  
  181. " toggle list mode
  182. nnoremap <silent> <leader>L :set list!<bar>:set list?<CR>
  183.  
  184. " Append current datetime stamp
  185. nnoremap <leader>d a<c-r>=strftime('%d %b %Y')<CR><Esc>
  186.  
  187. " Execute current line
  188. nnoremap <silent> <leader>e :call ExecuteLine()<CR>
  189.  
  190. " Toggle relative and normal line numbers
  191. nnoremap <silent> <leader>n :if &relativenumber == 1 \| setlocal number \| else \| setlocal relativenumber \| endif<CR>
  192.  
  193. "-----------
  194. " Other Maps
  195. "-----------
  196.  
  197. " temporarily clear highlighting
  198. nnoremap <silent> <C-l> :noh<CR><C-l>
  199.  
  200. " use jj as an alternate <esc>
  201. inoremap jj <esc>
  202.  
  203. " paragraph formatting
  204. nnoremap Q gq}
  205.  
  206. " Use * in visual mode to search for visual selection
  207. vnoremap * <Esc>/<c-r>=escape(@*, '\/.*$^~[]')<CR><CR>
  208.  
  209. " visual up,down
  210. nnoremap <Up> gk
  211. nnoremap <Down> gj
  212.  
  213. " move between windows
  214. nnoremap <C-Up> <C-w>k
  215. nnoremap <C-k> <C-w>k
  216. nnoremap <C-Down> <C-w>j
  217. nnoremap <C-j> <C-w>j
  218. nnoremap <C-Left> <C-w>h
  219. nnoremap <C-Right> <C-w>l
  220. nnoremap <C-p> <C-w>p
  221.  
  222. " toggle search highlighting
  223. nnoremap <C-Bslash>       :set hls!<bar>:set hls?<CR>
  224. inoremap <C-Bslash>  <Esc>:set hls!<bar>:set hls?<CR>a
  225.  
  226. " change local cd
  227. nnoremap <leader>cd :lcd %:h<cr>:pwd<cr>
  228.  
  229. """"""""""""""""
  230. " Auto Commands
  231. """"""""""""""""
  232.  
  233. " TODO: Remember wtf I used this for. :-/
  234. function! LoadBufOption(opts)
  235.   for mop in keys(a:opts)
  236.     echo mop a:opts[mop]
  237.     exe "let b:".mop."='".a:opts[mop]."'"
  238.   endfor
  239. endfunction
  240.  
  241. if has("autocmd")
  242.   augroup filetype
  243.     au!
  244.  
  245.     " Jump to last-known-position when editing files
  246.     au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
  247.  
  248.     " My file type maps
  249.     au BufRead,BufNewFile *.rem         set ft=remind
  250.     au BufRead .conkyrc                 set filetype=conkyrc
  251.  
  252.     " Outliner files use tabs, so disable error highlight
  253.     au BufRead,BufNewFile *.otl         set noet | call ToggleRedundantSpaces()
  254.  
  255.     " text files are in asciidoc format
  256.     au BufNewFile *.txt                 set filetype=asciidoc | so ~/.vim/scripts/asciidoc.vim
  257.  
  258.     " automatically fold top-most comment block in C files
  259.     au BufRead *.c set fdm=manual | silent 1/^\/\*/,/^\s*\*\//fo
  260.  
  261.     " reformatter for xml type files
  262.     :au Filetype html,xml,xsl           set equalprg="hindent -cs -i2 -t0 test.xml | sed -e '/^\s*$/d'"
  263.  
  264.     " lyx
  265.     au BufRead *.lyx                    set syntax=lyx foldmethod=syntax foldcolumn=3
  266.     au BufRead *.lyx                    syntax sync fromstart
  267.  
  268.     " Dokuwiki
  269.     au BufRead,BufNewFile *.doku        set filetype=dokuwiki
  270.  
  271.     " Ruby Omnicomplete
  272.     "au FileType ruby,eruby              so ~/.vim/scripts/ruby.vim
  273.     "au BufWritePost *.rb                silent !ctags -R
  274.  
  275.     " Help files
  276.     au FileType help                    so ~/.vim/scripts/help.vim
  277.  
  278.     " Omni Completion Based on Syntax
  279.     if has("autocmd") && exists("+omnifunc")
  280.       autocmd Filetype *
  281.             \       if &omnifunc == "" |
  282.             \               setlocal omnifunc=syntaxcomplete#Complete |
  283.             \       endif
  284.     endif
  285.  
  286.     " TODO: is this redundant with the qxolotl plugin?
  287.     " Remember which window we came from when using a QuickFix command
  288.     au BufReadPost quickfix let w:quickfix_source_win = winnr('#')
  289.  
  290.   augroup END
  291. endif
  292.  
  293. """""""""""
  294. " Commands
  295. """""""""""
  296.  
  297. " Help
  298. command! -nargs=* -complete=help HG helpgrep <args>
  299.  
  300. " Better Find-in-File Support
  301. cabbrev lvim
  302.       \ lvim /\<lt><C-R><C-W>\>/gj
  303.       \ *<C-R>=(expand("%:e")=="" ? "" : ".".expand("%:e"))<CR>
  304.       \ <Bar> lw
  305.       \ <C-Left><C-Left><C-Left>
  306.  
  307. " Show all matches in the runtimepath
  308. command! -nargs=1 RTPMatch echo join(filter(split(&rtp, ','), 'v:val =~ "<args>"'), "\n")
  309.  
  310. " Better Diffing
  311. command DiffOrig let g:diffline = line('.') | vert new | set bt=nofile | r # | 0d_ | diffthis | :exe "norm! ".g:diffline."G" | wincmd p | diffthis | wincmd p
  312. nnoremap <Leader>do :DiffOrig<cr>
  313. nnoremap <leader>dc :q<cr>:diffoff<cr>:exe "norm! ".g:diffline."G"<cr>
  314.  
  315. """"""""""
  316. " GVim
  317. """"""""""
  318. if has("gui_running")
  319.   set t_vb=
  320. endif
  321.  
  322. """"""""""
  323. " Plugins
  324. """"""""""
  325.  
  326. " Matchit
  327. runtime macros/matchit.vim
  328.  
  329. " Vim Outliner
  330. let g:otl_bold_headers = 0
  331.  
  332. source $HOME/.vim/vimrc_${USER}_post
Advertisement
Add Comment
Please, Sign In to add comment