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