vimrc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. " Persistent undo
  23. if has("persistent_undo")
  24. set undofile
  25. set undodir=~/.vimundo
  26. endif
  27. if has("autocmd")
  28. " Enable filetype specific features
  29. filetype plugin indent on
  30. " Clear existing autocmd
  31. autocmd!
  32. " When editing a file, always jump to the last cursor position
  33. autocmd BufReadPost *
  34. \ if line("'\"") > 0 && line ("'\"") <= line("$") |
  35. \ exe "normal! g'\"" |
  36. \ endif
  37. " Any actions on startup
  38. autocmd VimEnter * call StartUp()
  39. autocmd WinEnter * setlocal cursorline
  40. autocmd WinLeave * setlocal nocursorline
  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. " Reload Powerline colours after source
  47. " https://github.com/Lokaltog/vim-powerline/issues/28
  48. autocmd BufWritePost .vimrc call Pl#Load()
  49. " Local remaps for Quickfix buffer
  50. autocmd FileType qf nnoremap <silent> <buffer> q :ccl<CR>:lcl<CR>
  51. autocmd FileType qf nnoremap <silent> <buffer> o <CR>
  52. " TODO this doesn't always return to the window in more complex layouts
  53. autocmd FileType qf nnoremap <silent> <buffer> go <CR><C-W><C-W>
  54. " Global remaps when QuickFix buffer is opened
  55. autocmd BufWinEnter quickfix
  56. \ let g:qfix_win = bufnr("$") |
  57. \ call MapQfPrevNext()
  58. autocmd BufWinLeave *
  59. \ if exists("g:qfix_win") && expand("<abuf>") == g:qfix_win |
  60. \ unlet! g:qfix_win |
  61. \ call UnmapQfPrefNext() |
  62. \ endif
  63. " Open QuickFix window after any grep invocation (Glog and Ggrep)
  64. autocmd QuickFixCmdPost *grep* cwindow | 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. else
  68. set autoindent on
  69. endif
  70. """"""""
  71. "" Tabs and Text Formatting
  72. """"""""""""""""""""""""""""""""""""""""""""""""""""""
  73. set expandtab " change to single spaces
  74. set tabstop=2 " actual tab press distance
  75. set shiftround " indent to nearest tabstops
  76. set shiftwidth=2 " amount to indent with > and <
  77. set smarttab " backspace tabs where appropriate even if spaces
  78. set softtabstop=2 " let backspace delete by indents
  79. set nowrap " do not wrap long lines of text
  80. set backspace=eol,start,indent "backspace over everything
  81. set textwidth=80 " try to keep text within 80 characters
  82. set colorcolumn=+1 " mark out the limits of the textwidth
  83. set listchars=trail:_,tab:>.,eol:$
  84. """"""""
  85. "" UI - Colours
  86. """"""""""""""""""""""""""""""""""""""""""""""""""""""
  87. syntax enable
  88. colorscheme solarized
  89. set t_Co=16
  90. set background=dark
  91. " set colour for folded lines
  92. highlight Folded term=none cterm=none ctermfg=darkred ctermbg=none
  93. " show trailing white space
  94. highlight ExtraWhitespace ctermfg=red ctermbg=red guifg=red guibg=red
  95. " reverse indent guides highlighting
  96. highlight IndentGuidesOdd ctermbg=darkgrey
  97. highlight IndentGuidesEven ctermbg=black
  98. """"""""
  99. "" UI - Numbering
  100. """"""""""""""""""""""""""""""""""""""""""""""""""""""""
  101. set number " show line numbers
  102. "set relativenumber " current line always 0 (requires 7.3 and up)
  103. """"""""
  104. "" UI - Statusline (now using powerline)
  105. """"""""""""""""""""""""""""""""""""""""""""""""""""""""
  106. set laststatus=2 " status line is second last line (not hidden by commands)
  107. """"""""
  108. "" UI - Code Folding
  109. """"""""""""""""""""""""""""""""""""""""""""""""""""""""
  110. set foldmethod=indent
  111. set foldlevel=10
  112. set foldtext=FoldText()
  113. """"""""
  114. "" UI - Search
  115. """"""""""""""""""""""""""""""""""""""""""""""""""""""""
  116. set hlsearch " make searches highlighted
  117. set incsearch " vim will search as you type!
  118. set ignorecase " ignore case for searches
  119. set smartcase " well, unless a user puts in uppercase search characters
  120. set magic " enables wildcard searching
  121. """"""""
  122. "" Key Remaps
  123. """"""""""""""""""""""""""""""""""""""""""""""""""""""""
  124. let mapleader = "," " easier to use than \
  125. " More convenient escape
  126. imap ii <ESC>
  127. imap II <ESC>
  128. " Yank to end of line, like D deletes to end of line
  129. nmap Y y$
  130. " Add extra lines up and down
  131. nmap <leader>j o<Esc>k
  132. nmap <leader>k O<Esc>j
  133. " Edit .vimrc
  134. nmap <leader>v :e $MYVIMRC<CR>
  135. " Clear TRAILING WHITE SPACE
  136. nmap <silent> <leader>$ :%s/\s\+$//g<CR>
  137. " Convert TABS to SPACES
  138. nmap <silent> <leader><TAB> :%s/<TAB>/ /g<CR>
  139. " Space as a folding toggle in normal mode.
  140. nmap <silent> <space> @=(foldlevel('.')?'za':"\<space>")<CR>
  141. " Escape to CLEAR CURRENT SEARCH (and stop highlighting) in normal mode
  142. nnoremap <silent> <ESC> :call ClearSearch()<CR><ESC>
  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 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. let g:Powerline_symbols = 'fancy' " alternatives: fancy, unicode
  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. """"""""
  210. "" Functions
  211. """"""""""""""""""""""""""""""""""""""""""""""""""""""""
  212. function! StartUp()
  213. " start NERDTree if vim called with no arguments
  214. if 0 == argc()
  215. NERDTree
  216. end
  217. endfunction
  218. function! FoldText()
  219. let line = getline(v:foldstart)
  220. let indent = indent(v:foldstart)
  221. let indentOnly = strpart(line, 0, indent-1)
  222. let linecount = v:foldend+1 - v:foldstart
  223. let foldtext = '+'.indentOnly.'... ('.linecount.' More lines)'
  224. return foldtext
  225. endfunction
  226. function! ToggleMouse()
  227. if &mouse == 'a'
  228. set mouse=
  229. echo "Mouse usage disabled"
  230. else
  231. set mouse=a
  232. echo "Mouse usage enabled"
  233. endif
  234. endfunction
  235. function! ScreenMovement(movement)
  236. if &wrap
  237. return "g" . a:movement
  238. else
  239. return a:movement
  240. endif
  241. endfunction
  242. function! ClearSearch()
  243. if (@/ != "")
  244. let @/=""
  245. redraw
  246. end
  247. endfunction
  248. function! MapQfPrevNext()
  249. " jump to the next/previous instance in Quickfix window
  250. exec "nmap <silent> 3 :cprev<CR>zz"
  251. exec "nmap <silent> 8 :cnext<CR>zz"
  252. endfunction
  253. function! UnmapQfPrefNext()
  254. exec "nunmap 3"
  255. exec "nunmap 8"
  256. endfunction