vimrc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. " Use pathogen to load other plugins
  2. execute pathogen#infect()
  3. Helptags
  4. filetype plugin indent on
  5. set encoding=utf-8
  6. set spelllang=en_us
  7. " Search settings
  8. set incsearch
  9. set ignorecase
  10. set smartcase
  11. set hlsearch
  12. " Indentation and tab functionality
  13. set tabstop=2
  14. set shiftwidth=2
  15. set softtabstop=2
  16. set expandtab
  17. set autoindent
  18. set smarttab
  19. set smartindent
  20. " Code Folding
  21. set foldopen-=block
  22. set nofoldenable
  23. set foldmethod=indent
  24. set foldnestmax=10
  25. set viewoptions=cursor,folds,slash,unix
  26. " Command expiration
  27. set timeout
  28. set ttimeoutlen=15
  29. " Set font for (m|g)vim
  30. set guifont=Meslo\ LG\ S\ for\ Powerline:h11
  31. set guioptions=ce
  32. set mouse=a
  33. set number " Line numbers
  34. set nowrap " Line Wrapping
  35. set hidden " Just hide buffer when :bd'ing
  36. set autoread " Automatically update externally updated files
  37. set cc=80 " Which line is the 80th column?
  38. set tw=80 " Text width line wrapping
  39. set splitright " New window split settings
  40. set splitbelow " New window split settings
  41. set viminfo^=% " Remember buffer info on close
  42. set completeopt=menu
  43. set maxmempattern=8171906 " Because some files are big
  44. """"""""""""""""""""""""""""
  45. " Keybinds
  46. """"""""""""""""""""""""""""
  47. com! W w
  48. com! Q q
  49. let mapleader="\<space>"
  50. " Wrapped lines treated like normal ones
  51. nnoremap j gj
  52. nnoremap k gk
  53. nnoremap <leader>S :call ToggleSpellCheck()<cr>
  54. function! ToggleSpellCheck()
  55. if &spell ==# "nospell"
  56. set spell
  57. else
  58. set nospell
  59. endif
  60. endfunction
  61. " Set CTRL+S to save becuase I smack that every 10 seconds on whatever application I use
  62. command! -nargs=0 Save if &modified | confirm write | endif
  63. nnoremap <silent> <C-s> :Save<CR>
  64. inoremap <C-s> <C-o>:Save<CR>
  65. vnoremap <C-s> <C-o>:Save<CR>
  66. " CTRL+w to close the current buffer
  67. nnoremap <silent> <C-w> :Sayonara<CR>
  68. nnoremap <silent> <S-w> :hide<CR>
  69. " Buffer magic
  70. nnoremap <leader>l :ls<CR>:b<space>
  71. nnoremap <leader>; :tabs<CR>:tabn<space>
  72. " Switch indentation settings
  73. nnoremap <leader>y :set expandtab tabstop=4 shiftwidth=4 softtabstop=4<CR>
  74. nnoremap <leader>Y :set expandtab tabstop=8 shiftwidth=8 softtabstop=4<CR>
  75. nnoremap <leader>m :set expandtab tabstop=2 shiftwidth=2 softtabstop=2<CR>
  76. nnoremap <leader>M :set noexpandtab tabstop=8 softtabstop=4 shiftwidth=4<CR>
  77. " To wrap or not to wrap
  78. nnoremap <leader>w :setlocal wrap!<CR>:setlocal wrap?<CR>
  79. " Window Switching and Resizing
  80. nnoremap <silent> <C-k> :wincmd k<CR>
  81. nnoremap <silent> <C-j> :wincmd j<CR>
  82. nnoremap <silent> <C-h> :wincmd h<CR>
  83. nnoremap <silent> <C-l> :wincmd l<CR>
  84. if has("nvim")
  85. nnoremap <silent> <A-K> :wincmd K<CR>
  86. nnoremap <silent> <A-J> :wincmd J<CR>
  87. nnoremap <silent> <A-H> :wincmd H<CR>
  88. nnoremap <silent> <A-L> :wincmd L<CR>
  89. nnoremap <silent> <A-T> :wincmd T<CR>
  90. else
  91. nnoremap <silent> K :wincmd K<CR>
  92. nnoremap <silent> J :wincmd J<CR>
  93. nnoremap <silent> H :wincmd H<CR>
  94. nnoremap <silent> L :wincmd L<CR>
  95. nnoremap <silent> T :wincmd T<CR>
  96. endif
  97. nnoremap <silent> + :wincmd +<CR>
  98. nnoremap <silent> _ :wincmd -<CR>
  99. nnoremap <silent> ) :wincmd ><CR>
  100. nnoremap <silent> ( :wincmd <<CR>
  101. nnoremap <silent> <leader>r :wincmd r<CR>
  102. nnoremap <silent> <leader>R :wincmd R<CR>
  103. " Navigate tag stack
  104. nmap <A-]> <C-]>
  105. nmap <A-[> <C-T>
  106. " ALt+d to duplicate a line, vmode version is best for SHIFT+V, not the others
  107. nmap Y y$
  108. nnoremap <A-d> "yyy"yp
  109. vnoremap <A-d> "yy<C-o>"yp
  110. inoremap <A-d> <C-o>:yank<CR><C-o>:put<CR>
  111. " Yank/put to clipboard
  112. nnoremap <A-p> "+p
  113. vnoremap <A-p> "+p
  114. nnoremap <expr> <silent> <A-y> '"+' . (v:count ? v:count : '') . 'y'
  115. vnoremap <A-y> "+y
  116. " Move lines of text via alt+[jk]
  117. nnoremap <silent> <A-j> :m+1<CR>==
  118. nnoremap <silent> <A-k> :m-2<CR>==
  119. vnoremap <silent> <A-j> :m '>+1<CR>gv=gv
  120. vnoremap <silent> <A-k> :m '<-2<CR>gv=gv
  121. " Move text left/right via alt+[hl]
  122. nnoremap <silent> <A-l> "mx"mp
  123. nnoremap <silent> <A-h> "mxhh"mp
  124. " TODO: Maintain selection
  125. vnoremap <silent> <A-l> "mx"mp
  126. vnoremap <silent> <A-h> "mxhh"mp
  127. " Clear searches so there aren't underlined words.
  128. nnoremap <silent> <C-i> :nohlsearch<CR>
  129. " Formatting
  130. nmap <silent> =j :FormatJSON<CR>
  131. nmap <silent> =x :FormatXML<CR>
  132. nmap <silent> =, :s/$/,/<CR><C-i>
  133. " Set specific directories for swap, undo, and backups.
  134. set backupdir=~/.vim/tmp/backup//
  135. set directory=~/.vim/tmp/swap//
  136. set undodir=~/.vim/tmp/undo//
  137. set viewdir=~/.vim/tmp/view//
  138. set undofile
  139. map <S-k> <nop>
  140. map <S-q> <nop>
  141. """"""""""""""""""""""""""""
  142. " vim-rspec
  143. """"""""""""""""""""""""""""
  144. map <silent> <leader>t :call RunCurrentSpecFile()<CR>
  145. map <silent> <leader>a :call RunAllSpecs()<CR>
  146. map <silent> <leader>s :call RunNearestSpec()<CR>
  147. map <silent> <leader>k :call RunLastSpec()<CR>
  148. let g:rspec_command = "!if command -v rspec >/dev/null 2>&1; then rspec {spec}; else; bundle exec spec {spec}; fi"
  149. """"""""""""""""""""""""""""
  150. " Ctrl-P Settings
  151. """"""""""""""""""""""""""""
  152. " Keybinding and functionality
  153. let g:ctrlp_map = "<leader>o"
  154. " Move Ctrl-P to top of the screen
  155. let g:ctrlp_match_window_bottom = 0
  156. let g:ctrlp_match_window_reversed = 0
  157. " Ignore some filetypes
  158. let g:ctrlp_custom_ignore = '\v\~$|\.(o|swp|pyc|wav|mp3|ogg|blend)$|(^|[/\\])\.(hg|git|bzr)($|[/\\])|__init__\.py'
  159. "let g:ctrlp_by_filename = 1
  160. let g:ctrlp_clear_cache_on_exit = 0
  161. " Uncomment to show hidden directories.
  162. "let g:ctrlp_show_hidden = 1
  163. """"""""""""""""""""""""""""
  164. " Airline (Powerline replacement)
  165. """"""""""""""""""""""""""""
  166. let g:airline_theme = 'powerlineish'
  167. let g:airline_powerline_fonts = 1
  168. """"""""""""""""""""""""""""
  169. " NerdTree
  170. """"""""""""""""""""""""""""
  171. " Directory tree toggle
  172. nnoremap <leader>e :NERDTreeToggle<CR>
  173. """"""""""""""""""""""""""""
  174. " NerdCommenter
  175. """"""""""""""""""""""""""""
  176. map <leader>cc <plug>NERDCommenterToggle
  177. " For some reason, CommentToggle doesn't work unless this is
  178. " remapped, so remapping it to something I won't ever use.
  179. map <leader><A-/><space> <plug>NERDCommenterComment
  180. """"""""""""""""""""""""""""
  181. " TeX Settings
  182. """"""""""""""""""""""""""""
  183. let g:tex_flavor = "latex"
  184. let g:LatexBox_latexmk_options = "-pvc -pdfps"
  185. """"""""""""""""""""""""""""
  186. " Rails.vim
  187. """"""""""""""""""""""""""""
  188. let g:rails_gem_projections = {
  189. \ "fabrication": {
  190. \ "spec/fabricators/*_fabricator.rb": {
  191. \ "command": "fabricator",
  192. \ "affinity": "model",
  193. \ "alternate": "app/models/%s.rb",
  194. \ "related": "db/schema.rb#%p",
  195. \ "test": "spec/models/%s_spec.rb",
  196. \ "template": "Fabricator :%s do\nend",
  197. \ "keywords": "Fabricate Fabricator sequence"
  198. \ }
  199. \ },
  200. \ "factory_girl_rails": {
  201. \ "spec/factories/*_factory.rb": {
  202. \ "command": "factory",
  203. \ "affinity": "collection",
  204. \ "alternate": "app/models/%i.rb",
  205. \ "related": "db/schema.rb#%s",
  206. \ "test": "spec/models/%i_spec.rb",
  207. \ "template": "FactoryGirl.define do\n factory :%i do\n end\nend",
  208. \ "keywords": "factory sequence"
  209. \ }
  210. \ },
  211. \ "factory_girl": {
  212. \ "spec/factories/*_factory.rb": {
  213. \ "command": "factory",
  214. \ "affinity": "collection",
  215. \ "alternate": "app/models/%i.rb",
  216. \ "related": "db/schema.rb#%s",
  217. \ "test": "spec/models/%i_spec.rb",
  218. \ "template": "FactoryGirl.define do\n factory :%i do\n end\nend",
  219. \ "keywords": "factory sequence"
  220. \ }
  221. \ },
  222. \ "jasmine": {
  223. \ "spec/javascripts/*_spec.coffee": {
  224. \ "command": "rice",
  225. \ "template": "describe %S, ->\n ",
  226. \ "keywords": "describe it beforeEach expect loadFixtures xit"
  227. \ },
  228. \ "spec/javascripts/*_spec.js": {
  229. \ "command": "rice",
  230. \ "template": "describe(%S, function() {\n \n})",
  231. \ "keywords": "describe it beforeEach expect loadFixtures xit"
  232. \ }
  233. \ },
  234. \ "interactor-rails": {
  235. \ "app/interactors/*.rb": {
  236. \ "command": "interactor",
  237. \ "template": "class %S\n\nend"
  238. \ },
  239. \ },
  240. \ "cucumber-rails": {
  241. \ "features/*.feature": {
  242. \ "command": "feature",
  243. \ "template": "Feature: %h\n\n Scenario: " ,
  244. \ },
  245. \ "features/step_definitions/*_steps.rb": {
  246. \ "command": "steps",
  247. \ "affinity": "collection"
  248. \ }
  249. \ },
  250. \ "draper": {
  251. \ "app/decorators/*_decorator.rb": {
  252. \ "command": "decorator",
  253. \ "affinity": "model",
  254. \ "alternate": "app/models/%s.rb",
  255. \ "related": "db/schema.rb#%p",
  256. \ "test": "spec/decorators/%s_decorator_spec.rb",
  257. \ "template": "class %SDecorator < Draper::Decorator\n\nend",
  258. \ "keywords": "delegate_all"
  259. \ }
  260. \ }
  261. \ }
  262. let g:rails_projections = {
  263. \ "app/services/*.rb": {"command": "service"}
  264. \ }
  265. """"""""""""""""""""""""""""
  266. " delimitMate
  267. """"""""""""""""""""""""""""
  268. let g:delimitMate_expand_space = 1
  269. let g:delimitMate_expand_cr = 1
  270. """"""""""""""""""""""""""""
  271. " YCM Settings
  272. """"""""""""""""""""""""""""
  273. let g:ycm_register_as_syntastic_checker = 0
  274. let g:ycm_autoclose_preview_window_after_completion = 1
  275. let g:ycm_server_log_level = 'error'
  276. """"""""""""""""""""""""""""
  277. " Colors and Themes
  278. """"""""""""""""""""""""""""
  279. syntax enable
  280. "Set 256 color mode (not always needed).
  281. if $TERM == "xterm-256color" || $TERM == "screen-256color" || $COLORTERM == "xfce4-terminal"
  282. set t_Co=256
  283. endif
  284. " Use a nice color scheme
  285. colors monokai
  286. " but override matchparen color
  287. hi MatchParen ctermfg=160 ctermbg=NONE cterm=underline guifg=#df0000 guibg=NONE gui=underline
  288. """"""""""""""""""""""""""""""""""""""""
  289. " Useful Stuff
  290. """"""""""""""""""""""""""""""""""""""""
  291. " Delete trailing whitespaces on saving.
  292. function! DeleteTrailingWS()
  293. if !exists('b:noStrip')
  294. exe "normal mz"
  295. %s/\s\+$//ge
  296. exe "normal `z"
  297. endif
  298. endfunction
  299. au BufWrite * :call DeleteTrailingWS()
  300. au FileType go let b:noStrip=1
  301. if !has("nvim")
  302. " Allow the use of ALT as a function key.
  303. let c='a'
  304. while c <= 'z'
  305. exec "set <A-".c.">=\e".c
  306. exec "imap \e".c." <A-".c.">"
  307. let c = nr2char(1+char2nr(c))
  308. endw
  309. endif
  310. " Smart indent when entering insert mode with i on empty lines
  311. function! IndentWithI()
  312. if len(getline('.')) == 0
  313. return "\"_cc"
  314. else
  315. return "i"
  316. endif
  317. endfunction
  318. nnoremap <expr> i IndentWithI()
  319. " For auto-aligning '|' delimited tables
  320. inoremap <silent> <Bar> <Bar><Esc>:call <SID>align()<CR>a
  321. function! s:align()
  322. let p = '^\s*|\s.*\s|\s*$'
  323. if exists(':Tabularize') && getline('.') =~# '^\s*|' && (getline(line('.')-1) =~# p || getline(line('.')+1) =~# p)
  324. let column = strlen(substitute(getline('.')[0:col('.')],'[^|]','','g'))
  325. let position = strlen(matchstr(getline('.')[0:col('.')],'.*|\s*\zs.*'))
  326. Tabularize/|/l1
  327. normal! 0
  328. call search(repeat('[^|]*|',column).'\s\{-\}'.repeat('.',position),'ce',line('.'))
  329. endif
  330. endfunction
  331. " Set tabline at the top
  332. function! Tabline()
  333. let s = ''
  334. for i in range(tabpagenr('$'))
  335. let tab = i + 1
  336. let bufnr = tabpagebuflist(tab)[tabpagewinnr(tab) - 1]
  337. let bufname = bufname(bufnr)
  338. let bufmodified = getbufvar(bufnr, "&mod")
  339. let s .= '%' . tab . 'T'
  340. let s .= (tab == tabpagenr() ? '%#TabLineSel#' : '%#TabLine#')
  341. let s .= ' ' . tab . (bufmodified ? '+' : '') . ': '
  342. let s .= (bufname != '' ? fnamemodify(bufname, ':t') . ' ' : '--- ')
  343. endfor
  344. let s .= '%#TabLineFill#'
  345. return s
  346. endfunction
  347. set tabline=%!Tabline()
  348. " Go settings
  349. au BufRead,BufNewFile *.go set filetype=go
  350. au BufWritePre *.go :GoImports
  351. au FileType go map <buffer> <silent> <leader>t :GoTest<CR>
  352. let g:go_highlight_functions = 1
  353. let g:go_highlight_methods = 1
  354. let g:go_highlight_fields = 1
  355. let g:go_highlight_types = 1
  356. let g:go_highlight_operators = 0
  357. let g:go_highlight_extra_types = 1
  358. let g:go_highlight_format_strings = 1
  359. let g:go_highlight_generate_tags = 1
  360. let g:go_fmt_autosave = 0 " Let syntastic do this
  361. let g:go_fmt_experimental = 1 " Maintain folds after GoFmt + write
  362. let g:syntastic_go_checkers = ['gofmt']
  363. set rtp+=$GOPATH/src/github.com/golang/lint/misc/vim
  364. au BufRead,BufNewFile *.go map <leader>d :GoDeclsDir<cr>
  365. " JSON things
  366. let g:vim_json_syntax_conceal = 1
  367. com! FormatJSON %!python -m json.tool
  368. com! FormatXML %!xmllint --format -
  369. " Operator highlighting
  370. let g:ophigh_highlight_link_group = "Operator"
  371. let g:ophigh_container_color = "208"
  372. let g:ophigh_container_color_gui = "#fd971f"
  373. " Filetypes
  374. au BufRead,BufNewFile *.Dockerfile set filetype=dockerfile
  375. au BufRead,BufNewFile *.xsd set filetype=xml
  376. au BufRead,BufNewFile *.wsdl set filetype=xml
  377. map <F10> :echo "hi<" . synIDattr(synID(line("."),col("."),1),"name") . '> trans<'
  378. \ . synIDattr(synID(line("."),col("."),0),"name") . "> lo<"
  379. \ . synIDattr(synIDtrans(synID(line("."),col("."),1)),"name") . ">"<CR>
  380. let g:rustfmt_autosave = 1
  381. let g:ycm_rust_src_path = $RUST_SRC_PATH
  382. autocmd BufRead *.rs :setlocal tags=./rusty-tags.vi;/,$RUST_SRC_PATH/rusty-tags.vi
  383. autocmd BufWrite *.rs :silent! exec "!rusty-tags vi --quiet --start-dir=" . expand('%:p:h') . "&" <bar> redraw!