]> git.rmz.io Git - dotfiles.git/blob - vim/vimrc
vim: space be your leader
[dotfiles.git] / vim / vimrc
1 " My vimrc file.
2 "
3 " Author: Samir Benmendil <samir.benmendil[at]gmail[dot]com>
4 "
5
6 " bundles {{{1
7 filetype off
8 set runtimepath& " reset rtp
9
10 set runtimepath+=$XDG_DATA_HOME/vim/vundle
11 call vundle#rc('$XDG_DATA_HOME/vim')
12
13 Bundle 'gmarik/vundle'
14
15 Bundle 'bling/vim-airline'
16 Bundle 'elzr/vim-json'
17 Bundle 'ervandew/ag.git'
18 Bundle 'http://git.code.sf.net/p/vim-latex/vim-latex'
19 Bundle 'kien/ctrlp.vim'
20 Bundle 'majutsushi/tagbar'
21 Bundle 'scrooloose/nerdtree'
22 Bundle 'scrooloose/syntastic'
23 Bundle 'sjl/gundo.vim'
24 Bundle 'tomtom/tcomment_vim'
25 Bundle 'tpope/vim-fugitive'
26 Bundle 'tpope/vim-surround'
27 Bundle 'tpope/vim-unimpaired'
28 Bundle 'valloric/youcompleteme'
29 "Bundle 'zhaocai/goldenview.vim'
30
31 Bundle 'ompugao/ros.vim'
32 Bundle 'ompugao/ctrlp-ros'
33
34 Bundle 'firef0x/pkgbuild.vim'
35 Bundle 'derekwyatt/vim-fswitch'
36 " seems to have problems right now... may be add later?
37 "Bundle 'jalcine/cmake.vim'
38 Bundle 'powerman/vim-plugin-viewdoc'
39
40 " rtp + xdg {{{1
41 " remove entries first
42 set runtimepath -=$HOME/.vim
43 set runtimepath -=$HOME/.vim/after
44 set runtimepath -=$XDG_CONFIG_HOME/vim
45 set runtimepath -=$XDG_CONFIG_HOME/vim/after
46 " then prepend and append them
47 set runtimepath ^=$XDG_CONFIG_HOME/vim
48 set runtimepath +=$XDG_CONFIG_HOME/vim/after
49
50 filetype plugin indent on
51
52 " bindings {{{1
53 " allow both <space> and / to be <leader>
54 map <space> <leader>
55
56 " moving around, searching and patterns {{{1
57 set incsearch " show match for partly typed search command
58 set ignorecase " ignore case when using a search pattern
59 set smartcase " override 'ignorecase' when pattern has upper case characters
60 set hlsearch " highlight all matches for the last used search pattern
61
62 " use leader-n to unhighlight search
63 nmap <silent> <Leader>n :silent nohl<CR>
64 " use leader-# to display the number of matches for the last search
65 nmap <Leader># :%s:<C-R>/::gn<CR>
66 " center cursor after search
67 nnoremap n nzz
68 " disable arrows
69 inoremap <Up> <NOP>
70 inoremap <Down> <NOP>
71 inoremap <Left> <NOP>
72 inoremap <Right> <NOP>
73 noremap <Up> <NOP>
74 noremap <Down> <NOP>
75 noremap <Left> <NOP>
76 noremap <Right> <NOP>
77
78 set nostartofline " don't move the cursor to the first non-blank char of a line
79 set path=.,include/,../include/,/usr/include/c++/*,/opt/ros/hydro/include
80
81 " displaying text {{{1
82 set scrolloff=5 " number of screen lines to show around the cursor
83 set nowrap " long lines wrap
84 set linebreak " wrap long lines at a character in 'breakat'
85 set showbreak=▒▒ " show these chars for wrapped lines
86
87 set lazyredraw " don't redraw while executing macros
88
89 set list " show chars defined in 'listchars'
90 set listchars=tab:»·,trail:· " list of strings used for list mode
91
92 set number " show the line number for each line
93 set relativenumber " show the relative line number for each line
94 " toggle relativenumber
95 nnoremap <silent> <Leader>u :exe "set relativenumber!"<CR>
96
97 if has("autocmd")
98 " When editing a file, always jump to the last known cursor position.
99 " Don't do it when the position is invalid or when inside an event handler
100 " (happens when dropping a file on gvim).
101 " Also don't do it when the mark is in the first line, that is the default
102 " position when opening a file.
103 " blacklist certain filetype, you can get a file type with :echo &ft
104 let blacklist = ['gitcommit']
105 autocmd BufReadPost *
106 \ if index(blacklist, &ft) < 0 && line("'\"") > 1 && line("'\"") <= line("$") |
107 \ exe "normal! g`\"" |
108 \ endif
109 endif
110
111
112 " syntax, highlighting and spelling {{{1
113 colorscheme badwolf
114 syntax on
115
116 set dictionary=spell " list of dictionary files for keyword completion
117 " Spell Check http://tex.stackexchange.com/a/52932
118 let b:myLang=0
119 let g:myLangList=["en_gb","en_us","de","fr"]
120
121 function! ToggleSpell()
122 execute "setlocal spell!"
123 if (&spell)
124 echo "setlocal spelllang=" g:myLangList[b:myLang]
125 endif
126 endfunction
127 nnoremap <silent> <Leader>s :call ToggleSpell()<CR>
128
129 function! SwitchSpell()
130 if (&spell)
131 let b:myLang=b:myLang+1
132 if b:myLang>=len(g:myLangList) | let b:myLang=0 | endif
133 endif
134 execute "setlocal spell spelllang=".get(g:myLangList, b:myLang)
135 echo "setlocal spelllang=" g:myLangList[b:myLang]
136 endfunction
137 nnoremap <silent> <Leader>S :call SwitchSpell()<CR>
138
139 " fix spelling with first choice
140 nnoremap <Leader>f 1z=
141
142 if has("autocmd")
143 au Filetype *
144 \ if &omnifunc == "" | setl omnifunc=syntaxcomplete#Complete | endif
145 endif
146
147 " multiple windows {{{1
148 set laststatus=2 " 0, 1 or 2; when to use a status line for the last window
149
150 set previewheight=20 " default height for the preview window
151
152 set splitright " a new window is put right of the current one
153
154 map <F1> :ls<CR>:b<space>
155
156 nmap <C-L> <C-W>w
157 nmap <C-H> <C-W>W
158
159 " using the mouse {{{1
160 set mouse=rnv " list of flags for using the mouse
161 set ttymouse=xterm " type of mouse
162
163 "xterm mouse with middleclick paste
164 nnoremap <MiddleMouse> i<MiddleMouse>
165 vnoremap <MiddleMouse> s<MiddleMouse>
166
167 " messages and info {{{1
168 set showcmd " Show (partial) command in status line.
169 set ruler " show the cursor position all the time
170 set confirm " Ask what to do when closing unsaved documents
171 set shortmess=filnxtoOI " don't show intro message
172
173 " editing text {{{1
174 set backspace=indent,eol,start " allow backspacing over everything in insert mode
175
176 set showmatch " Show matching brackets.
177
178 set nojoinspaces " don't use two spaces after '.' when joining a line
179 set formatoptions+=j " remove comment leader when joining lines
180
181 set nrformats=hex " number formats recognized for CTRL-A and CTRL-X commands
182
183 " whether to use a popup menu for Insert mode completion
184 set completeopt=longest,menuone,preview
185
186 " Indent if we're at the beginning of a line. Else, do completion.
187 function! InsertTabWrapper()
188 let col = col('.') - 1
189 if !col || getline('.')[col - 1] !~ '\k'
190 return "\<Tab>"
191 else
192 return "\<C-N>"
193 endif
194 endfunction
195 inoremap <Tab> <C-R>=InsertTabWrapper()<CR>
196 inoremap <S-Tab> <C-P>
197
198 " fix legacy vi inconsistency
199 map Y y$
200
201 " allow repeat operator on visual
202 vnoremap . :normal .<CR>
203
204 " add line without changing position or leaving mode
205 map <Leader>o :set paste<CR>m`o<ESC>``:set nopaste<CR>
206 map <Leader>O :set paste<CR>m`O<ESC>``:set nopaste<CR>
207
208 " Don't use Ex mode, use Q for formatting
209 map Q gq
210
211 " allow undoing in insert-mode
212 inoremap <C-U> <C-G>u<C-U>
213 inoremap <C-W> <C-G>u<C-W>
214
215 " tabs and indent {{{1
216 set shiftwidth=4 " number of spaces used for each step of (auto)indent
217 set smarttab " a <Tab> in an indent inserts 'shiftwidth' spaces
218 set softtabstop=4 " if non-zero, number of spaces to insert for a <Tab>
219 set shiftround " round to 'shiftwidth' for "<<" and ">>"
220 set expandtab " expand <Tab> to spaces in Insert mode
221
222 set cindent " use smart C indenting (see :h C-indenting)
223 set cinoptions=l1,c4,(0,U1,w1,m1,j1,J1
224
225 set pastetoggle=<F11> " key sequence to toggle paste mode
226
227 nmap <Leader>b :set expandtab tabstop=4 shiftwidth=4 softtabstop=4<CR>
228 nmap <Leader>B :set expandtab tabstop=8 shiftwidth=8 softtabstop=4<CR>
229 nmap <Leader>M :set noexpandtab tabstop=8 softtabstop=4 shiftwidth=4<CR>
230 nmap <Leader>m :set expandtab tabstop=2 shiftwidth=2 softtabstop=2<CR>
231
232 " folding {{{1
233 set foldmethod=marker " folding type
234 set foldlevelstart=0 " value for 'foldlevel' when starting to edit a file
235
236 " space will toggle current fold in normal mode
237 nnoremap <leader><Space> za
238 " create folds around visual selection
239 vnoremap <leader><Space> zf
240
241 " save and restore folds
242 set viewoptions=folds,cursor " don't save local options
243 autocmd BufWinLeave *.* mkview
244 autocmd BufWinEnter *.* silent loadview
245
246 " reading and writing files {{{1
247 set writebackup " write a backup file before overwriting a file
248 set backup " keep a backup after owerwriting a file
249 set backupdir=$XDG_CACHE_HOME/vim
250
251 set undofile " persistent undo history
252 set undodir=$XDG_CACHE_HOME/vim
253
254 set autowrite " automatically write a file when leaving a modified buffer
255
256 " save with sudo
257 cmap w!! w !sudo tee % > /dev/null
258
259 " the swap file {{{1
260 set directory=$XDG_CACHE_HOME/vim,.,/var/tmp
261
262 " command line editing {{{1
263 set history=500 " how many command lines are remembered
264 set wildmode=longest:full " specifies how command line completion works
265 set wildmenu " command-line completion shows a list of matches
266
267 " various {{{1
268 set virtualedit=block " let cursor move past last char in <C-V> mode
269 set viminfo='100,<50,s10,h,n$XDG_CACHE_HOME/vim/viminfo " viminfo defaults but save file in .cache
270
271 set viewdir=$XDG_CACHE_HOME/vim
272
273 " plugins {{{1
274 " airline {{{2
275 let g:airline_detect_whitespace=2
276 let g:airline#extensions#tabline#enabled = 1
277 let g:airline_powerline_fonts = 1
278
279 " GoldenView {{{2
280 "let g:goldenview__enable_default_mapping = 0
281 "nmap <silent> <C-N> <Plug>GoldenViewSplit
282 "nmap <silent> <C-L> <Plug>GoldenViewNext
283 "nmap <silent> <C-H> <Plug>GoldenViewPrevious
284 "nmap <silent> <C-M> <Plug>GoldenViewSwitchMain
285 "nmap <silent> <C-S-M> <Plug>GoldenViewPrevious
286
287 " Gundo {{{2
288 nnoremap <F7> :GundoToggle<CR>
289 " fugitive {{{2
290 nmap <silent> <leader>dd :tab split \| Gdiff \| wincmd h<CR>
291 " delete fugitive buffers when closed
292 autocmd BufReadPost fugitive://* set bufhidden=delete
293
294 " NERDTree {{{2
295 " open/close NERDTree with \e
296 nmap <Leader>e :NERDTreeToggle<CR>
297 nmap <F6> :NERDTreeToggle<CR>
298 " <space> to open files/dirs
299 let NERDTreeMapActivateNode='<space>'
300 " close vim if only NERDTree is open
301 autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTreeType") && b:NERDTreeType == "primary") | q | endif
302
303 " synastic {{{2
304 let g:syntastic_enable_highlighting = 0
305 let g:syntastic_error_symbol='E'
306 let g:syntastic_style_error_symbol='S'
307 let g:syntastic_warning_symbol='W'
308 let g:syntastic_style_warning_symbol='S'
309 let g:syntastic_always_populate_loc_list=1
310 nmap <silent> <leader>y :SyntasticCheck<cr>
311
312 if ! &diff
313 let g:syntastic_check_on_open=1
314 endif
315
316 " tagbar {{{2
317 map <F5> :TagbarToggle<cr>
318 let g:tagbar_sort = 0
319 let g:tagbar_compact = 1
320 let g:tagbar_autoshowtag = 1
321 let g:tagbar_width = 25
322 let g:tagbar_iconchars = ['+', '-']
323
324 " YouCompleteMe {{{2
325 let g:ycm_extra_conf_globlist = ['~/src/*']
326
327 " vim-json {{{2
328 let g:vim_json_syntax_conceal = 0
329
330 " vim-latex {{{2
331 let g:tex_flavor='latex'
332 let g:Tex_DefaultTargetFormat='pdf'
333
334 " vim-viewdoc {{{2
335 let g:no_viewdoc_maps = 1
336 nnoremap K :call ViewDoc('doc', '<cword>')<cr>
337
338 " functions {{{1
339 " auto source vimrc when saved
340 autocmd bufwritepost vimrc source $MYVIMRC
341 nmap <leader>v :tabedit $MYVIMRC<CR>
342
343 " Convenient command to see the difference between the current buffer and the
344 " file it was loaded from, thus the changes you made.
345 " Only define it when not defined already.
346 if !exists(":DiffOrig")
347 command DiffOrig vert new | set bt=nofile | r # | 0d_ | diffthis
348 \ | wincmd p | diffthis
349 endif