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