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