-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.vimrc.fast
477 lines (405 loc) · 14.6 KB
/
.vimrc.fast
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
" *************************************************************************
" presettings
" *************************************************************************
" ASCII art -- https://patorjk.com/software/taag/#p=display&f=Small&t=MY-VIMRC
let MY_VIMRC_WELCOME = "\n"
\ .' __ ____ __ __ _____ __ __ ___ ___ '."\n"
\ .'| \/ \ \ / /_\ \ / /_ _| \/ | _ \/ __|'."\n"
\ .'| |\/| |\ V /___\ V / | || |\/| | / (__ '."\n"
\ .'|_| |_| |_| \_/ |___|_| |_|_|_\\___|'."\n"
" vim data directory
if has('win32')
let DATA_DIR = expand($HOME.'/vimfiles')
else
let DATA_DIR = expand($HOME.'/.vim')
endif
" only use basic functions of my-vimrc -- 1:true 0:false
let MY_VIMRC_BASIC = 0
" mirrors for github site & github raw
let GITHUB_SITE = 'https://ghp.ci/https://github.com/'
"let GITHUB_SITE = 'https://www.ghproxy.cn/https://github.com/'
"let GITHUB_SITE = 'https://github.site/'
"let GITHUB_SITE = 'https://github.store/'
"let GITHUB_SITE = 'https://github.com/'
let GITHUB_RAW = 'https://ghp.ci/https://raw.githubusercontent.com/'
"let GITHUB_RAW = 'https://www.ghproxy.cn/https://raw.githubusercontent.com/'
"let GITHUB_RAW = 'https://raw.github.site/'
"let GITHUB_RAW = 'https://raw.github.store/'
"let GITHUB_RAW = 'https://raw.githubusercontent.com/'
" *************************************************************************
" basic settings
" *************************************************************************
set encoding=utf8
set nocompatible
if has('termguicolors')
set termguicolors
endif
" speed up vim scrolling
" https://stackoverflow.com/questions/307148/vim-scrolling-slowly
set ttyfast
set lazyredraw
" speed up syntax highlighting & regex performance
" https://vi.stackexchange.com/a/21641
" https://gist.github.com/glts/5646749
if exists('®expengine')
set regexpengine=1
endif
" to enable backspace key
" https://vi.stackexchange.com/a/2163
set backspace=indent,eol,start
set novisualbell
" to deal with REPLACE MODE problem on windows cmd or windows terminal
" https://superuser.com/a/1525060
set t_u7=
set t_Co=256
syntax on
set hlsearch
set incsearch
set ignorecase
set smartcase
set number
set relativenumber
set nowrap
set tabstop=4
set shiftwidth=4
set expandtab
set autoindent
set wildmode=longest,full
set wildmenu
set mouse=a
" mouse will still work beyond the 223rd col if vim supports mouse-sgr
" https://stackoverflow.com/a/19253251/15283141
if has("mouse_sgr")
set ttymouse=sgr
else
set ttymouse=xterm2
end
set laststatus=2
set splitbelow
set splitright
set dictionary+=/usr/share/dict/words
set complete+=k
" to be compatable with older version
" https://stackoverflow.com/a/36374234/15283141
if has("patch-7.4.710")
set listchars=eol:↵,tab:\|\|,trail:~,extends:>,precedes:<,space:·
else
set listchars=eol:↵,tab:\|\|,trail:~,extends:>,precedes:<
endif
set list
" Let's save undo info!
" from https://vi.stackexchange.com/a/53
let s:undo_dir = expand(DATA_DIR.'/undo-dir')
if !isdirectory(s:undo_dir)
call mkdir(s:undo_dir, "p", 0700)
endif
let &undodir = s:undo_dir
set undofile
" search tags file recursively
" https://stackoverflow.com/a/5019111/15283141
set tags=./tags,./TAGS,tags;~,TAGS;~
" *************************************************************************
" basic functions
" *************************************************************************
" a wrapper of input() but without the retval
function! s:ShowDialog(text)
if !has('gui_running')
"TODO: a trick only to keep the text shown on win32
if has('win32')
new | redraw | quit
echo "\n"
endif
call input(a:text)
else
call inputdialog(a:text)
endif
endfunction
" verify the first maxline of the downloaded file with the pattern
" retval: 0 -- good;
" -1 -- file not readable;
" -2 -- pattern not found in the first maxline of file
function! s:VerifyDownload(filename, pattern, maxline)
if !filereadable(a:filename)
echo '[my-vimrc] Download failed. "'.a:filename.'" is not found or not readable.'
return -1
endif
let lines = readfile(a:filename)
let line_count = len(lines)
let i = 0
while i < line_count && i < a:maxline
if lines[i] =~ a:pattern
return 0
endif
let i += 1
endwhile
echo '[my-vimrc] Verification failed. "'.a:filename.'" might be corrupted.'
return -2
endfunction
" function to run a shell
" https://stackoverflow.com/questions/1236563/how-do-i-run-a-terminal-inside-of-vim
function! OpenShell()
if v:version < 801
echo "Press any key to open a shell..."
echo "After the shell opened, press <C-d> or type 'exit' to quit the shell."
call getchar()
sh
else
bo 10sp | term ++curwin
" enable the shortcut to close the shell
if has('win32')
tnoremap <silent> <F3> <C-End><C-Home>exit<CR>
else
tnoremap <silent> <F3> <C-a><C-k><C-d>
endif
endif
endfunction
" allow toggling between local and default mode
" https://vim.fandom.com/wiki/Toggle_between_tabs_and_spaces
function! TabToggle()
if &expandtab
set noexpandtab
else
set expandtab
endif
endfunction
" *************************************************************************
" basic keymaps
" *************************************************************************
" quickly edit this config file
nnoremap <leader>ve :tabnew $MYVIMRC<CR>
" quickly save and source this config file
nnoremap <leader>vs :wa<Bar>so $MYVIMRC<CR>
" toggle paste mode
nnoremap <leader>p :set paste!<CR>
" toggle list char
nnoremap <leader>l :set list!<CR>
" toggle tab/spaces
nnoremap <leader>t :call TabToggle()<CR>
" strip trailing whitespaces
" https://vi.stackexchange.com/a/2285
nnoremap <leader>s :let _s=@/<Bar>:%s/\s\+$//e<Bar>:let @/=_s<Bar><CR>
" switch between tabs
nnoremap <leader>} :tabnext<CR>
nnoremap <leader>{ :tabprevious<CR>
" switch between buffers
nnoremap <leader>] :bnext<CR>
nnoremap <leader>[ :bprevious<CR>
" *************************************************************************
" plugin manager
" *************************************************************************
" only use basic functions of my-vimrc
if MY_VIMRC_BASIC != 0
finish
endif
" first we check for git; finish execution if no git is found
if !executable('git')
call s:ShowDialog('[my-vimrc] The "git" command is missing. '
\ .'Only basic functions are available.'
\ ."\nPress ENTER to continue\n")
finish
endif
let AUTOLOAD_DIR = expand(DATA_DIR.'/autoload')
let PLUGIN_MANAGER_PATH = expand(AUTOLOAD_DIR.'/plug.vim')
let PLUGIN_MANAGER_URL = GITHUB_RAW.'/junegunn/vim-plug/master/plug.vim'
let PLUGIN_MANAGER_PATTERN = ':PlugInstall'
" download the plugin manager if not installed
silent if s:VerifyDownload(PLUGIN_MANAGER_PATH, PLUGIN_MANAGER_PATTERN, 1000) != 0
" welcome our beloved user
call s:ShowDialog(MY_VIMRC_WELCOME."\n[my-vimrc] Thank you for using my-vimrc!\n"
\ ."Press ENTER to download the plugin manager\n")
" try different ways to download the plugin manager
if has('win32') && executable('powershell')
silent execute '!powershell "iwr -useb '.PLUGIN_MANAGER_URL.' |` '
\ .'ni '.PLUGIN_MANAGER_PATH.' -Force"'
elseif has('win32') && executable('certutil')
silent execute '!(if not exist "'.AUTOLOAD_DIR.'" mkdir "'.AUTOLOAD_DIR.'")'
\ .'&& certutil -urlcache -split -f "'.PLUGIN_MANAGER_URL.'"'
\ .' "'.PLUGIN_MANAGER_PATH.'"'
elseif executable('wget')
silent execute '!mkdir -p '.AUTOLOAD_DIR.' '
\ .'&& wget -O '.PLUGIN_MANAGER_PATH.' '.PLUGIN_MANAGER_URL.' '
\ .'&& echo "Download successful." || echo "Download failed." '
elseif executable('curl')
silent execute '!curl -fLo '.PLUGIN_MANAGER_PATH
\ .' --create-dirs '.PLUGIN_MANAGER_URL.' '
\ .'&& echo "Download successful." || echo "Download failed." '
else
echo '[my-vimrc] No downloader available.'
endif
" verify the downloaded file; finish the execution if failed
if s:VerifyDownload(PLUGIN_MANAGER_PATH, PLUGIN_MANAGER_PATTERN, 1000) != 0
call s:ShowDialog('[my-vimrc] Unable to download the plugin manager. '
\ .'Only basic functions are available. '
\ ."\nPlease manually download the plugin manager from "
\ .'"https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim"'
\ .' and save it as "'.PLUGIN_MANAGER_PATH.'"'
\ ."\nPress ENTER to continue\n")
finish
else
autocmd VimEnter * PlugInstall --sync | source $MYVIMRC
endif
endif
call plug#begin()
" --------------------
" color schemes
" --------------------
Plug GITHUB_SITE.'flazz/vim-colorschemes'
" --------------------
" mostly used
" --------------------
Plug GITHUB_SITE.'preservim/nerdtree', { 'on': 'NERDTreeToggle' }
Plug GITHUB_SITE.'rbong/vim-crystalline', { 'branch': 'vim-7' }
Plug GITHUB_SITE.'mbbill/undotree'
Plug GITHUB_SITE.'preservim/tagbar', { 'on': 'TagbarToggle' }
Plug GITHUB_SITE.'NewComer00/ack.vim', { 'branch': 'patch-1' }
Plug GITHUB_SITE.'vim-scripts/YankRing.vim'
" --------------------
" more convenience
" --------------------
if has('timers')
Plug GITHUB_SITE.'delphinus/vim-auto-cursorline'
endif
Plug GITHUB_SITE.'luochen1990/rainbow'
Plug GITHUB_SITE.'gosukiwi/vim-smartpairs'
Plug GITHUB_SITE.'airblade/vim-rooter'
Plug GITHUB_SITE.'junegunn/vim-peekaboo'
Plug GITHUB_SITE.'tpope/vim-commentary'
Plug GITHUB_SITE.'farmergreg/vim-lastplace'
" system clipboard
if has('patch-8.2.1337')
" https://github.com/vim/vim/issues/6591
Plug GITHUB_SITE.'ojroques/vim-oscyank'
Plug GITHUB_SITE.'christoomey/vim-system-copy'
else
Plug GITHUB_SITE.'ojroques/vim-oscyank', { 'commit': '14685fc' }
Plug GITHUB_SITE.'christoomey/vim-system-copy', { 'commit': '1e5afc4' }
endif
" git related
Plug GITHUB_SITE.'tpope/vim-fugitive'
Plug GITHUB_SITE.'junegunn/gv.vim'
" vim performance
if has('timers') && has('terminal')
Plug GITHUB_SITE.'dstein64/vim-startuptime'
else
Plug GITHUB_SITE.'NewComer00/startuptime.vim', { 'branch': 'patch-1' }
endif
call plug#end()
" *************************************************************************
" plugin configs
" *************************************************************************
" flazz/vim-colorschemes
if exists('plugs') && has_key(plugs, 'vim-colorschemes')
\ && filereadable(plugs['vim-colorschemes']['dir'].'/colors/molokai.vim')
colorscheme molokai
endif
" rbong/vim-crystalline
function! StatusLine(...)
return crystalline#mode() . crystalline#right_mode_sep('Line')
\ . ' %f%h%w%m%r ' . crystalline#right_sep('Line', 'Fill') . '%='
\ . crystalline#left_sep('Line', 'Fill')
\ . ' %{&ft} [%{&fenc!=#""?&fenc:&enc}] [%{&ff}] Line:%l/%L Col:%c%V %P '
endfunction
let g:crystalline_statusline_fn = 'StatusLine'
let g:crystalline_theme = 'jellybeans'
" preservim/nerdtree
let NERDTreeWinPos="right"
let NERDTreeShowHidden=1
let NERDTreeMouseMode=2
" disable the original file explorer
let g:loaded_netrw = 1
let g:loaded_netrwPlugin = 1
" lazy load nerdtree when open a directory
" https://github.com/junegunn/vim-plug/issues/424#issuecomment-189343357
augroup nerd_loader
autocmd!
autocmd VimEnter * silent! autocmd! FileExplorer
autocmd BufEnter,BufNew *
\ if isdirectory(expand('<amatch>'))
\| call plug#load('nerdtree')
\| execute 'autocmd! nerd_loader'
\| endif
augroup END
" preservim/tagbar
let g:tagbar_position = 'vertical leftabove'
let g:tagbar_width = max([25, winwidth(0) / 5])
" mileszs/ack.vim
if executable('ag')
let g:ackprg = 'ag --vimgrep --hidden --ignore .git'
endif
" rainbow/luochen1990
let g:rainbow_active = 1
let g:rainbow_conf = {
\ 'separately': {
\ 'nerdtree': 0,
\ }
\}
" vim-scripts/YankRing.vim
" to avoid <C-p> collision with the ctrlp plugin
let g:yankring_replace_n_pkey = '<m-p>'
let g:yankring_replace_n_nkey = '<m-n>'
" save yankring history in this dir
let s:yankring_dir = expand(DATA_DIR.'/yankring-dir')
if !isdirectory(s:yankring_dir)
call mkdir(s:yankring_dir, "p", 0700)
endif
let g:yankring_history_dir = s:yankring_dir
" christoomey/vim-system-copy
let g:system_copy_enable_osc52 = 1
if has('win32') && executable('powershell')
" force cmd.exe to use utf-8 encoding
" https://stackoverflow.com/questions/57131654/using-utf-8-encoding-chcp-65001-in-command-prompt-windows-powershell-window
call system('chcp 65001')
" https://github.com/christoomey/vim-system-copy/pull/35#issue-557371087
let g:system_copy#paste_command='powershell "Get-Clipboard"'
endif
" *************************************************************************
" extra functions
" *************************************************************************
" test the startup time of Vim
function! TimingVimStartup(sorted)
let l:cmd = ''
if has('timers') && has('terminal')
let l:cmd = 'StartupTime --tries 10'
if a:sorted == 0
let l:cmd .= ' --no-sort'
endif
else
let l:cmd = 'StartupTime'
endif
execute(l:cmd)
endfunction
" *************************************************************************
" extra keymaps
" *************************************************************************
" functional hotkeys for plugins
nnoremap <silent> <F2> :NERDTreeToggle<CR>
nnoremap <silent> <F3> :call OpenShell()<CR>
nnoremap <silent> <F4> :UndotreeToggle<CR>
nnoremap <silent> <F7> :YRShow<CR>
nnoremap <silent> <F8> :TagbarToggle<CR>
nnoremap <F9> :AckFile!<Space>
inoremap <silent> <F2> <Esc>:NERDTreeToggle<CR>
inoremap <silent> <F3> <Esc>:call OpenShell()<CR>
inoremap <silent> <F4> <Esc>:UndotreeToggle<CR>
inoremap <silent> <F7> <Esc>:YRShow<CR>
inoremap <silent> <F8> <Esc>:TagbarToggle<CR>
inoremap <F9> <Esc>:AckFile!<Space>
cnoremap <silent> <F9> <C-c>
" plugin manager shortcuts
nnoremap <leader>vi :wa<Bar>silent! so $MYVIMRC<CR>:PlugInstall<CR>
nnoremap <leader>vc :wa<Bar>silent! so $MYVIMRC<CR>:PlugClean<CR>
nnoremap <leader>vu :wa<Bar>silent! so $MYVIMRC<CR>:PlugUpdate<CR>
" test vim startup time
nnoremap <leader>vt :call TimingVimStartup(1)<CR>
nnoremap <leader>vT :call TimingVimStartup(0)<CR>
" search the word under the cursor
nnoremap <leader>a :Ack!<CR>
" search the given word
nnoremap <leader>A :Ack!<Space>
" christoomey/vim-system-copy
nmap cy <Plug>SystemCopy
xmap cy <Plug>SystemCopy
nmap cY <Plug>SystemCopyLine
nmap cp <Plug>SystemPaste
xmap cp <Plug>SystemPaste
nmap cP <Plug>SystemPasteLine