vimrc 11 KB

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