vimrc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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=10 " 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. " Any actions on startup
  42. autocmd VimEnter * call StartUp()
  43. autocmd WinEnter * setlocal cursorline
  44. autocmd WinLeave * setlocal nocursorline
  45. " Show trailing white space
  46. autocmd InsertEnter * match ExtraWhitespace /\s\+\%#\@<!$/
  47. autocmd InsertLeave * match ExtraWhitespace /\s\+$/
  48. " Source the vimrc file after saving it
  49. autocmd BufWritePost .vimrc source $MYVIMRC
  50. " Local remaps for Quickfix buffer
  51. autocmd FileType qf nnoremap <silent> <buffer> q :ccl<CR>:lcl<CR>
  52. autocmd FileType qf nnoremap <silent> <buffer> o <CR>
  53. " TODO this doesn't always return to the window in more complex layouts
  54. autocmd FileType qf nnoremap <silent> <buffer> go <CR><C-W><C-W>
  55. " Global remaps when QuickFix buffer is opened
  56. autocmd BufWinEnter quickfix
  57. \ let g:qfix_win = bufnr("$") |
  58. \ call MapQfPrevNext()
  59. autocmd BufWinLeave *
  60. \ if exists("g:qfix_win") && expand("<abuf>") == g:qfix_win |
  61. \ unlet! g:qfix_win |
  62. \ call UnmapQfPrefNext() |
  63. \ endif
  64. " Open QuickFix window after any grep invocation (Glog and Ggrep)
  65. autocmd QuickFixCmdPost *grep* cwindow | call MapQfPrevNext()
  66. " if the last window is NERDTree, then close Vim
  67. "autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTreeType") && b:NERDTreeType == "primary") | q | endif
  68. else
  69. set autoindent on
  70. endif
  71. """"""""
  72. "" Tabs and Text Formatting
  73. """"""""""""""""""""""""""""""""""""""""""""""""""""""
  74. set expandtab " change to single spaces
  75. set tabstop=2 " actual tab press distance
  76. set shiftround " indent to nearest tabstops
  77. set shiftwidth=2 " amount to indent with > and <
  78. set smarttab " backspace tabs where appropriate even if spaces
  79. set softtabstop=2 " let backspace delete by indents
  80. set nowrap " do not wrap long lines of text
  81. set backspace=eol,start,indent "backspace over everything
  82. set textwidth=80 " try to keep text within 80 characters
  83. set colorcolumn=+1 " mark out the limits of the textwidth
  84. set listchars=trail:_,tab:>.,eol:$
  85. """"""""
  86. "" UI - Colours
  87. """"""""""""""""""""""""""""""""""""""""""""""""""""""
  88. syntax enable
  89. colorscheme solarized
  90. set t_Co=16
  91. set background=dark
  92. " set colour for folded lines
  93. highlight Folded term=none cterm=none ctermfg=darkred ctermbg=none
  94. " show trailing white space
  95. highlight ExtraWhitespace ctermfg=red ctermbg=red guifg=red guibg=red
  96. " reverse indent guides highlighting
  97. highlight IndentGuidesOdd ctermbg=darkgrey
  98. highlight IndentGuidesEven ctermbg=black
  99. """"""""
  100. "" UI - Numbering
  101. """"""""""""""""""""""""""""""""""""""""""""""""""""""""
  102. set number " show line numbers
  103. "set relativenumber " current line always 0 (requires 7.3 and up)
  104. """"""""
  105. "" UI - Statusline (now using powerline)
  106. """"""""""""""""""""""""""""""""""""""""""""""""""""""""
  107. set laststatus=2 " status line is second last line (not hidden by commands)
  108. """"""""
  109. "" UI - Code Folding
  110. """"""""""""""""""""""""""""""""""""""""""""""""""""""""
  111. set foldmethod=indent
  112. set foldlevel=10
  113. set foldtext=FoldText()
  114. """"""""
  115. "" UI - Search
  116. """"""""""""""""""""""""""""""""""""""""""""""""""""""""
  117. set hlsearch " make searches highlighted
  118. set incsearch " vim will search as you type!
  119. set ignorecase " ignore case for searches
  120. set smartcase " well, unless a user puts in uppercase search characters
  121. set magic " enables wildcard searching
  122. """"""""
  123. "" Key Remaps
  124. """"""""""""""""""""""""""""""""""""""""""""""""""""""""
  125. let mapleader = "," " easier to use than \
  126. " More convenient escape
  127. imap ii <ESC>
  128. imap II <ESC>
  129. " Yank to end of line, like D deletes to end of line
  130. nmap Y y$
  131. " Add extra lines up and down
  132. nmap <leader>j o<Esc>k
  133. nmap <leader>k O<Esc>j
  134. " Edit .vimrc
  135. nmap <leader>v :e $MYVIMRC<CR>
  136. " Clear TRAILING WHITE SPACE
  137. nmap <silent> <leader>$ :%s/\s\+$//g<CR>
  138. " Convert TABS to SPACES
  139. nmap <silent> <leader><TAB> :%s/<TAB>/ /g<CR>
  140. " Space as a folding toggle in normal mode.
  141. nmap <silent> <space> @=(foldlevel('.')?'za':"\<space>")<CR>
  142. " Tab to CLEAR CURRENT SEARCH (and stop highlighting) in normal mode
  143. " TODO would love to use <ESC> (more intuitive), but there are issues at vim
  144. " startup
  145. nmap <silent> <TAB> :call ClearSearch()<CR>
  146. nmap <silent> <leader>n :set number!<CR>
  147. nmap <silent> <leader>w :set wrap!<CR>
  148. " Toggle paste mode - no autoindenting of pasted material
  149. nmap <silent> <leader>p :set paste! paste?<CR>
  150. " Toggle visible whitespace characters
  151. nmap <silent> <leader>l :set list!<CR>
  152. " Toggle scrollbind for moving multiple splits in sync together
  153. nmap <silent> <leader>s :set scrollbind! scrollbind?<CR>
  154. " Toggle mouse support.
  155. nmap <silent> <leader>m :call ToggleMouse()<CR>
  156. " Toggle NERDTree file browser
  157. nmap <silent> <leader>d :NERDTreeToggle<CR>
  158. " Toggle Commenting out lines with NERDCommenter
  159. nmap <silent> <leader>, :call NERDComment("n", "toggle")<CR>
  160. vmap <silent> <leader>, <ESC>:call NERDComment("x", "toggle")<CR>
  161. " Traverse undo tree with Gundo
  162. nmap <silent> <leader>u :GundoToggle<CR>
  163. " Git blame and log with Fugitive
  164. nmap <silent> <leader>b :Gblame<CR>
  165. nmap <silent> <leader>g :Glog<CR><CR>
  166. " Code heirarchy with Tagbar
  167. nmap <silent> <leader>t :TagbarToggle<CR>
  168. """"""""
  169. "" Key Remaps - Movement and Windows
  170. """"""""""""""""""""""""""""""""""""""""""""""""""""""""
  171. " jump to beginning and end of line easier
  172. nmap H ^
  173. nmap L $
  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>"
  253. exec "nmap <silent> <Bslash> :cnext<CR>"
  254. endfunction
  255. function! UnmapQfPrefNext()
  256. exec "nunmap <TAB>"
  257. exec "nunmap <Bslash>"
  258. endfunction