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