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