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