-
Notifications
You must be signed in to change notification settings - Fork 169
/
.vimrc
505 lines (429 loc) · 18 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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
"/////////////////////////////////////////////////////////////////////////////
" basic
"/////////////////////////////////////////////////////////////////////////////
set nocompatible " be iMproved, required
function! OSX()
return has('macunix')
endfunction
function! LINUX()
return has('unix') && !has('macunix') && !has('win32unix')
endfunction
function! WINDOWS()
return (has('win16') || has('win32') || has('win64'))
endfunction
" On Windows, also use '.vim' instead of 'vimfiles'; this makes synchronization
" across (heterogeneous) systems easier.
if !exists('g:exvim_custom_path')
if WINDOWS()
set runtimepath=$HOME/.vim,$VIM/vimfiles,$VIMRUNTIME,$VIM/vimfiles/after,$HOME/.vim/after
endif
endif
"/////////////////////////////////////////////////////////////////////////////
" language and encoding setup
"/////////////////////////////////////////////////////////////////////////////
" always use English menu
" NOTE: this must before filetype off, otherwise it won't work
set langmenu=none
" use English for anaything in vim-editor.
if WINDOWS()
silent exec 'language english'
elseif OSX()
silent exec 'language en_US'
else
let s:uname = system("uname -s")
if s:uname == "Darwin\n"
" in mac-terminal
silent exec 'language en_US'
else
" in linux-terminal
silent exec 'language en_US.utf8'
endif
endif
" try to set encoding to utf-8
if WINDOWS()
" Be nice and check for multi_byte even if the config requires
" multi_byte support most of the time
if has('multi_byte')
" Windows cmd.exe still uses cp850. If Windows ever moved to
" Powershell as the primary terminal, this would be utf-8
set termencoding=cp850
" Let Vim use utf-8 internally, because many scripts require this
set encoding=utf-8
setglobal fileencoding=utf-8
" Windows has traditionally used cp1252, so it's probably wise to
" fallback into cp1252 instead of eg. iso-8859-15.
" Newer Windows files might contain utf-8 or utf-16 LE so we might
" want to try them first.
set fileencodings=ucs-bom,utf-8,utf-16le,cp1252,iso-8859-15
endif
else
" set default encoding to utf-8
set encoding=utf-8
set termencoding=utf-8
endif
scriptencoding utf-8
"/////////////////////////////////////////////////////////////////////////////
" Bundle steup
"/////////////////////////////////////////////////////////////////////////////
" vundle#begin
filetype off " required
" set the runtime path to include Vundle
if exists('g:exvim_custom_path')
let g:ex_tools_path = g:exvim_custom_path.'/vimfiles/tools/'
exec 'set rtp+=' . fnameescape ( g:exvim_custom_path.'/vimfiles/bundle/Vundle.vim/' )
call vundle#rc(g:exvim_custom_path.'/vimfiles/bundle/')
else
let g:ex_tools_path = '~/.vim/tools/'
set rtp+=~/.vim/bundle/Vundle.vim/
call vundle#rc('~/.vim/bundle/')
endif
" load .vimrc.plugins & .vimrc.plugins.local
if exists('g:exvim_custom_path')
let vimrc_plugins_path = g:exvim_custom_path.'/.vimrc.plugins'
let vimrc_plugins_local_path = g:exvim_custom_path.'/.vimrc.plugins.local'
else
let vimrc_plugins_path = '~/.vimrc.plugins'
let vimrc_plugins_local_path = '~/.vimrc.plugins.local'
endif
if filereadable(expand(vimrc_plugins_path))
exec 'source ' . fnameescape(vimrc_plugins_path)
endif
if filereadable(expand(vimrc_plugins_local_path))
exec 'source ' . fnameescape(vimrc_plugins_local_path)
endif
" vundle#end
filetype plugin indent on " required
syntax on " required
"/////////////////////////////////////////////////////////////////////////////
" Default colorscheme setup
"/////////////////////////////////////////////////////////////////////////////
if has('gui_running')
set background=dark
else
set background=dark
set t_Co=256 " make sure our terminal use 256 color
let g:solarized_termcolors = 256
endif
colorscheme solarized
" colorscheme exlightgray
" colorscheme gruvbox
"/////////////////////////////////////////////////////////////////////////////
" General
"/////////////////////////////////////////////////////////////////////////////
"set path=.,/usr/include/*,, " where gf, ^Wf, :find will search
set backup " make backup file and leave it around
" setup back and swap directory
let data_dir = $HOME.'/.data/'
let backup_dir = data_dir . 'backup'
let swap_dir = data_dir . 'swap'
if finddir(data_dir) == ''
silent call mkdir(data_dir)
endif
if finddir(backup_dir) == ''
silent call mkdir(backup_dir)
endif
if finddir(swap_dir) == ''
silent call mkdir(swap_dir)
endif
unlet backup_dir
unlet swap_dir
unlet data_dir
set backupdir=$HOME/.data/backup " where to put backup file
set directory=$HOME/.data/swap " where to put swap file
" Redefine the shell redirection operator to receive both the stderr messages and stdout messages
set shellredir=>%s\ 2>&1
set history=50 " keep 50 lines of command line history
set updatetime=1000 " default = 4000
set autoread " auto read same-file change ( better for vc/vim change )
set maxmempattern=1000 " enlarge maxmempattern from 1000 to ... (2000000 will give it without limit)
"/////////////////////////////////////////////////////////////////////////////
" xterm settings
"/////////////////////////////////////////////////////////////////////////////
behave xterm " set mouse behavior as xterm
if &term =~ 'xterm'
set mouse=a
endif
"/////////////////////////////////////////////////////////////////////////////
" Variable settings ( set all )
"/////////////////////////////////////////////////////////////////////////////
" ------------------------------------------------------------------
" Desc: Visual
" ------------------------------------------------------------------
set matchtime=0 " 0 second to show the matching paren ( much faster )
set nu " show line number
set scrolloff=0 " minimal number of screen lines to keep above and below the cursor
set nowrap " do not wrap text
" only supoort in 7.3 or higher
if v:version >= 703
set noacd " no autochchdir
endif
" set default guifont
if has('gui_running')
augroup ex_gui_font
" check and determine the gui font after GUIEnter.
" NOTE: getfontname function only works after GUIEnter.
au!
au GUIEnter * call s:set_gui_font()
augroup END
" set guifont
function! s:set_gui_font()
if has('gui_gtk2')
if getfontname( 'DejaVu Sans Mono for Powerline' ) != ''
set guifont=DejaVu\ Sans\ Mono\ for\ Powerline\ 12
elseif getfontname( 'DejaVu Sans Mono' ) != ''
set guifont=DejaVu\ Sans\ Mono\ 12
else
set guifont=Luxi\ Mono\ 12
endif
elseif has('x11')
" Also for GTK 1
set guifont=*-lucidatypewriter-medium-r-normal-*-*-180-*-*-m-*-*
elseif OSX()
if getfontname( 'DejaVu Sans Mono for Powerline' ) != ''
set guifont=DejaVu\ Sans\ Mono\ for\ Powerline:h15
elseif getfontname( 'DejaVu Sans Mono' ) != ''
set guifont=DejaVu\ Sans\ Mono:h15
endif
elseif WINDOWS()
if getfontname( 'DejaVu Sans Mono for Powerline' ) != ''
set guifont=DejaVu\ Sans\ Mono\ for\ Powerline:h11:cANSI
elseif getfontname( 'DejaVu Sans Mono' ) != ''
set guifont=DejaVu\ Sans\ Mono:h11:cANSI
elseif getfontname( 'Consolas' ) != ''
set guifont=Consolas:h11:cANSI " this is the default visual studio font
else
set guifont=Lucida_Console:h11:cANSI
endif
endif
endfunction
endif
" ------------------------------------------------------------------
" Desc: Vim UI
" ------------------------------------------------------------------
set wildmenu " turn on wild menu, try typing :h and press <Tab>
set showcmd " display incomplete commands
set cmdheight=1 " 1 screen lines to use for the command-line
set ruler " show the cursor position all the time
set hidden " allow to change buffer without saving
set shortmess=aoOtTI " shortens messages to avoid 'press a key' prompt
set lazyredraw " do not redraw while executing macros (much faster)
set display+=lastline " for easy browse last line with wrap text
set laststatus=2 " always have status-line
set titlestring=%t\ (%{expand(\"%:p:.:h\")}/)
" set window size (if it's GUI)
if has('gui_running')
" set window's width to 130 columns and height to 40 rows
if exists('+lines')
set lines=40
endif
if exists('+columns')
set columns=130
endif
" DISABLE
" if WINDOWS()
" au GUIEnter * simalt ~x " Maximize window when enter vim
" else
" " TODO: no way right now
" endif
endif
set showfulltag " show tag with function protype.
set guioptions+=b " present the bottom scrollbar when the longest visible line exceed the window
" disable menu & toolbar
set guioptions-=m
set guioptions-=T
" ------------------------------------------------------------------
" Desc: Text edit
" ------------------------------------------------------------------
set ai " autoindent
set si " smartindent
set backspace=indent,eol,start " allow backspacing over everything in insert mode
" indent options
" see help cinoptions-values for more details
set cinoptions=>s,e0,n0,f0,{0,}0,^0,:0,=s,l0,b0,g0,hs,ps,ts,is,+s,c3,C0,0,(0,us,U0,w0,W0,m0,j0,)20,*30
" default '0{,0},0),:,0#,!^F,o,O,e' disable 0# for not ident preprocess
" set cinkeys=0{,0},0),:,!^F,o,O,e
" official diff settings
set diffexpr=g:MyDiff()
function! g:MyDiff()
let opt = '-a --binary -w '
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
silent execute '!' . 'diff ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3
endfunction
set cindent shiftwidth=2 " set cindent on to autoinent when editing c/c++ file, with 4 shift width
set tabstop=2 " set tabstop to 4 characters
set expandtab " set expandtab on, the tab will be change to space automaticaly
set ve=block " in visual block mode, cursor can be positioned where there is no actual character
" set Number format to null(default is octal) , when press CTRL-A on number
" like 007, it would not become 010
set nf=
" ------------------------------------------------------------------
" Desc: Fold text
" ------------------------------------------------------------------
set foldmethod=marker foldmarker={,} foldlevel=9999
set diffopt=filler,context:9999
" ------------------------------------------------------------------
" Desc: Search
" ------------------------------------------------------------------
set showmatch " show matching paren
set incsearch " do incremental searching
set hlsearch " highlight search terms
set ignorecase " set search/replace pattern to ignore case
set smartcase " set smartcase mode on, If there is upper case character in the search patern, the 'ignorecase' option will be override.
" set this to use id-utils for global search
set grepprg=lid\ -Rgrep\ -s
set grepformat=%f:%l:%m
"/////////////////////////////////////////////////////////////////////////////
" Auto Command
"/////////////////////////////////////////////////////////////////////////////
" ------------------------------------------------------------------
" Desc: Only do this part when compiled with support for autocommands.
" ------------------------------------------------------------------
if has('autocmd')
augroup ex
au!
" ------------------------------------------------------------------
" Desc: Buffer
" ------------------------------------------------------------------
" when editing a file, always jump to the last known cursor position.
" don't do it when the position is invalid or when inside an event handler
" (happens when dropping a file on gvim).
au BufReadPost *
\ if line("'\"") > 0 && line("'\"") <= line("$") |
\ exe "normal g`\"" |
\ endif
au BufNewFile,BufEnter * set cpoptions+=d " NOTE: ctags find the tags file from the current path instead of the path of currect file
au BufEnter * :syntax sync fromstart " ensure every file does syntax highlighting (full)
au BufNewFile,BufRead *.avs set syntax=avs " for avs syntax file.
" DISABLE {
" NOTE: will have problem with exvim, because exvim use exES_CWD as working directory for tag and other thing
" Change current directory to the file of the buffer ( from Script#65"CD.vim"
" au BufEnter * execute ":lcd " . expand("%:p:h")
" } DISABLE end
" ------------------------------------------------------------------
" Desc: file types
" ------------------------------------------------------------------
au FileType text setlocal textwidth=78 " for all text files set 'textwidth' to 78 characters.
au FileType c,cpp,cs,swig set nomodeline " this will avoid bug in my project with namespace ex, the vim will tree ex:: as modeline.
" disable auto-comment for c/cpp, lua, javascript, c# and vim-script
au FileType c,cpp,java,javascript set comments=sO:*\ -,mO:*\ \ ,exO:*/,s1:/*,mb:*,ex:*/,f://
au FileType cs set comments=sO:*\ -,mO:*\ \ ,exO:*/,s1:/*,mb:*,ex:*/,f:///,f://
au FileType vim set comments=sO:\"\ -,mO:\"\ \ ,eO:\"\",f:\"
au FileType lua set comments=f:--
" if edit python scripts, check if have \t. ( python said: the programme can only use \t or not, but can't use them together )
au FileType python,coffee call s:check_if_expand_tab()
augroup END
function! s:check_if_expand_tab()
let has_noexpandtab = search('^\t','wn')
let has_expandtab = search('^ ','wn')
"
if has_noexpandtab && has_expandtab
let idx = inputlist ( ['ERROR: current file exists both expand and noexpand TAB, python can only use one of these two mode in one file.\nSelect Tab Expand Type:',
\ '1. expand (tab=space, recommended)',
\ '2. noexpand (tab=\t, currently have risk)',
\ '3. do nothing (I will handle it by myself)'])
let tab_space = printf('%*s',&tabstop,'')
if idx == 1
let has_noexpandtab = 0
let has_expandtab = 1
silent exec '%s/\t/' . tab_space . '/g'
elseif idx == 2
let has_noexpandtab = 1
let has_expandtab = 0
silent exec '%s/' . tab_space . '/\t/g'
else
return
endif
endif
"
if has_noexpandtab == 1 && has_expandtab == 0
echomsg 'substitute space to TAB...'
set noexpandtab
echomsg 'done!'
elseif has_noexpandtab == 0 && has_expandtab == 1
echomsg 'substitute TAB to space...'
set expandtab
echomsg 'done!'
else
" it may be a new file
" we use original vim setting
endif
endfunction
endif
"/////////////////////////////////////////////////////////////////////////////
" Key Mappings
"/////////////////////////////////////////////////////////////////////////////
" NOTE: F10 looks like have some feature, when map with F10, the map will take no effects
" Don't use Ex mode, use Q for formatting
map Q gq
" define the copy/paste judged by clipboard
if &clipboard ==# 'unnamed'
" fix the visual paste bug in vim
" vnoremap <silent>p :call g:()<CR>
else
" general copy/paste.
" NOTE: y,p,P could be mapped by other key-mapping
map <leader>y "*y
map <leader>p "*p
map <leader>P "*P
endif
" copy folder path to clipboard, foo/bar/foobar.c => foo/bar/
nnoremap <silent> <leader>y1 :let @*=fnamemodify(bufname('%'),":p:h")<CR>
" copy file name to clipboard, foo/bar/foobar.c => foobar.c
nnoremap <silent> <leader>y2 :let @*=fnamemodify(bufname('%'),":p:t")<CR>
" copy full path to clipboard, foo/bar/foobar.c => foo/bar/foobar.c
nnoremap <silent> <leader>y3 :let @*=fnamemodify(bufname('%'),":p")<CR>
" F8 or <leader>/: Set Search pattern highlight on/off
nnoremap <F8> :let @/=""<CR>
nnoremap <leader>/ :let @/=""<CR>
" DISABLE: though nohlsearch is standard way in Vim, but it will not erase the
" search pattern, which is not so good when use it with exVim's <leader>r
" filter method
" nnoremap <F8> :nohlsearch<CR>
" nnoremap <leader>/ :nohlsearch<CR>
" map Ctrl-Tab to switch window
nnoremap <S-Up> <C-W><Up>
nnoremap <S-Down> <C-W><Down>
nnoremap <S-Left> <C-W><Left>
nnoremap <S-Right> <C-W><Right>
" easy buffer navigation
" NOTE: if we already map to EXbn,EXbp. skip setting this
if !hasmapto(':EXbn<CR>') && mapcheck('<C-l>','n') == ''
nnoremap <C-l> :bn<CR>
endif
if !hasmapto(':EXbp<CR>') && mapcheck('<C-h>','n') == ''
noremap <C-h> :bp<CR>
endif
" easy diff goto
noremap <C-k> [c
noremap <C-j> ]c
" enhance '<' '>' , do not need to reselect the block after shift it.
vnoremap < <gv
vnoremap > >gv
" map Up & Down to gj & gk, helpful for wrap text edit
noremap <Up> gk
noremap <Down> gj
" TODO: I should write a better one, make it as plugin exvim/swapword
" VimTip 329: A map for swapping words
" http://vim.sourceforge.net/tip_view.php?tip_id=
" Then when you put the cursor on or in a word, press "\sw", and
" the word will be swapped with the next word. The words may
" even be separated by punctuation (such as "abc = def").
nnoremap <silent> <leader>sw "_yiw:s/\(\%#\w\+\)\(\W\+\)\(\w\+\)/\3\2\1/<cr><c-o>
"/////////////////////////////////////////////////////////////////////////////
" local setup
"/////////////////////////////////////////////////////////////////////////////
let vimrc_local_path = '~/.vimrc.local'
if exists('g:exvim_custom_path')
let vimrc_local_path = g:exvim_custom_path.'/.vimrc.local'
endif
if filereadable(expand(vimrc_local_path))
exec 'source ' . fnameescape(vimrc_local_path)
endif
" vim:ts=4:sw=4:sts=4 et fdm=marker: