vimrc 9.8 KB

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