vimrc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. set nocompatible " Don't have to try to be compatible with old vi
  2. set encoding=utf-8
  3. """"""""
  4. "" Plugin Loading with Pathogen
  5. """""""""""""""""""""""""""""""""""""""""""""""""""
  6. call pathogen#infect()
  7. call pathogen#helptags()
  8. """"""""
  9. "" Environment - What kind of machine are we on?
  10. """""""""""""""""""""""""""""""""""""""""""""""""""
  11. if has("unix")
  12. " remove the newline character from the uname command
  13. let s:uname = substitute(system("uname"), "\n", "", "")
  14. endif
  15. """"""""
  16. "" General Behaviours
  17. """""""""""""""""""""""""""""""""""""""""""""""""""
  18. set splitbelow " New splits appear below current window instead of above
  19. set splitright " New splits appear right of current window
  20. set ttyfast " Smooth movement
  21. set ttymouse=xterm2
  22. set mouse=a
  23. set scrolloff=4 " keep distance from top and bottom for current line
  24. set cursorline " ensure that there is a cursor line
  25. " Persistent undo
  26. if has("persistent_undo")
  27. set undofile
  28. set undodir=~/.vimundo
  29. endif
  30. if has("autocmd")
  31. " Enable filetype specific features
  32. filetype plugin indent on
  33. " Clear existing autocmd
  34. autocmd!
  35. " When editing a file, always jump to the last cursor position
  36. " (from Ubuntu's `/etc/vim/vimrc`)
  37. autocmd BufReadPost *
  38. \ if line("'\"") > 0 && line ("'\"") <= line("$") |
  39. \ exe "normal! g'\"" |
  40. \ endif
  41. " Show trailing white space
  42. autocmd InsertEnter * match ExtraWhitespace /\s\+\%#\@<!$/
  43. autocmd InsertLeave * match ExtraWhitespace /\s\+$/
  44. " Source the vimrc file after saving it
  45. autocmd BufWritePost .vimrc source $MYVIMRC
  46. " Things for Quickfix buffers
  47. " Local remaps for all Quickfix buffers
  48. autocmd FileType qf nnoremap <silent> <buffer> q :ccl<CR>:lcl<CR>
  49. autocmd FileType qf nnoremap <silent> <buffer> o <CR>
  50. " Global remaps for all QuickFix buffers
  51. autocmd BufWinEnter quickfix
  52. \ setlocal nocursorline |
  53. \ let g:qfix_win = bufnr("$") |
  54. \ call MapQfPrevNext()
  55. autocmd BufWinLeave *
  56. \ if exists("g:qfix_win") && expand("<abuf>") == g:qfix_win |
  57. \ unlet! g:qfix_win |
  58. \ call UnmapQfPrefNext() |
  59. \ endif
  60. " Open QuickFix window after any grep invocation (Glog and Ggrep)
  61. autocmd QuickFixCmdPost *grep* cwindow |
  62. \ setlocal nocursorline |
  63. \ let g:qfix_win = bufnr("$") |
  64. \ call MapQfPrevNext()
  65. " if the last window is NERDTree, then close Vim
  66. "autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTreeType") && b:NERDTreeType == "primary") | q | endif
  67. "
  68. " Show cursorline only on active window. No need for this now
  69. "autocmd WinEnter * setlocal cursorline
  70. "autocmd WinLeave * setlocal nocursorline
  71. else
  72. set autoindent on
  73. endif
  74. """"""""
  75. "" Tabs and Text Formatting
  76. """"""""""""""""""""""""""""""""""""""""""""""""""""""
  77. set expandtab " change to single spaces
  78. set tabstop=2 " actual tab press distance
  79. set shiftround " indent to nearest tabstops
  80. set shiftwidth=2 " amount to indent with > and <
  81. set smarttab " backspace tabs where appropriate even if spaces
  82. set softtabstop=2 " let backspace delete by indents
  83. set nowrap " do not wrap long lines of text
  84. set backspace=eol,start,indent "backspace over everything
  85. set textwidth=80 " try to keep text within 80 characters
  86. set colorcolumn=+1 " mark out the limits of the textwidth
  87. set listchars=trail:_,tab:>.,eol:$
  88. """"""""
  89. "" UI - Colours
  90. """"""""""""""""""""""""""""""""""""""""""""""""""""""
  91. syntax enable
  92. colorscheme solarized
  93. set t_Co=16
  94. set background=dark
  95. " set colour for folded lines
  96. highlight Folded term=none cterm=none ctermfg=darkred ctermbg=none
  97. " show trailing white space
  98. highlight ExtraWhitespace ctermfg=red ctermbg=red guifg=red guibg=red
  99. " reverse indent guides highlighting
  100. highlight IndentGuidesOdd ctermbg=darkgrey
  101. highlight IndentGuidesEven ctermbg=black
  102. """"""""
  103. "" UI - Numbering
  104. """"""""""""""""""""""""""""""""""""""""""""""""""""""""
  105. set number " show line numbers
  106. "set relativenumber " current line always 0 (requires 7.3 and up)
  107. """"""""
  108. "" UI - Statusline (now using powerline)
  109. """"""""""""""""""""""""""""""""""""""""""""""""""""""""
  110. set laststatus=2 " status line is second last line (not hidden by commands)
  111. """"""""
  112. "" UI - Code Folding
  113. """"""""""""""""""""""""""""""""""""""""""""""""""""""""
  114. set foldmethod=indent
  115. set foldlevel=10
  116. set foldtext=FoldText()
  117. """"""""
  118. "" UI - Search
  119. """"""""""""""""""""""""""""""""""""""""""""""""""""""""
  120. set hlsearch " make searches highlighted
  121. set incsearch " vim will search as you type!
  122. set ignorecase " ignore case for searches
  123. set smartcase " well, unless a user puts in uppercase search characters
  124. set magic " enables wildcard searching
  125. " use ack for grepping if available
  126. if executable('ack-grep')
  127. set grepprg=ack-grep\ --with-filename\ --nocolor\ --nogroup
  128. elseif executable('ack')
  129. set grepprg=ack\ --with-filename\ --nocolor\ --nogroup
  130. endif
  131. set shellpipe=&> " don't display ack/grep terminal output. NOTE: not reliable
  132. " https://github.com/mileszs/ack.vim/issues/18
  133. """"""""
  134. "" Key Remaps
  135. """"""""""""""""""""""""""""""""""""""""""""""""""""""""
  136. let mapleader = "," " easier to use than \
  137. " More convenient escape
  138. imap ii <ESC>
  139. imap II <ESC>
  140. " Yank to end of line, like D deletes to end of line
  141. nmap Y y$
  142. " Add extra lines up and down
  143. nmap <leader>j o<Esc>k
  144. nmap <leader>k O<Esc>j
  145. " Edit .vimrc
  146. nmap <leader>v :e $MYVIMRC<CR>
  147. " Clear TRAILING WHITE SPACE
  148. nmap <silent> <leader>$ :%s/\s\+$//g<CR>
  149. " Convert TABS to SPACES
  150. nmap <silent> <leader><TAB> :%s/<TAB>/ /g<CR>
  151. " Space as a folding toggle in normal mode.
  152. nmap <silent> <space> @=(foldlevel('.')?'za':"\<space>")<CR>
  153. " Tab to CLEAR CURRENT SEARCH (and stop highlighting) in normal mode
  154. " TODO would love to use <ESC> (more intuitive), but there are issues at vim
  155. " startup
  156. nmap <silent> \ :call ClearSearch()<CR>
  157. nmap <silent> <leader>n :set number!<CR>
  158. nmap <silent> <leader>w :set wrap!<CR>
  159. nmap <silent> <leader>80 gggqG<C-o><C-o>
  160. " Toggle paste mode - no autoindenting of pasted material
  161. nmap <silent> <leader>p :set paste! paste?<CR>
  162. " Toggle visible whitespace characters
  163. nmap <silent> <leader>l :set list!<CR>
  164. " Toggle scrollbind for moving multiple splits in sync together
  165. nmap <silent> <leader>s :set scrollbind! scrollbind?<CR>
  166. " Toggle mouse support.
  167. nmap <silent> <leader>m :call ToggleMouse()<CR>
  168. " Toggle NERDTree file browser
  169. nmap <silent> <leader>d :NERDTreeToggle<CR>
  170. " Toggle Commenting out lines with NERDCommenter
  171. nmap <silent> <leader>, :call NERDComment("n", "toggle")<CR>
  172. vmap <silent> <leader>, <ESC>:call NERDComment("x", "toggle")<CR>
  173. " Traverse undo tree with Gundo
  174. nmap <silent> <leader>u :GundoToggle<CR>
  175. " Git commands with Fugitive
  176. nmap <silent> <leader>gs :Gstatus<CR>
  177. nmap <silent> <leader>gc :Gcommit -v<CR>
  178. nmap <silent> <leader>gl :Glog<CR><CR>
  179. nmap <silent> <leader>gap :Git add -p<CR>
  180. nmap <silent> <leader>b :Gblame<CR>
  181. " Ack with Ctrl-F
  182. nmap <C-F> :Grep<space>
  183. " Code heirarchy with Tagbar
  184. nmap <silent> <leader>t :TagbarToggle<CR>
  185. """"""""
  186. "" Key Remaps - Movement and Windows
  187. """"""""""""""""""""""""""""""""""""""""""""""""""""""""
  188. " jump to beginning and end of line easier
  189. nmap H ^
  190. nmap L $
  191. vmap H ^
  192. vmap L $
  193. " Smart way to move between windows
  194. nmap <C-j> <C-W>j
  195. nmap <C-k> <C-W>k
  196. nmap <C-h> <C-W>h
  197. nmap <C-l> <C-W>l
  198. " mapping to make movements operate on 1 screen line in wrap mode
  199. onoremap <silent> <expr> j ScreenMovement("j")
  200. onoremap <silent> <expr> k ScreenMovement("k")
  201. onoremap <silent> <expr> 0 ScreenMovement("0")
  202. onoremap <silent> <expr> ^ ScreenMovement("^")
  203. onoremap <silent> <expr> $ ScreenMovement("$")
  204. nnoremap <silent> <expr> j ScreenMovement("j")
  205. nnoremap <silent> <expr> k ScreenMovement("k")
  206. nnoremap <silent> <expr> 0 ScreenMovement("0")
  207. nnoremap <silent> <expr> ^ ScreenMovement("^")
  208. nnoremap <silent> <expr> $ ScreenMovement("$")
  209. vnoremap <silent> <expr> j ScreenMovement("j")
  210. vnoremap <silent> <expr> k ScreenMovement("k")
  211. vnoremap <silent> <expr> 0 ScreenMovement("0")
  212. vnoremap <silent> <expr> ^ ScreenMovement("^")
  213. vnoremap <silent> <expr> $ ScreenMovement("$")
  214. """"""""
  215. "" Plugin options
  216. """"""""""""""""""""""""""""""""""""""""""""""""""""""""
  217. """"""""" NERDTree
  218. let g:NERDTreeQuitOnOpen = 1 " close sidebar after we go to selection
  219. """"""""" Gundo
  220. let g:gundo_width = 30
  221. let g:gundo_preview_height = 12
  222. let g:gundo_preview_bottom = 1
  223. """"""""" Powerline
  224. set rtp+=~/dotfiles/powerline/powerline/powerline/bindings/vim
  225. set noshowmode " don't show e.g. --INSERT-- since we're using powerline
  226. """"""""" Tagbar
  227. let g:tagbar_autoclose = 1 " close sidebar after we go to selection
  228. """"""""" EasyGrep
  229. let g:EasyGrepCommand = 1 " don't use the built in vimgrep, which is slow
  230. let g:EasyGrepHighlightQfMatches = 1
  231. """"""""" Indent Guides
  232. let g:indent_guides_enable_on_vim_startup = 1
  233. let g:indent_guides_auto_colors = 0
  234. """"""""" SuperTab
  235. let g:SuperTabDefaultCompletionType = 'context'
  236. """"""""" Syntastic
  237. let g:syntastic_auto_jump = 1
  238. """"""""" CtrlP
  239. let g:ctrlp_working_path_mode = 'rw'
  240. let g:ctrlp_user_command = {
  241. \ 'types': {
  242. \ 1: ['.git', 'cd %s && git ls-files'],
  243. \ 2: ['.hg', 'hg --cwd %s locate -I .']
  244. \ },
  245. \ 'fallback': 'find %s -type f'
  246. \ }
  247. """"""""" Vdebug
  248. let g:vdebug_options = {
  249. \ 'break_on_open' : 0,
  250. \ 'path_maps' : {"/usr": "/jails/alcatraz/usr"}
  251. \ }
  252. """"""""" Vim Markdown
  253. let g:vim_markdown_folding_disabled=1
  254. """"""""
  255. "" Functions
  256. """"""""""""""""""""""""""""""""""""""""""""""""""""""""
  257. function! FoldText()
  258. let line = getline(v:foldstart)
  259. let indent = indent(v:foldstart)
  260. let indentOnly = strpart(line, 0, indent-1)
  261. let linecount = v:foldend+1 - v:foldstart
  262. let foldtext = '+'.indentOnly.'... ('.linecount.' More lines)'
  263. return foldtext
  264. endfunction
  265. function! ToggleMouse()
  266. if &mouse == 'a'
  267. set mouse=
  268. echo "Mouse usage disabled"
  269. else
  270. set mouse=a
  271. echo "Mouse usage enabled"
  272. endif
  273. endfunction
  274. function! ScreenMovement(movement)
  275. if &wrap
  276. return "g" . a:movement
  277. else
  278. return a:movement
  279. endif
  280. endfunction
  281. function! ClearSearch()
  282. if (@/ != "")
  283. let @/=""
  284. redraw
  285. end
  286. endfunction
  287. function! MapQfPrevNext()
  288. " jump to the next/previous instance in Quickfix window
  289. exec "nmap <silent> [ :cprev<CR>"
  290. exec "nmap <silent> ] :cnext<CR>"
  291. endfunction
  292. function! UnmapQfPrefNext()
  293. exec "nunmap ["
  294. exec "nunmap ]"
  295. endfunction