vimrc 8.0 KB

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