-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.vimrc.nine
536 lines (457 loc) · 16.2 KB
/
.vimrc.nine
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
vim9script
# *************************************************************************
# presettings
# *************************************************************************
# ASCII art -- https://patorjk.com/software/taag/#p=display&f=Small&t=MY-VIMRC
var MY_VIMRC_WELCOME = "\n"
.. ' __ ____ __ __ _____ __ __ ___ ___ ' .. "\n"
.. '| \/ \ \ / /_\ \ / /_ _| \/ | _ \/ __|' .. "\n"
.. '| |\/| |\ V /___\ V / | || |\/| | / (__ ' .. "\n"
.. '|_| |_| |_| \_/ |___|_| |_|_|_\\___|' .. "\n"
# vim data directory
var DATA_DIR = expand($HOME .. '/.vim')
if has('win32')
DATA_DIR = expand($HOME .. '/vimfiles')
endif
# only use basic functions of my-vimrc -- 1:true 0:false
var MY_VIMRC_BASIC = 0
# mirrors for github site & github raw
g:GITHUB_SITE = 'https://ghp.ci/https://github.com/'
# g:GITHUB_SITE = 'https://www.ghproxy.cn/https://github.com/'
# g:GITHUB_SITE = 'https://github.site/'
# g:GITHUB_SITE = 'https://github.store/'
# g:GITHUB_SITE = 'https://github.com/'
g:GITHUB_RAW = 'https://ghp.ci/https://raw.githubusercontent.com/'
# g:GITHUB_RAW = 'https://www.ghproxy.cn/https://raw.githubusercontent.com/'
# g:GITHUB_RAW = 'https://raw.github.site/'
# g:GITHUB_RAW = 'https://raw.github.store/'
# g:GITHUB_RAW = 'https://raw.githubusercontent.com/'
# *************************************************************************
# basic settings
# *************************************************************************
set encoding=utf8
set nocompatible
if has('termguicolors')
set termguicolors
endif
set cursorline
# 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
endif
set splitbelow
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
var undo_dir = expand(DATA_DIR .. '/undo-dir')
if !isdirectory(undo_dir)
mkdir(undo_dir, "p", 0o700)
endif
&undodir = 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
def ShowDialog(text: string)
if !has('gui_running')
# TODO: a trick only to keep the text shown on win32
if has('win32')
new | redraw | quit
echo "\n"
endif
input(text)
else
inputdialog(text)
endif
enddef
# 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
def VerifyDownload(filename: string, pattern: string, maxline: number): number
if !filereadable(filename)
echo '[my-vimrc] Download failed. "' .. filename .. '" is not found or not readable.'
return -1
endif
var lines = readfile(filename)
var line_count = len(lines)
var i = 0
while i < line_count && i < maxline
if lines[i] =~ pattern
return 0
endif
i += 1
endwhile
echo '[my-vimrc] Verification failed. "' .. filename .. '" might be corrupted.'
return -2
enddef
# allow toggling between local and default mode
# https://vim.fandom.com/wiki/Toggle_between_tabs_and_spaces
def TabToggle()
if &expandtab
set noexpandtab
else
set expandtab
endif
enddef
# *************************************************************************
# basic filetypes
# *************************************************************************
# scons
augroup scons_ft
au!
autocmd bufnewfile,bufread sconstruct set filetype=python
augroup end
# *************************************************************************
# 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 <SID>TabToggle()<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')
ShowDialog('[my-vimrc] The "git" command is missing. '
.. 'Only basic functions are available.'
.. "\nPress ENTER to continue\n")
finish
endif
var AUTOLOAD_DIR = expand(DATA_DIR .. '/autoload')
var PLUGIN_MANAGER_PATH = expand(AUTOLOAD_DIR .. '/plug.vim')
var PLUGIN_MANAGER_URL = g:GITHUB_RAW .. '/junegunn/vim-plug/master/plug.vim'
var PLUGIN_MANAGER_PATTERN = ':PlugInstall'
# download the plugin manager if not installed
silent if VerifyDownload(PLUGIN_MANAGER_PATH, PLUGIN_MANAGER_PATTERN, 1000) != 0
# welcome our beloved user
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 VerifyDownload(PLUGIN_MANAGER_PATH, PLUGIN_MANAGER_PATTERN, 1000) != 0
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
plug#begin()
# --------------------
# color schemes
# --------------------
Plug g:GITHUB_SITE .. 'flazz/vim-colorschemes'
# --------------------
# mostly used
# --------------------
Plug g:GITHUB_SITE .. 'preservim/nerdtree', { 'on': 'NERDTreeToggle' }
Plug g:GITHUB_SITE .. 'vim-airline/vim-airline'
if has('terminal')
Plug g:GITHUB_SITE .. 'voldikss/vim-floaterm'
else
Plug g:GITHUB_SITE .. 'Shougo/vimproc.vim', { 'do': 'make' }
Plug g:GITHUB_SITE .. 'Shougo/vimshell.vim', { 'on': 'VimShellPop' }
Plug g:GITHUB_SITE .. 'supermomonga/vimshell-inline-history.vim'
endif
Plug g:GITHUB_SITE .. 'mbbill/undotree'
Plug g:GITHUB_SITE .. 'preservim/tagbar', { 'on': 'TagbarToggle' }
# finders
Plug g:GITHUB_SITE .. 'ctrlpvim/ctrlp.vim'
Plug g:GITHUB_SITE .. 'NewComer00/ack.vim', { 'branch': 'patch-1' }
Plug g:GITHUB_SITE .. 'vim-scripts/YankRing.vim'
# --------------------
# more convenience
# --------------------
Plug g:GITHUB_SITE .. 'luochen1990/rainbow'
Plug g:GITHUB_SITE .. 'itchyny/vim-cursorword.git'
Plug g:GITHUB_SITE .. 'ntpeters/vim-better-whitespace'
Plug g:GITHUB_SITE .. 'gosukiwi/vim-smartpairs'
Plug g:GITHUB_SITE .. 'tpope/vim-surround'
Plug g:GITHUB_SITE .. 'airblade/vim-rooter'
Plug g:GITHUB_SITE .. 'junegunn/vim-peekaboo'
Plug g:GITHUB_SITE .. 'tpope/vim-commentary'
Plug g:GITHUB_SITE .. 'farmergreg/vim-lastplace'
Plug g:GITHUB_SITE .. 'unblevable/quick-scope'
# system clipboard
if has('patch-8.2.1337')
# https://github.com/vim/vim/issues/6591
Plug g:GITHUB_SITE .. 'ojroques/vim-oscyank'
Plug g:GITHUB_SITE .. 'christoomey/vim-system-copy'
else
Plug g:GITHUB_SITE .. 'ojroques/vim-oscyank', { 'commit': '14685fc' }
Plug g:GITHUB_SITE .. 'christoomey/vim-system-copy', { 'commit': '1e5afc4' }
endif
# git related
Plug g:GITHUB_SITE .. 'tpope/vim-fugitive'
Plug g:GITHUB_SITE .. 'junegunn/gv.vim'
# vim performance
if has('timers') && has('terminal')
Plug g:GITHUB_SITE .. 'dstein64/vim-startuptime'
else
Plug g:GITHUB_SITE .. 'NewComer00/startuptime.vim', { 'branch': 'patch-1' }
endif
# --------------------
# language related
# --------------------
Plug g:GITHUB_SITE .. 'othree/xml.vim'
# code formatters
Plug g:GITHUB_SITE .. 'google/vim-maktaba'
Plug g:GITHUB_SITE .. 'google/vim-codefmt', { 'on': ['FormatCode', 'FormatLines'] }
Plug g:GITHUB_SITE .. 'google/vim-glaive'
plug#end()
# *************************************************************************
# plugin configs
# *************************************************************************
# flazz/vim-colorschemes
if exists('g:plugs') && has_key(g:plugs, 'vim-colorschemes')
&& filereadable(g:plugs['vim-colorschemes']['dir'] .. '/colors/molokai.vim')
colorscheme molokai
endif
# preservim/nerdtree
g:NERDTreeWinPos = "right"
g:NERDTreeShowHidden = 1
g:NERDTreeMouseMode = 2
# disable the original file explorer
g:loaded_netrw = 1
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>'))
\| plug#load('nerdtree')
\| execute 'autocmd! nerd_loader'
\| endif
augroup END
if has('terminal')
# voldikss/vim-floaterm
g:floaterm_wintype = 'split'
g:floaterm_height = 0.3
autocmd QuitPre * :FloatermKill!
else
# Shougo/vimshell.vim
g:vimshell_enable_start_insert = 1
g:vimshell_popup_height = 30
endif
# preservim/tagbar
g:tagbar_position = 'vertical leftabove'
g:tagbar_width = max([25, winwidth(0) / 5])
# christoomey/vim-system-copy
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
system('chcp 65001')
# https://github.com/christoomey/vim-system-copy/pull/35#issue-557371087
g:system_copy#paste_command = 'powershell "Get-Clipboard"'
endif
# mileszs/ack.vim
if executable('rg')
g:ackprg = 'rg --vimgrep --hidden --glob=!.git/'
elseif executable('ag')
g:ackprg = 'ag --vimgrep --hidden --ignore .git'
endif
# davidhalter/jedi-vim
g:jedi#show_call_signatures = "2"
# rainbow/luochen1990
g:rainbow_active = 1
g:rainbow_conf = {
\ 'separately': {
\ 'nerdtree': 0,
\ }
\}
# ctrlpvim/ctrlp
g:ctrlp_extensions = ['tag']
g:ctrlp_show_hidden = 1
g:ctrlp_clear_cache_on_exit = 0
g:ctrlp_max_files = 0
# using F9 to toggle Ctrlp
g:ctrlp_prompt_mappings = { 'PrtExit()': ['<F9>', '<esc>', '<c-c>', '<c-g>'] }
if executable('rg')
set grepprg=rg\ --color=never\ --hidden
g:ctrlp_user_command = 'rg %s --files --color=never --hidden --glob=!.git/'
elseif executable("ag")
set grepprg=ag\ --nogroup\ --nocolor\ --hidden
g:ctrlp_user_command = 'ag %s -l --nocolor --hidden --ignore .git -g ""'
else
g:ctrlp_custom_ignore = {
\ 'dir': '\v[\/]\.(git|hg|svn)$',
\ 'file': '\v\.(exe|so|dll)$',
\ }
endif
# vim-scripts/YankRing.vim
# to avoid <C-p> collision with the ctrlp plugin
g:yankring_replace_n_pkey = '<m-p>'
g:yankring_replace_n_nkey = '<m-n>'
# save yankring history in this dir
var yankring_dir = expand(DATA_DIR .. '/yankring-dir')
if !isdirectory(yankring_dir)
mkdir(yankring_dir, "p", 0o700)
endif
g:yankring_history_dir = yankring_dir
# unblevable/quick-scope
g:qs_buftype_blacklist = ['terminal', 'nofile']
g:qs_filetype_blacklist = ['dashboard', 'startify']
# google/vim-maktaba
if exists('*maktaba#syscall#SetUsableShellRegex')
# work-around for windows
maktaba#syscall#SetUsableShellRegex('\v^/bin/sh|cmd|cmd\.exe|command\.com$')
endif
# google/vim-codefmt
if exists('*maktaba#plugin#GetOrInstall') && exists('*glaive#Install')
glaive#Install()
endif
# *************************************************************************
# extra functions
# *************************************************************************
# function to run a shell
def OpenShell(shell_id: number)
if has('terminal')
execute('FloatermToggle #' .. shell_id)
else
execute('VimShellPop')
endif
enddef
# test the startup time of Vim
def TimingVimStartup(sorted: number)
var cmd = ''
if has('timers') && has('terminal')
cmd = 'StartupTime --tries 10'
if sorted == 0
cmd ..= ' --no-sort'
endif
else
cmd = 'StartupTime'
endif
execute(cmd)
enddef
# *************************************************************************
# extra keymaps
# *************************************************************************
# functional hotkeys for plugins
nnoremap <silent> <F2> :NERDTreeToggle<CR>
nnoremap <silent> <F3> :call <SID>OpenShell(v:count1)<CR>
nnoremap <silent> <F4> :UndotreeToggle<CR>
nnoremap <silent> <F5> :AirlineToggle<CR>
nnoremap <silent> <F7> :YRShow<CR>
nnoremap <silent> <F8> :TagbarToggle<CR>
nnoremap <silent> <F9> :CtrlP<CR>
nnoremap <S-F9> :CtrlPClearCache<CR>
inoremap <silent> <F2> <Esc>:NERDTreeToggle<CR>
inoremap <silent> <F3> <Esc>:call <SID>OpenShell(v:count1)<CR>
inoremap <silent> <F4> <Esc>:UndotreeToggle<CR>
inoremap <silent> <F5> <Esc>:AirlineToggle<CR>
inoremap <silent> <F7> <Esc>:YRShow<CR>
inoremap <silent> <F8> <Esc>:TagbarToggle<CR>
inoremap <silent> <F9> <Esc>:CtrlP<CR>
inoremap <S-F9> <Esc>:CtrlPClearCache<CR>a
if has('terminal')
tnoremap <silent> <F3> <C-W>:FloatermHide<CR>
endif
# 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 <SID>TimingVimStartup(1)<CR>
nnoremap <leader>vT :call <SID>TimingVimStartup(0)<CR>
# google auto format
nnoremap <leader>f :FormatLines<CR>
nnoremap <leader>F :FormatCode<CR>
# strip trailing whitespaces
nnoremap <leader>s :StripWhitespace<CR>
# search the word under the cursor
nnoremap <leader>a :Ack!<CR>
# search the given word
nnoremap <leader>A :Ack!<Space>
# toggle highlighting for unblevable/quick-scope
nmap <leader>q <plug>(QuickScopeToggle)
xmap <leader>q <plug>(QuickScopeToggle)
# 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