vimrc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. " Persistent undo
  24. if has("persistent_undo")
  25. set undofile
  26. set undodir=~/.vimundo
  27. endif
  28. if has("autocmd")
  29. " Enable filetype specific features
  30. filetype plugin indent on
  31. " Clear existing autocmd
  32. autocmd!
  33. " When editing a file, always jump to the last cursor position
  34. autocmd BufReadPost *
  35. \ if line("'\"") > 0 && line ("'\"") <= line("$") |
  36. \ exe "normal! g'\"" |
  37. \ endif
  38. " Any actions on startup
  39. autocmd VimEnter * call StartUp()
  40. autocmd WinEnter * setlocal cursorline
  41. autocmd WinLeave * setlocal nocursorline
  42. " Show trailing white space
  43. autocmd InsertEnter * match ExtraWhitespace /\s\+\%#\@<!$/
  44. autocmd InsertLeave * match ExtraWhitespace /\s\+$/
  45. " Source the vimrc file after saving it
  46. autocmd BufWritePost .vimrc source $MYVIMRC
  47. " Local remaps for Quickfix buffer
  48. autocmd FileType qf nnoremap <silent> <buffer> q :ccl<CR>:lcl<CR>
  49. autocmd FileType qf nnoremap <silent> <buffer> o <CR>
  50. " TODO this doesn't always return to the window in more complex layouts
  51. autocmd FileType qf nnoremap <silent> <buffer> go <CR><C-W><C-W>
  52. " Global remaps when QuickFix buffer is opened
  53. autocmd BufWinEnter quickfix
  54. \ let g:qfix_win = bufnr("$") |
  55. \ call MapQfPrevNext()
  56. autocmd BufWinLeave *
  57. \ if exists("g:qfix_win") && expand("<abuf>") == g:qfix_win |
  58. \ unlet! g:qfix_win |
  59. \ call UnmapQfPrefNext() |
  60. \ endif
  61. " Open QuickFix window after any grep invocation (Glog and Ggrep)
  62. autocmd QuickFixCmdPost *grep* cwindow | call MapQfPrevNext()
  63. " if the last window is NERDTree, then close Vim
  64. "autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTreeType") && b:NERDTreeType == "primary") | q | endif
  65. else
  66. set autoindent on
  67. endif
  68. """"""""
  69. "" Tabs and Text Formatting
  70. """"""""""""""""""""""""""""""""""""""""""""""""""""""
  71. set expandtab " change to single spaces
  72. set tabstop=2 " actual tab press distance
  73. set shiftround " indent to nearest tabstops
  74. set shiftwidth=2 " amount to indent with > and <
  75. set smarttab " backspace tabs where appropriate even if spaces
  76. set softtabstop=2 " let backspace delete by indents
  77. set nowrap " do not wrap long lines of text
  78. set backspace=eol,start,indent "backspace over everything
  79. set textwidth=80 " try to keep text within 80 characters
  80. set colorcolumn=+1 " mark out the limits of the textwidth
  81. set listchars=trail:_,tab:>.,eol:$
  82. """"""""
  83. "" UI - Colours
  84. """"""""""""""""""""""""""""""""""""""""""""""""""""""
  85. syntax enable
  86. colorscheme solarized
  87. set t_Co=16
  88. set background=dark
  89. " set colour for folded lines
  90. highlight Folded term=none cterm=none ctermfg=darkred ctermbg=none
  91. " show trailing white space
  92. highlight ExtraWhitespace ctermfg=red ctermbg=red guifg=red guibg=red
  93. " reverse indent guides highlighting
  94. highlight IndentGuidesOdd ctermbg=darkgrey
  95. highlight IndentGuidesEven ctermbg=black
  96. """"""""
  97. "" UI - Numbering
  98. """"""""""""""""""""""""""""""""""""""""""""""""""""""""
  99. set number " show line numbers
  100. "set relativenumber " current line always 0 (requires 7.3 and up)
  101. """"""""
  102. "" UI - Statusline (now using powerline)
  103. """"""""""""""""""""""""""""""""""""""""""""""""""""""""
  104. set laststatus=2 " status line is second last line (not hidden by commands)
  105. """"""""
  106. "" UI - Code Folding
  107. """"""""""""""""""""""""""""""""""""""""""""""""""""""""
  108. set foldmethod=indent
  109. set foldlevel=10
  110. set foldtext=FoldText()
  111. """"""""
  112. "" UI - Search
  113. """"""""""""""""""""""""""""""""""""""""""""""""""""""""
  114. set hlsearch " make searches highlighted
  115. set incsearch " vim will search as you type!
  116. set ignorecase " ignore case for searches
  117. set smartcase " well, unless a user puts in uppercase search characters
  118. set magic " enables wildcard searching
  119. """"""""
  120. "" Key Remaps
  121. """"""""""""""""""""""""""""""""""""""""""""""""""""""""
  122. let mapleader = "," " easier to use than \
  123. " More convenient escape
  124. imap ii <ESC>
  125. imap II <ESC>
  126. " Yank to end of line, like D deletes to end of line
  127. nmap Y y$
  128. " Add extra lines up and down
  129. nmap <leader>j o<Esc>k
  130. nmap <leader>k O<Esc>j
  131. " Edit .vimrc
  132. nmap <leader>v :e $MYVIMRC<CR>
  133. " Clear TRAILING WHITE SPACE
  134. nmap <silent> <leader>$ :%s/\s\+$//g<CR>
  135. " Convert TABS to SPACES
  136. nmap <silent> <leader><TAB> :%s/<TAB>/ /g<CR>
  137. " Space as a folding toggle in normal mode.
  138. nmap <silent> <space> @=(foldlevel('.')?'za':"\<space>")<CR>
  139. " Tab to CLEAR CURRENT SEARCH (and stop highlighting) in normal mode
  140. " TODO would love to use <ESC> (more intuitive), but there are issues at vim
  141. " startup
  142. nmap <silent> <TAB> :call ClearSearch()<CR>
  143. nmap <silent> <leader>n :set number!<CR>
  144. nmap <silent> <leader>w :set wrap!<CR>
  145. " Toggle paste mode - no autoindenting of pasted material
  146. nmap <silent> <leader>p :set paste! paste?<CR>
  147. " Toggle visible whitespace characters
  148. nmap <silent> <leader>l :set list!<CR>
  149. " Toggle scrollbind for moving multiple splits in sync together
  150. nmap <silent> <leader>s :set scrollbind! scrollbind?<CR>
  151. " Toggle mouse support.
  152. nmap <silent> <leader>m :call ToggleMouse()<CR>
  153. " Toggle NERDTree file browser
  154. nmap <silent> <leader>d :NERDTreeToggle<CR>
  155. " Toggle Commenting out lines with NERDCommenter
  156. nmap <silent> <leader>, :call NERDComment("n", "toggle")<CR>
  157. vmap <silent> <leader>, <ESC>:call NERDComment("x", "toggle")<CR>
  158. " Traverse undo tree with Gundo
  159. nmap <silent> <leader>u :GundoToggle<CR>
  160. " Git blame and log with Fugitive
  161. nmap <silent> <leader>b :Gblame<CR>
  162. nmap <silent> <leader>g :Glog<CR><CR>
  163. " Code heirarchy with Tagbar
  164. nmap <silent> <leader>t :TagbarToggle<CR>
  165. """"""""
  166. "" Key Remaps - Movement and Windows
  167. """"""""""""""""""""""""""""""""""""""""""""""""""""""""
  168. " jump to beginning and end of line easier
  169. nmap H ^
  170. nmap L $
  171. " center current line when moving between searches
  172. nmap n nzz
  173. nmap N Nzz
  174. " Smart way to move between windows
  175. nmap <C-j> <C-W>j
  176. nmap <C-k> <C-W>k
  177. nmap <C-h> <C-W>h
  178. nmap <C-l> <C-W>l
  179. " mapping to make movements operate on 1 screen line in wrap mode
  180. onoremap <silent> <expr> j ScreenMovement("j")
  181. onoremap <silent> <expr> k ScreenMovement("k")
  182. onoremap <silent> <expr> 0 ScreenMovement("0")
  183. onoremap <silent> <expr> ^ ScreenMovement("^")
  184. onoremap <silent> <expr> $ ScreenMovement("$")
  185. nnoremap <silent> <expr> j ScreenMovement("j")
  186. nnoremap <silent> <expr> k ScreenMovement("k")
  187. nnoremap <silent> <expr> 0 ScreenMovement("0")
  188. nnoremap <silent> <expr> ^ ScreenMovement("^")
  189. nnoremap <silent> <expr> $ ScreenMovement("$")
  190. """"""""
  191. "" Plugin options
  192. """"""""""""""""""""""""""""""""""""""""""""""""""""""""
  193. """"""""" NERDTree
  194. let g:NERDTreeQuitOnOpen = 1 " close sidebar after we go to selection
  195. """"""""" Gundo
  196. let g:gundo_width = 30
  197. let g:gundo_preview_height = 12
  198. let g:gundo_preview_bottom = 1
  199. """"""""" Powerline
  200. set rtp+=~/dotfiles/powerline/powerline/bindings/vim
  201. """"""""" Tagbar
  202. let g:tagbar_autoclose = 1 " close sidebar after we go to selection
  203. """"""""" EasyGrep
  204. let g:EasyGrepJumpToMatch = 0
  205. let g:EasyGrepHighlightQfMatches = 1
  206. """"""""" Indent Guides
  207. let g:indent_guides_enable_on_vim_startup = 1
  208. let g:indent_guides_auto_colors = 0
  209. """"""""" Syntastic
  210. let g:syntastic_auto_jump = 1
  211. """"""""
  212. "" Functions
  213. """"""""""""""""""""""""""""""""""""""""""""""""""""""""
  214. function! StartUp()
  215. " start NERDTree if vim called with no arguments
  216. if 0 == argc()
  217. NERDTree
  218. end
  219. endfunction
  220. function! FoldText()
  221. let line = getline(v:foldstart)
  222. let indent = indent(v:foldstart)
  223. let indentOnly = strpart(line, 0, indent-1)
  224. let linecount = v:foldend+1 - v:foldstart
  225. let foldtext = '+'.indentOnly.'... ('.linecount.' More lines)'
  226. return foldtext
  227. endfunction
  228. function! ToggleMouse()
  229. if &mouse == 'a'
  230. set mouse=
  231. echo "Mouse usage disabled"
  232. else
  233. set mouse=a
  234. echo "Mouse usage enabled"
  235. endif
  236. endfunction
  237. function! ScreenMovement(movement)
  238. if &wrap
  239. return "g" . a:movement
  240. else
  241. return a:movement
  242. endif
  243. endfunction
  244. function! ClearSearch()
  245. if (@/ != "")
  246. let @/=""
  247. redraw
  248. end
  249. endfunction
  250. function! MapQfPrevNext()
  251. " jump to the next/previous instance in Quickfix window
  252. exec "nmap <silent> <TAB> :cprev<CR>zz"
  253. exec "nmap <silent> <Bslash> :cnext<CR>zz"
  254. endfunction
  255. function! UnmapQfPrefNext()
  256. exec "nunmap <TAB>"
  257. exec "nunmap <Bslash>"
  258. endfunction