-
Notifications
You must be signed in to change notification settings - Fork 23
/
_vimrc
455 lines (378 loc) · 12.6 KB
/
_vimrc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
" Chen3feng's .vimrc
" Author: CHEN Feng <[email protected]>
if version < 700
echo "~/.vimrc: Vim 7.0+ is required!, you should upgrade your vim to latest version."
endif
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" common settings section
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
set nocompatible
syntax on
filetype plugin on
filetype plugin indent on
set modeline
set modelines=10
" backup settings
set backup
set backupdir=~/.vimbackup
if !isdirectory(expand(&backupdir))
if exists("*mkdir")
call mkdir(expand(&backupdir), "p")
endif
endif
set autowrite
" input settings
set backspace=2
set tabstop=4
set shiftwidth=4
set smarttab
" set softtabstop=4
set expandtab " expand tab to spaces
" indent settiongs
set autoindent
set smartindent
set cindent
set cinoptions=:0,g0,t0,(0,Ws,m1
" search settings
set hlsearch
set incsearch
set smartcase
" quickfix settings
compiler gcc
let g:NeoComplCache_EnableAtStartup = 1 " Old neocomplcache
let g:neocomplcache_enable_at_startup = 1 " New neocomplcache
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Display settings section
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
set ruler
set showmatch
set showmode
set wildmenu
set wildmode=longest:full,full
if !&diff
" set colorcolumn=81,101
set colorcolumn=101
endif
" status line
set laststatus=2
set statusline=%<%f\ %h%m%r%=%k[%{&fileformat}][%{(&fenc==\"\")?&enc:&fenc}%{(&bomb?\",BOM\":\"\")}]\ %-14.(%l,%c%V%)\ %P\ %-10.3n
if has("mswin")
set diffexpr=MyDiff()
function MyDiff()
let opt = '-a --binary '
if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif
if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif
let arg1 = v:fname_in
if arg1 =~ ' ' | let arg1 = '"' . arg1 . '"' | endif
let arg2 = v:fname_new
if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif
let arg3 = v:fname_out
if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif
let eq = ''
if $VIMRUNTIME =~ ' '
if &sh =~ '\<cmd'
let cmd = '""' . $VIMRUNTIME . '\diff"'
let eq = '"'
else
let cmd = substitute($VIMRUNTIME, ' ', '" ', '') . '\diff"'
endif
else
let cmd = $VIMRUNTIME . '\diff'
endif
silent execute '!' . cmd . ' ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3 . eq
endfunction
endif
" auto encoding detecting
set fileencodings=ucs-bom,utf-8-bom,utf-8,cp936,big5,gb18030,ucs
let g:fencview_autodetect = 1
" set term encoding according to system locale
let &termencoding = substitute($LANG, "[a-zA-Z_-]*\.", "", "")
" support gnu syntaxt
let c_gnu = 1
" show error for mixed tab-space
let c_space_errors = 1
"let c_no_tab_space_error = 1
" don't show gcc statement expression ({x, y;}) as error
let c_no_curly_error = 1
" hilight characters over 100 columns
" match DiffAdd '\%>100v.*'
" hilight extra spaces at end of line
match Error '\s\+$'
let g:load_doxygen_syntax=1
" show tab as '--->'
" show trailing space as '-'
" set their color to darkgray
set listchars=tab:>-,trail:-
set list
highlight SpecialKey ctermfg=darkgray guifg=grey70
" fix vim quick fix
set errorformat^=%-GIn\ file\ included\ %.%#
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Key mappings section
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" F2 to search high lights
map <F2> :silent! nohlsearch<CR>
" jump to previous building error
map <F3> :cp<CR>
" jump to next building error
map <F4> :cn<CR>
" run make command
map <F5> :Build blade build<CR>
" run make clean command
map <F6> :Build blade clean<CR>
" alt .h .cpp
map <F7> :A<CR>
nnoremap <silent> <F8> :TlistToggle<CR>
map <silent><S-Left> :bprevious<CR>
map <silent><S-Right> :bnext<CR>
map <silent><S-End> :blast<CR>
map <silent><S-Home> :bfirst<CR>
" QUICKFIX WINDOW
" @see http://c9s.blogspot.com/2007/10/vim-quickfix-windows.html
command -bang -nargs=? QFix call QFixToggle(<bang>0)
function! QFixToggle(forced)
if exists("g:qfix_win") && a:forced == 0
cclose
unlet g:qfix_win
else
copen
let g:qfix_win = bufnr("$")
endif
endfunction
" toggle quickfix window
nmap <F9> :QFix<CR>
map <F10> :NERDTreeToggle<CR>
imap <F10> <ESC> :NERDTreeToggle<CR>
" netrw config. See https://shapeshed.com/vim-netrw/
let g:netrw_liststyle = 3 " Changing the directory view in netrw
let g:netrw_banner = 0 " Removing the banner
let g:netrw_browse_split = 1
let g:netrw_winsize = 25
let g:netrw_altv = 1
" https://vi.stackexchange.com/questions/10988/toggle-explorer-window
let g:NetrwIsOpen=0
function! ToggleNetrw()
if g:NetrwIsOpen
let i = bufnr("$")
while (i >= 1)
if (getbufvar(i, "&filetype") == "netrw")
silent exe "bwipeout " . i
endif
let i-=1
endwhile
let g:NetrwIsOpen=0
else
let g:NetrwIsOpen=1
silent Lexplore
endif
endfunction
" Add your own mapping. For example:
noremap <silent> <C-E> :call ToggleNetrw()<CR>
" F11 toggle paste mode
set pastetoggle=<F11>
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" auto commands section
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" remove trailing spaces
function! RemoveTrailingSpace()
if &filetype == 'sh' " here document required tabs to indent
return
endif
if $VIM_HATE_SPACE_ERRORS != '0'
normal m`
silent! :%s/\s\+$//e
normal ``
endif
endfunction
" apply gnu indent rule for system headers
function! GnuIndent()
setlocal cinoptions=>4,n-2,{2,^-2,:2,=2,g0,h2,p5,t0,+2,(0,u0,w1,m1
setlocal shiftwidth=2
setlocal tabstop=8
endfunction
" fix inconsist line ending
function! FixInconsistFileFormat()
if &fileformat == 'unix'
silent! :%s/\r$//e
endif
endfunction
autocmd BufWritePre * nested call FixInconsistFileFormat()
" custom indent: no namespace indent, fix template indent errors
function! CppNoNamespaceAndTemplateIndent()
let l:cline_num = line('.')
let l:cline = getline(l:cline_num)
let l:pline_num = prevnonblank(l:cline_num - 1)
let l:pline = getline(l:pline_num)
while l:pline =~# '\(^\s*{\s*\|^\s*//\|^\s*/\*\|\*/\s*$\)'
let l:pline_num = prevnonblank(l:pline_num - 1)
let l:pline = getline(l:pline_num)
endwhile
let l:retv = cindent('.')
let l:pindent = indent(l:pline_num)
if l:pline =~# '^\s*template\s*<\s*$'
let l:retv = l:pindent + &shiftwidth
elseif l:pline =~# '^\s*template\s*<.*>\s*$'
let l:retv = l:pindent
elseif l:pline =~# '\s*typename\s*.*,\s*$'
let l:retv = l:pindent
elseif l:pline =~# '\s*typename\s*.*>\s*$'
let l:retv = l:pindent - &shiftwidth
elseif l:cline =~# '^\s*>\s*$'
let l:retv = l:pindent - &shiftwidth
elseif l:pline =~# '^\s\+: \S.*' " C++ initialize list
let l:retv = l:pindent + 2
elseif l:pline =~# '^\s*namespace.*'
let l:retv = 0
endif
return l:retv
endfunction
autocmd FileType cpp nested setlocal indentexpr=CppNoNamespaceAndTemplateIndent()
augroup filetype
autocmd! BufRead,BufNewFile *.proto set filetype=proto
autocmd! BufRead,BufNewFile *.thrift set filetype=thrift
autocmd! BufRead,BufNewFile *.pump set filetype=pump
autocmd! BufRead,BufNewFile BUILD set filetype=blade
autocmd! BufRead,BufNewFile *.go set filetype=go
augroup end
" When editing a file, always jump to the last cursor position
autocmd BufReadPost * nested
\ if line("'\"") > 0 && line ("'\"") <= line("$") |
\ exe "normal g'\"" |
\ endif
autocmd BufEnter /*/include/c++/* nested setfiletype cpp
" autocmd BufEnter /usr/include/* nested call GnuIndent()
autocmd BufEnter */include/c++/*.*.*/* nested call GnuIndent()
autocmd BufWritePre * nested call RemoveTrailingSpace()
autocmd FileType make nested colorscheme murphy |
autocmd FileType python nested set ts=4 sw=4
autocmd FileType python syn keyword Function self
autocmd FileType go set noexpandtab ts=8 sw=8
function SetLogHighLight()
highlight LogFatal ctermbg=red guifg=red
highlight LogError ctermfg=red guifg=red
highlight LogWarning ctermfg=yellow guifg=yellow
highlight LogInfo ctermfg=green guifg=green
syntax match LogFatal "^F\d\+ .*$"
syntax match LogError "^E\d\+ .*$"
syntax match LogWarning "^W\d\+ .*$"
" syntax match LogInfo "^I\d\+ .*$"
endfunction
autocmd BufEnter *.{INFO,WARNING,ERROR,FATAL},*.log.{INFO,WARNING,ERROR,FATAL}.* nested call SetLogHighLight()
"autocmd BufEnter *.log match DiffAdd '\%>1024v.*'
" auto insert gtest header inclusion for test source file
autocmd BufNewFile *_test.{cpp,cxx,cc} nested :normal i#include "thirdparty/gtest/gtest.h"
" locate project dir by BLADE_ROOT file
functio! FindProjectRootDir()
let rootfile = findfile("BLADE_ROOT", ".;")
" in project root dir
if rootfile == "BLADE_ROOT"
return ""
endif
return substitute(rootfile, "/BLADE_ROOT$", "", "")
endfunction
" set path automatically
function! AutoSetPath()
let project_root = FindProjectRootDir()
if project_root != ""
exec "setlocal path+=" . project_root
endif
endfunction
autocmd FileType {c,cpp} nested call AutoSetPath()
" auto insert gtest header inclusion for test source file
function! s:InsertHeaderGuard()
let fullname = expand("%:p")
let rootdir = FindProjectRootDir()
if rootdir != ""
let path = substitute(fullname, "^" . rootdir . "/", "", "")
else
let path = expand("%")
endif
let varname = toupper(substitute(path, "[^a-zA-Z0-9]", "_", "g")) . "_"
exec 'norm O#ifndef ' . varname
exec 'norm o#define ' . varname
exec 'norm o#pragma once'
exec '$norm o#endif // ' . varname
endfunction
autocmd BufNewFile *.{h,hh.hxx,hpp} nested call <SID>InsertHeaderGuard()
autocmd QuickFixCmdPost * :QFix
" Auto paste mode, from
" https://coderwall.com/p/if9mda/automatically-set-paste-mode-in-vim-when-pasting-in-insert-mode
function! WrapForTmux(s)
if !exists('$TMUX')
return a:s
endif
let tmux_start = "\<Esc>Ptmux;"
let tmux_end = "\<Esc>\\"
return tmux_start . substitute(a:s, "\<Esc>", "\<Esc>\<Esc>", 'g') . tmux_end
endfunction
let &t_SI .= WrapForTmux("\<Esc>[?2004h")
let &t_EI .= WrapForTmux("\<Esc>[?2004l")
function! XTermPasteBegin()
set pastetoggle=<Esc>[201~
set paste
return ""
endfunction
inoremap <special> <expr> <Esc>[200~ XTermPasteBegin()
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Custom commands sections
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" integrate blade into vim
function! Build(build, ...)
let l:old_makeprg = &makeprg
let &makeprg=a:build
echo a:build
execute "make " . join(a:000)
let &makeprg=old_makeprg
endfunction
command! -complete=dir -nargs=* Build call Build('<args>')
" integrate cpplint into vim
function! CppLint(...)
let l:args = join(a:000)
if l:args == ""
let l:args = expand("%")
if l:args == ""
let l:args = '*'
endif
endif
let l:old_makeprg = &makeprg
setlocal makeprg=cpplint.py
execute "make " . l:args
let &makeprg=old_makeprg
endfunction
command! -complete=file -nargs=* CppLint call CppLint('<args>')
" integrate pychecker into vim
function! PyCheck(...)
let l:old_makeprg = &makeprg
setlocal makeprg=pychecker
execute "make -q " . join(a:000)
let &makeprg=old_makeprg
endfunction
command! -complete=file -nargs=* PyCheck call PyCheck('<args>')
" playback build log for vim quickfix
function! PlaybackBuildLog(...)
let l:old_makeprg = &makeprg
setlocal makeprg=cat
execute "make " . join(a:000)
let &makeprg=old_makeprg
endfunction
command! -complete=file -nargs=1 PlaybackBuildLog call PlaybackBuildLog('<args>')
if !exists("*plug#begin")
finish
endif
" The `vim-plug` plugin, see
" https://github.com/junegunn/vim-plug
call plug#begin('~/.vim/plugged')
" Shorthand notation; fetches https://github.com/junegunn/vim-easy-align
Plug 'junegunn/vim-easy-align'
Plug 'vim-scripts/a.vim'
" On-demand loading
Plug 'scrooloose/nerdtree', { 'on': 'NERDTreeToggle' }
Plug 'tpope/vim-fireplace', { 'for': 'clojure' }
" Using a non-default branch
Plug 'rdnetto/YCM-Generator', { 'branch': 'stable' }
Plug 'fatih/vim-go', { 'do': ':GoUpdateBinaries' }
Plug 'tpope/vim-vinegar'
" Unmanaged plugin (manually installed and updated)
" Plug '~/my-prototype-plugin'
" Initialize plugin system
call plug#end()