]> git.rmz.io Git - dotfiles.git/blob - vim/vimrc
2cdb19d88c477dd9ea177477e9c527d25e1e73e6
[dotfiles.git] / vim / vimrc
1 " My vimrc.
2 "
3 " Author: Samir Benmendil <samir.benmendil[at]gmail[dot]com>
4 "
5
6 " plugins {{{1
7 filetype off
8 set runtimepath& " reset rtp
9 " remove all autocommands
10 autocmd!
11
12 set runtimepath+=$XDG_DATA_HOME/vim/vundle
13 call vundle#rc('$XDG_DATA_HOME/vim')
14
15 Plugin 'gmarik/vundle'
16
17 Plugin 'airblade/vim-gitgutter'
18 Plugin 'bling/vim-airline'
19 Plugin 'derekwyatt/vim-fswitch'
20 Plugin 'elzr/vim-json'
21 Plugin 'firef0x/pkgbuild.vim'
22 Plugin 'junegunn/vim-easy-align'
23 Plugin 'kien/ctrlp.vim'
24 " Plugin 'klen/python-mode'
25 Plugin 'wilywampa/python-mode'
26 Plugin 'kshenoy/vim-signature'
27 Plugin 'majutsushi/tagbar'
28 Plugin 'raimondi/delimitmate'
29 Plugin 'rking/ag.vim'
30 Plugin 'scrooloose/nerdtree'
31 Plugin 'scrooloose/syntastic'
32 Plugin 'sjl/gundo.vim'
33 Plugin 'tomtom/tcomment_vim'
34 Plugin 'tpope/vim-endwise'
35 Plugin 'tpope/vim-fugitive'
36 Plugin 'tpope/vim-repeat'
37 Plugin 'tpope/vim-surround'
38 Plugin 'tpope/vim-unimpaired'
39 Plugin 'valloric/youcompleteme'
40 Plugin 'vim-scripts/yankring.vim'
41
42 " snippets
43 Plugin 'sirver/ultisnips'
44 Plugin 'honza/vim-snippets'
45
46 " ros
47 Plugin 'ompugao/ros.vim'
48 Plugin 'ompugao/ctrlp-ros'
49
50 " seems to have problems right now... may be add later?
51 " Plugin 'jalcine/cmake.vim'
52 " Plugin 'powerman/vim-plugin-viewdoc'
53
54 " find an alternative
55 Plugin 'http://git.code.sf.net/p/vim-latex/vim-latex'
56 " Plugin 'lokaltog/vim-easymotion'
57 Plugin 'justinmk/vim-sneak'
58 Plugin 'chrisbra/checkattach'
59 Plugin 'nathanaelkane/vim-indent-guides'
60 Plugin 'alx741/vinfo'
61
62 Plugin 'kana/vim-textobj-user'
63 Plugin 'julian/vim-textobj-variable-segment'
64 Plugin 'sgur/vim-textobj-parameter'
65
66 " remove entries first
67 set runtimepath -=$HOME/.vim
68 set runtimepath -=$HOME/.vim/after
69 set runtimepath -=$XDG_CONFIG_HOME/vim
70 set runtimepath -=$XDG_CONFIG_HOME/vim/after
71 " then prepend and append them
72 set runtimepath ^=$XDG_CONFIG_HOME/vim
73 set runtimepath +=$XDG_CONFIG_HOME/vim/after
74
75 filetype plugin indent on
76
77 " colorscheme {{{1
78 syntax on
79 colorscheme badwolf
80
81 " options {{{1
82 " moving around, searching and patterns {{{2
83 set incsearch " show match for partly typed search command
84 set ignorecase " ignore case when using a search pattern
85 set smartcase " override 'ignorecase' when pattern has upper case characters
86 set hlsearch " highlight all matches for the last used search pattern
87
88 set nostartofline " don't move the cursor to the first non-blank char of a line
89 set path=.,include/,../include/,/usr/include/c++/*,/opt/ros/hydro/include
90
91 " displaying text {{{2
92 set scrolloff=5 " number of screen lines to show around the cursor
93 set nowrap " long lines wrap
94 set linebreak " wrap long lines at a character in 'breakat'
95 set showbreak=↪ " show these chars for wrapped lines
96
97 set lazyredraw " don't redraw while executing macros
98
99 set list " show chars defined in 'listchars'
100 set listchars=tab:▸\ " list of strings used for list mode
101 set listchars+=extends:❯,precedes:❮
102 " Only shown when not in insert mode
103 augroup trailing
104 au!
105 au InsertEnter * :set listchars-=trail:·
106 au InsertLeave * :set listchars+=trail:·
107 augroup END
108
109 set sidescroll=1 " number of collumns to scroll
110 set sidescrolloff=1 " don't scroll over the listchars
111
112 set fillchars=diff:⣿,vert:│
113
114 set nonumber " show the line number for each line
115 set norelativenumber " show the relative line number for each line
116
117 " syntax, highlighting and spelling {{{2
118 set synmaxcol=800 " don't highlight long lines
119
120 set dictionary=spell " list of dictionary files for keyword completion
121
122 set colorcolumn=+1
123
124 " multiple windows {{{2
125 set laststatus=2 " 0, 1 or 2; when to use a status line for the last window
126
127 set previewheight=20 " default height for the preview window
128
129 set hidden
130
131 " set splitbelow " a new window is put below of the current one
132 set splitright " a new window is put right of the current one
133
134 " terminal {{{2
135 set ttyfast
136
137 " using the mouse {{{2
138 set mouse=rnv " list of flags for using the mouse
139 set ttymouse=xterm " type of mouse
140
141 " messages and info {{{2
142 set showcmd " Show (partial) command in status line.
143 set ruler " show the cursor position all the time
144 set confirm " Ask what to do when closing unsaved documents
145 set shortmess=filnxtoOI " don't show intro message
146
147 " editing text {{{2
148 set backspace=indent,eol,start " allow backspacing over everything in insert mode
149
150 set showmatch " Show matching brackets.
151
152 set nojoinspaces " don't use two spaces after '.' when joining a line
153 set formatoptions=jcrnql
154
155 set nrformats=hex " number formats recognized for CTRL-A and CTRL-X commands
156
157 set complete=.,w,b,u,t
158 " whether to use a popup menu for Insert mode completion
159 set completeopt=longest,menuone,preview
160
161 " tabs and indent {{{2
162 set shiftwidth=4 " number of spaces used for each step of (auto)indent
163 set smarttab " a <Tab> in an indent inserts 'shiftwidth' spaces
164 set softtabstop=4 " if non-zero, number of spaces to insert for a <Tab>
165 set shiftround " round to 'shiftwidth' for "<<" and ">>"
166 set expandtab " expand <Tab> to spaces in Insert mode
167 set autoindent
168
169 set pastetoggle=<F11> " key sequence to toggle paste mode
170
171 " folding {{{2
172 set foldmethod=marker " folding type
173 set foldlevelstart=0 " value for 'foldlevel' when starting to edit a file
174
175 " save and restore folds
176 set viewoptions=cursor " only save cursor position
177
178 " diff mode {{{2
179 set diffopt=filler,vertical
180
181 " reading and writing files {{{2
182 set modeline " read modelines
183 set modelines=2 " only check first/last 2 lines
184
185 set writebackup " write a backup file before overwriting a file
186 set backup " keep a backup after owerwriting a file
187 set backupdir=$XDG_CACHE_HOME/vim/backup//
188
189 set backupskip+=.netrc " skip netrc
190 set backupskip+=/dev/shm/pass* " skip passwordstore files
191
192 set undofile " persistent undo history
193 set undodir=$XDG_CACHE_HOME/vim/undo//
194
195 augroup undoskip
196 au!
197 au BufWritePre .netrc setlocal noundofile
198 au BufWritePre /dev/shm/pass* setlocal noundofile
199 au BufWritePre /tmp/* setlocal noundofile
200 augroup END
201
202 set autowrite " automatically write a file when leaving a modified buffer
203 set autoread " automatically read a file that has been modified
204
205 " the swap file {{{2
206 set noswapfile
207 set directory=$XDG_CACHE_HOME/vim/swap//
208
209 " command line editing {{{2
210 set history=5000 " how many command lines are remembered
211 set wildmenu " command-line completion shows a list of matches
212 set wildmode=longest:full,full " specifies how command line completion works
213
214 set wildignore+=.hg,.git,.svn " Version control
215 set wildignore+=*.aux,*.out,*.toc " LaTeX intermediate files
216 set wildignore+=*.jpg,*.bmp,*.gif,*.png,*.jpeg " binary images
217 set wildignore+=*.o,*.obj,*.exe,*.dll,*.manifest " compiled object files
218 set wildignore+=*.spl " compiled spelling word lists
219 set wildignore+=*.sw? " Vim swap files
220 set wildignore+=*.luac " Lua byte code
221 set wildignore+=*.pyc " Python byte code
222 set wildignore+=*.orig " Merge resolution files
223
224 " various {{{2
225 set virtualedit=block " let cursor move past last char in <C-V> mode
226 set viminfo='100,<50,s10,h,n$XDG_CACHE_HOME/vim/viminfo " viminfo defaults but save file in .cache
227
228 set viewdir=$XDG_CACHE_HOME/vim/view//
229
230 " autocmds {{{1
231 " Resize splits when the window is resized {{{2
232 augroup resize
233 au!
234 autocmd VimResized * :wincmd =
235 augroup END
236
237 " Only show cursorline in the current window and in normal mode {{{2
238 augroup cline
239 au!
240 au WinLeave,InsertEnter * set nocursorline
241 au WinEnter,InsertLeave * set cursorline
242 augroup END
243
244 " Treat buffers from stdin (e.g.: echo foo | vim -) as scratch {{{2
245 augroup ft_stdin
246 au!
247 au StdinReadPost * :set buftype=nofile
248 augroup END
249
250 " Jump to last known cursor position {{{2
251 augroup cursor_pos
252 au!
253 " blacklist certain filetype
254 let blacklist = ['gitcommit']
255 autocmd BufReadPost *
256 \ if index(blacklist, &ft) < 0 && line("'\"") > 1 && line("'\"") <= line("$") |
257 \ exe "normal! g`\"" |
258 \ endif
259 augroup END
260
261 " bindings {{{1
262
263 " allow both <space> and / to be <leader>
264 map <space> <leader>
265
266 " make
267 nnoremap <leader>r :make<cr>
268 nnoremap <leader><cr> :make<cr>
269
270 " unhighlight search
271 nnoremap <silent> <Leader>/ :silent nohl<CR>
272
273 " Tabs
274 nnoremap <leader>[ :tabprev<cr>
275 nnoremap <leader>] :tabnext<cr>
276
277 " paste from selection
278 nnoremap <leader>p* :silent! set paste<CR>"*p:set nopaste<CR>
279 " paste from clipboard
280 nnoremap <leader>p+ :silent! set paste<CR>"+p:set nopaste<CR>
281
282 " Clean trailing whitespace
283 nnoremap <silent> <leader>ww m':%s/\s\+$//<cr>:let @/=''<cr>``zz
284
285 " Source
286 vnoremap <leader>S y:execute @@<cr>:echo 'Sourced selection.'<cr>
287 nnoremap <leader>S ^vg_y:execute @@<cr>:echo 'Sourced line.'<cr>
288
289 " jump to last cursor position
290 noremap ' `
291
292 " Select (charwise) the contents of the current line, excluding indentation.
293 nnoremap vv ^vg_
294
295 " Unfuck my screen
296 nnoremap U :syntax sync fromstart<cr>:AirlineRefresh<cr>:redraw!<cr>
297
298 " Ranger
299 " nnoremap <leader>r :silent !ranger %:h<cr>:redraw!<cr>
300 " nnoremap <leader>R :silent !ranger<cr>:redraw!<cr>
301
302 " Use sane regexes.
303 nnoremap / /\v
304 vnoremap / /\v
305 cnoremap s/ s/\v
306
307 " display the number of matches for the last search
308 nmap <Leader># :%s:<C-R>/::gn<CR>
309
310 " center cursor after search and open folds
311 nnoremap n nzzzv
312 nnoremap N Nzzzv
313
314 " same when jumping around
315 nnoremap g; g;zzzv
316 nnoremap g, g,zzzv
317 nnoremap <c-o> <c-o>zzzv
318
319 " Not using the default mappings of 'To line from top/bottom'
320 noremap H ^
321 noremap L $
322 vnoremap H ^
323 vnoremap L g_
324
325 " Heresy, emacs insert bindings
326 inoremap <c-a> <esc>I
327 inoremap <c-e> <esc>A
328 cnoremap <c-a> <home>
329 cnoremap <c-e> <end>
330
331 " proper movement when lines are wrapped
332 noremap <expr> j (v:count == 0 ? 'gj' : 'j')
333 noremap <expr> k (v:count == 0 ? 'gk' : 'k')
334
335 " disable arrows
336 noremap <Up> <NOP>
337 noremap <Down> <NOP>
338 noremap <Left> <NOP>
339 noremap <Right> <NOP>
340 inoremap <Up> <NOP>
341 inoremap <Down> <NOP>
342 inoremap <Left> <NOP>
343 inoremap <Right> <NOP>
344 cnoremap <Up> <NOP>
345 cnoremap <Down> <NOP>
346 cnoremap <Left> <NOP>
347 cnoremap <Right> <NOP>
348 cnoremap <C-K> <Up>
349 cnoremap <C-J> <Down>
350 cnoremap <C-H> <Left>
351 cnoremap <C-L> <Right>
352
353 " close all folds open fold in cursor
354 nnoremap zx zMzxzt
355
356 map <F1> :ls<CR>:b<space>
357
358 nnoremap <C-L> <C-W>w
359 nnoremap <C-H> <C-W>W
360
361 "xterm mouse with middleclick paste
362 nnoremap <MiddleMouse> i<MiddleMouse>
363 vnoremap <MiddleMouse> s<MiddleMouse>
364
365 " fix legacy vi inconsistency
366 map Y y$
367
368 " allow repeat operator on visual
369 vnoremap . :normal .<CR>
370
371 " add line without changing position or leaving mode
372 noremap <silent> <Leader>o :set paste<CR>m`o<ESC>``:set nopaste<CR>
373 noremap <silent> <Leader>O :set paste<CR>m`O<ESC>``:set nopaste<CR>
374
375 " Don't use Ex mode, use Q for formatting
376 map Q gq
377
378 " break undo sequence before removing word
379 inoremap <C-W> <C-G>u<C-W>
380
381 nmap <Leader>b :set expandtab tabstop=4 shiftwidth=4 softtabstop=4<CR>
382 nmap <Leader>B :set expandtab tabstop=8 shiftwidth=8 softtabstop=4<CR>
383 nmap <Leader>M :set noexpandtab tabstop=8 softtabstop=4 shiftwidth=4<CR>
384 nmap <Leader>m :set expandtab tabstop=2 shiftwidth=2 softtabstop=2<CR>
385
386 " space will toggle current fold in normal mode
387 nnoremap <leader><Space> za
388 " create folds around visual selection
389 vnoremap <leader><Space> zf
390
391 autocmd BufWinLeave *.* mkview
392 autocmd BufWinEnter *.* silent loadview
393
394 " save with sudo
395 cnoremap w!! w !sudo tee % > /dev/null
396
397 " uppercase previous word
398 inoremap <C-C> <Esc>gUiwgi
399
400 " plugins options {{{1
401 " ag {{{2
402 let g:AgSmartCase = 1
403 nnoremap <leader>ag yiw:Ag \12"<cr>
404 vnoremap <leader>ag y:Ag \12"<cr>
405
406 " airline {{{2
407 let g:airline#extensions#whitespace#enabled = 1
408 let g:airline#extensions#tabline#enabled = 1
409 let g:airline_powerline_fonts = 1
410
411 " checkattach {{{2
412 let g:checkattach_filebrowser = 'ranger'
413 let g:checkattach_once = 'y'
414
415 " delimitmate {{{2
416 let delimitMate_expand_cr = 2
417 let g:delimitMate_expand_space = 1
418
419 " fswitch {{{2
420 nnoremap <silent> <Leader>ff :FSHere<CR>
421 nnoremap <silent> <Leader>fl :FSRight<CR>
422 nnoremap <silent> <Leader>fh :FSLeft<CR>
423 nnoremap <silent> <Leader>fL :FSSplitRight<CR>
424 nnoremap <silent> <Leader>fH :FSSplitLeft<CR>
425
426 " fugitive {{{2
427 nmap <silent> <leader>dd :tab split \| Gdiff \| wincmd h<CR>
428 " delete fugitive buffers when closed
429 autocmd BufReadPost fugitive://* set bufhidden=delete
430
431 nnoremap <silent> <leader>gs :Gstatus<CR>
432 nnoremap <silent> <leader>gd :Gdiff<CR>
433 nnoremap <silent> <leader>gc :tab Gcommit -v<CR>
434 nnoremap <silent> <leader>ga :Gwrite<cr>
435 nnoremap <silent> <leader>gb :Gblame<cr>
436
437 " Gundo {{{2
438 nnoremap <F7> :GundoToggle<CR>
439
440 " indent-guides {{{2
441 let g:indent_guides_default_mapping = 0
442 let g:indent_guides_guide_size = 1
443 nmap <silent> cog <Plug>IndentGuidesToggle
444 nmap <silent> [og <Plug>IndentGuidesEnable
445 nmap <silent> ]og <Plug>IndentGuidesDisable
446
447 " NERDTree {{{2
448 " open/close NERDTree with \e
449 nmap <Leader>e :NERDTreeToggle<CR>
450 nmap <F6> :NERDTreeToggle<CR>
451 " <space> to open files/dirs
452 let NERDTreeMapActivateNode='l'
453 " close vim if only NERDTree is open
454 autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTreeType") && b:NERDTreeType == "primary") | q | endif
455
456 " python-mode {{{2
457
458 let g:pymode_rope_completion = 0
459 let g:pymode_rope = 0
460 let g:pymode_folding = 1
461 let g:pymode_lint_ignore = "E221,E266,E501"
462 let g:pymode_syntax_space_errors = 0 " don't bother me when I'm typing
463
464 " synastic {{{2
465 let g:syntastic_enable_highlighting = 0
466 let g:syntastic_error_symbol='E'
467 let g:syntastic_style_error_symbol='S'
468 let g:syntastic_warning_symbol='W'
469 let g:syntastic_style_warning_symbol='S'
470 let g:syntastic_always_populate_loc_list=1
471 nmap <silent> <leader>y :SyntasticCheck<cr>
472
473 if ! &diff
474 let g:syntastic_check_on_open=1
475 endif
476
477 " tagbar {{{2
478 map <F5> :TagbarToggle<cr>
479 let g:tagbar_sort = 0
480 let g:tagbar_compact = 1
481 let g:tagbar_autoshowtag = 1
482 let g:tagbar_width = 25
483 let g:tagbar_iconchars = ['+', '-']
484
485 " UltiSnips {{{2
486 let g:UltiSnipsEditSplit = 'vertical'
487 let g:UltiSnipsSnippetsDir = expand("$XDG_CONFIG_HOME/vim/ultisnips")
488 let g:UltiSnipsSnippetDirectories = ["UltiSnips", "ultisnips"]
489 let g:UltiSnipsExpandTrigger = "<tab>"
490 let g:UltiSnipsJumpForwardTrigger = "<tab>"
491 let g:UltiSnipsJumpBackwardTrigger = "<s-tab>"
492
493 " UltiSnips completion function that tries to expand a snippet. If there's no
494 " snippet for expanding, it checks for completion window and if it's shown,
495 " selects first element. If there's no completion window it tries to jump to
496 " next placeholder. If there's no placeholder it just returns TAB key
497 " https://github.com/Valloric/YouCompleteMe/issues/36#issuecomment-15451411
498 function! g:UltiSnips_Complete()
499 call UltiSnips#ExpandSnippet()
500 if g:ulti_expand_res == 0
501 if pumvisible()
502 return "\<C-n>"
503 else
504 call UltiSnips#JumpForwards()
505 if g:ulti_jump_forwards_res == 0
506 return "\<TAB>"
507 endif
508 endif
509 endif
510 return ""
511 endfunction
512 au InsertEnter * exec "inoremap <silent> " . g:UltiSnipsExpandTrigger . " <C-R>=g:UltiSnips_Complete()<cr>"
513 let g:UltiSnipsListSnippets="<c-e>"
514
515 " yankring {{{2
516 nnoremap <silent> <leader>p :YRShow<cr>
517 let g:yankring_history_dir = expand('$XDG_CACHE_HOME/vim')
518 let g:yankring_replace_n_pkey = ''
519 let g:yankring_replace_n_nkey = ''
520
521 " map Y to y$ for the yank ring
522 function! YRRunAfterMaps()
523 nnoremap Y :<C-U>YRYankCount 'y$'<CR>
524 endfunction
525
526 " youcompleteme {{{2
527 let g:ycm_extra_conf_globlist = ['~/src/*','/mnt/data/src/*']
528 let g:ycm_global_ycm_extra_conf = expand('$XDG_CONFIG_HOME/vim/ycm_extra_conf.py')
529 let g:ycm_extra_conf_vim_data = ['getcwd()']
530 let g:ycm_add_preview_to_completeopt = 1
531 let g:ycm_autoclose_preview_window_after_insertion = 0
532 "let g:ycm_extra_conf_vim_data = ['%:p']
533 nnoremap <leader>jd :YcmCompleter GoTo<CR>
534
535 " vim-easy-align {{{2
536 " start interactive EasyAlign in visual mode
537 vmap <Enter> <Plug>(EasyAlign)
538
539 " vim-json {{{2
540 let g:vim_json_syntax_conceal = 0
541
542 " vim-latex {{{2
543 let g:tex_flavor='latex'
544 let g:Tex_DefaultTargetFormat='pdf'
545 let g:Tex_MultipleCompileFormats='pdf'
546
547 " vim-sneak {{{2
548 let g:sneak#streak = 1
549 let g:sneak#target_labels = "aoeuisnthdpylrcgfqjkxzmwvz" " dvorak
550 let g:sneak#use_ic_scs = 1 " follow 'ignorecase' and 'smartcase'
551
552 " sneaky f and t
553 nmap f <Plug>Sneak_f
554 nmap F <Plug>Sneak_F
555 xmap f <Plug>Sneak_f
556 xmap F <Plug>Sneak_F
557 omap f <Plug>Sneak_f
558 omap F <Plug>Sneak_F
559 nmap t <Plug>Sneak_t
560 nmap T <Plug>Sneak_T
561 xmap t <Plug>Sneak_t
562 xmap T <Plug>Sneak_T
563 omap t <Plug>Sneak_t
564 omap T <Plug>Sneak_T
565
566 " functions {{{1
567
568 " Convenient command to see the difference between the current buffer and the
569 " file it was loaded from, thus the changes you made.
570 " Only define it when not defined already.
571 if !exists(":DiffOrig")
572 command DiffOrig vert new | set bt=nofile | r # | 0d_ | diffthis
573 \ | wincmd p | diffthis
574 endif
575
576 " edit configs {{{2
577 function! EditConfig(what)
578 let l:dir = split(&runtimepath,',')[0]
579 if a:what == 'vimrc'
580 let l:file = expand($MYVIMRC)
581 elseif ! isdirectory(globpath(l:dir, a:what))
582 echoe a:what." is not valid!"
583 elseif empty(&filetype)
584 echoe 'filetype is empty!'
585 else
586 let l:file = l:dir.'/'.a:what.'/'.&filetype.'.vim'
587 endif
588
589 execute ':vsplit '.file
590 execute ':lcd %:p:h'
591 endf
592 nmap <leader>ev :call EditConfig('vimrc')<CR>
593 nmap <leader>ef :call EditConfig('ftplugin')<CR>
594 nmap <leader>es :call EditConfig('syntax')<CR>
595 nmap <leader>ei :call EditConfig('indent')<CR>
596 nmap <leader>eu :UltiSnipsEdit<CR>
597
598 " spell check {{{2
599 " http://tex.stackexchange.com/a/52932
600 let g:myLangList=["en_gb","en_us","de","fr"]
601
602 function! ToggleSpell()
603 if !exists("b:myLang")
604 let b:myLang=0
605 endif
606 execute "setlocal spell!"
607 if (&spell)
608 echo "setlocal spelllang=" g:myLangList[b:myLang]
609 endif
610 endfunction
611
612 function! SwitchSpell()
613 if !exists("b:myLang")
614 let b:myLang=0
615 endif
616 if (&spell)
617 let b:myLang=b:myLang+1
618 if b:myLang>=len(g:myLangList) | let b:myLang=0 | endif
619 endif
620 execute "setlocal spell spelllang=".get(g:myLangList, b:myLang)
621 echo "setlocal spelllang=" g:myLangList[b:myLang]
622 endfunction
623
624 nnoremap <silent> coS :call SwitchSpell()<CR>
625 " fix spelling with first choice
626 nnoremap <Leader>f 1z=