-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvimrc
550 lines (485 loc) · 15.2 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
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
""" Section I. Preparation
"" Plugins
call plug#begin('~/.vim/modules')
" Basic
Plug 'editorconfig/editorconfig-vim'
Plug 'mattn/gist-vim'
Plug 'mattn/webapi-vim'
Plug 'raimondi/delimitmate'
Plug 'tpope/vim-commentary'
Plug 'tpope/vim-dadbod'
Plug 'tpope/vim-dispatch'
Plug 'tpope/vim-repeat'
Plug 'tpope/vim-surround'
Plug 'tpope/vim-unimpaired'
" Git
Plug 'rhysd/committia.vim'
Plug 'tpope/vim-fugitive'
" Display
Plug 'vim-airline/vim-airline'
Plug 'arcticicestudio/nord-vim'
" Editor
Plug 'skywind3000/asyncrun.vim'
Plug 'wakatime/vim-wakatime'
" Jumpping
Plug 'junegunn/fzf', { 'do': './install --all' }
Plug 'junegunn/fzf.vim'
Plug 'scrooloose/nerdtree'
Plug 'xuyuanp/nerdtree-git-plugin'
" FileTypes
Plug 'sheerun/vim-polyglot'
Plug 'jaxbot/semantic-highlight.vim'
" -> TypeScript
Plug 'leafgarland/typescript-vim'
Plug 'mattn/emmet-vim'
" -> Markdown
Plug 'plasticboy/vim-markdown', { 'for': 'markdown' }
" -> Python
Plug 'psf/black', { 'for': 'python' }
Plug 'jmcantrell/vim-virtualenv', { 'for': 'python' }
Plug 'LKI/vim-pipenv', { 'for': 'python' }
" Others
Plug 'github/copilot.vim'
" NVim
if has('nvim')
" LSP Installer
Plug 'williamboman/mason.nvim'
Plug 'williamboman/mason-lspconfig.nvim'
" Language Servers
Plug 'neovim/nvim-lspconfig'
Plug 'hrsh7th/cmp-nvim-lsp'
Plug 'hrsh7th/cmp-buffer'
Plug 'hrsh7th/cmp-path'
Plug 'hrsh7th/cmp-cmdline'
Plug 'hrsh7th/nvim-cmp'
Plug 'hrsh7th/cmp-vsnip'
Plug 'hrsh7th/vim-vsnip'
" Display
Plug 'nvim-lualine/lualine.nvim'
Plug 'kyazdani42/nvim-web-devicons'
" Session
" Plug 'rmagatti/auto-session'
endif
call plug#end()
"" General
filetype plugin indent on " help :filetype-overview
syntax on
set nocompatible " Vi Improved, not vi
" File related
set autoread
set autowrite
set belloff=all
set encoding=utf-8
set fileencoding=utf-8
set fileencodings=utf-8,gb2313,gb18030,gbk,cp936,latin1
set fileformats=unix,dos
set nobackup
set noswapfile
set termencoding=utf-8
set updatetime=250
set wildignore+=.git,node_modules,dist,.idea,*.pyc,.pytest_cache,__pycache__,.coverage
" Read related
set completeopt=menu,menuone,noselect
set hidden
set hlsearch
set incsearch
set laststatus=2
set number
set scrolloff=6
set wildmenu
set wrap
" Write related
set autoindent
set backspace=2
set clipboard=unnamedplus " Use system clipboard
set expandtab
set matchtime=2
set showmatch
set smartindent
set smarttab
""" Section II. Appearance
if has('gui_running')
let g:has_gui=1
endif
" AirlineTheme nord
if filereadable(expand('$HOME/.vim/modules/nord-vim/colors/nord.vim'))
colorscheme nord
endif
if has('unix')
set t_Co=256
endif
if has('win32')
let $LANG='en_US'
if has('gui_win32') " GVim settings
set renderoptions=type:directx,renmode:0
autocmd GUIEnter * set lines=69 columns=250
else
set termguicolors
endif
endif
""" Section III. Keymap
let mapleader="\<Space>"
inoremap <silent> <S-Insert> <C-R>"
nnoremap <A-s> :set<Space>
nnoremap <Leader>gco :Git checkout -b<Space>
nnoremap <silent> <A-!> :NERDTreeFind<CR>
nnoremap <silent> <A-1> :NERDTreeToggle<CR>
nnoremap <silent> <A-9> :call TogGitLog()<CR>
nnoremap <silent> <A-S-a> :Git blame --date=short<CR>
nnoremap <silent> <A-S-l> :call Reformat()<CR>
nnoremap <silent> <A-S-n> :cnext<CR>
nnoremap <silent> <A-S-p> :cprevious<CR>
nnoremap <silent> <A-f> :Rg<CR>
nnoremap <silent> <A-h> :tabprevious<CR>
nnoremap <silent> <A-j> :bprevious<CR>
nnoremap <silent> <A-k> :bnext<CR>
nnoremap <silent> <A-l> :tabnext<CR>
nnoremap <silent> <A-n> :FZF<CR>
nnoremap <silent> <A-o> :Files<CR>
nnoremap <silent> <A-w> :call BufferClose()<CR>
nnoremap <silent> <F5> :call RunFile()<CR>
nnoremap <silent> <Leader>/ :let @/ = ""<CR>
nnoremap <silent> <Leader>b :Git blame<CR>
nnoremap <silent> <Leader>eg :edit ~/.lki/.gitconfig<CR>
nnoremap <silent> <Leader>eh :edit C:\Windows\System32\drivers\etc\hosts<CR>
nnoremap <silent> <Leader>ep :edit ~/.lki/.profile<CR>
nnoremap <silent> <Leader>es :edit ~/.ssh/config<CR>
nnoremap <silent> <Leader>ev :edit ~/.vim/vimrc<CR>
nnoremap <silent> <Leader>gca :Git commit -a -S<CR>
nnoremap <silent> <Leader>gcm :Git commit -a --amend --no-edit -S<CR>
nnoremap <silent> <Leader>gf :Git fetch o<CR>
nnoremap <silent> <Leader>gld :Git load<CR>
nnoremap <silent> <Leader>glg :silent Git log --all --graph --pretty=format:'%h - (%cr)%d %s <%an>' --abbrev-commit<CR>:setlocal filetype=gitlog<CR>
nnoremap <silent> <Leader>go :call GoIntoUrl()<CR>
nnoremap <silent> <Leader>gpd :Git pd<CR>
nnoremap <silent> <Leader>gpf :Git push -f<CR>
nnoremap <silent> <Leader>gpl :Git pull --rebase<CR>
nnoremap <silent> <Leader>gps :Git push<CR>
nnoremap <silent> <Leader>grd :Git rebase o/HEAD<CR>
nnoremap <silent> <Leader>gst :w<CR>:Git<CR>
nnoremap <silent> <Leader>gsv :w<CR>:AsyncRun git add . && git save<CR>
nnoremap <silent> <Leader>gwp :Git commit --all --message 'WIP' --allow-empty --no-verify<CR>
nnoremap <silent> <Leader>kaf :AsyncRun -raw kubectl apply -f %<CR>
nnoremap <silent> <Leader>n :NERDTreeFind<CR>
nnoremap <silent> <Leader>q :wq<CR>
nnoremap <silent> <Leader>sp :set paste!<CR>
nnoremap <silent> <Leader>sv :wincmd v<CR>:setl nobuflisted<CR>:bnext<CR>
nnoremap <silent> <Leader>u :set ff=unix<CR>:w<CR>
nnoremap <silent> <Leader>v :Pipenv<CR>
nnoremap <silent> <Leader>w :w<CR>
nnoremap <silent> gd :call GoInto()<CR>
nnoremap <silent> j gj
nnoremap <silent> k gk
noremap <silent> <Leader>c :Commentary<CR>
noremap <silent> <Leader>l :=<CR>
tnoremap <silent> <A-w> <C-W>:bdelete!<CR>
tnoremap <silent> <S-Insert> <C-W>"*
xnoremap <silent> <A-f> "zy:Rg<Space><C-r>z<CR>
xnoremap <silent> <Leader>r "zy:AsyncRun -r <C-r>z<CR>
xnoremap <silent> <Leader>st :sort<CR>
" Resize splits quickly by Alt+Shift+(-/=)
" ref: https://vim.fandom.com/wiki/Resize_splits_more_quickly
nnoremap <silent> <A-+> :execute 'resize ' . (winheight(0) * 3/2)<CR>
nnoremap <silent> <A-_> :execute 'resize ' . (winheight(0) * 2/3)<CR>
tnoremap <silent> <A-+> <C-W>:execute 'resize ' . (winheight(0) * 3/2)<CR>
tnoremap <silent> <A-_> <C-W>:execute 'resize ' . (winheight(0) * 2/3)<CR>
""" Section IV. Plugins
" Plugin 'scrooloose/nerdtree'
let NERDTreeAutoDeleteBuffer = 1
let NERDTreeChDirMode = 2
let NERDTreeMinimalUI = 1
let NERDTreeQuitOnOpen = 1
let NERDTreeRemoveFileCmd = 'rm '
let NERDTreeRespectWildIgnore = 1
let NERDTreeShowBookmarks = 1
let NERDTreeShowHidden = 1
let NERDTreeWinSize = 50
autocmd bufenter * if (winnr('$') == 1 && exists('b:NERDTree') && b:NERDTree.isTabTree()) | q | endif
let b:csv_headerline = 0
let g:csv_delim='|'
let g:fzf_layout = { 'window': { 'width': 0.9, 'height': 0.8 } }
let g:fzf_preview_window = ''
let g:airline#extensions#tabline#enabled = 1
let g:airline_powerline_fonts = 1
let g:airline_disable_statusline = 1
let g:asyncrun_open = 10
let g:vim_markdown_new_list_item_indent = 2
" Plugin 'mattn/emmet-vim'
let g:user_emmet_install_global = 0
let g:user_emmet_leader_key = '<C-Tab>'
autocmd FileType html,css,js,jsx,ts,tsx EmmetInstall
if has('win32')
for p in split($PATH, ';')
if filereadable(p . '/black.exe')
let g:black_virtualenv = fnamemodify(join(split(p, '\'), '/'), ':h')
break
endif
endfor
endif
let g:black_fast = 1
let g:black_linelength = 120
let $PYTHONNUNBUFFERED=1
" Vim insert mode cursor
" https://github.com/microsoft/terminal/issues/68#issuecomment-418438517
let &t_SI.="\e[5 q"
let &t_SR.="\e[4 q"
let &t_EI.="\e[1 q"
""" Section V. Autocmds
augroup setFileType
autocmd!
autocmd BufNewFile,BufRead *vimrc setl filetype=vim
autocmd BufNewFile,BufRead *.md setl filetype=markdown
augroup END
augroup setIndent
autocmd!
autocmd FileType sh,html,vim,js,jsx,md,ts,tsx setl shiftwidth=2 tabstop=2
augroup END
augroup mapping
autocmd!
autocmd FileType gitcommit nnoremap <buffer> <silent> <A-w> :w<CR>:bdelete<CR>
autocmd FileType fugitiveblame nnoremap <buffer> <silent> <A-S-a> :bdelete<CR>
autocmd FileType help nnoremap <buffer> <silent> <A-w> :bdelete<CR>
augroup END
augroup ignoreBuffer " inspired by https://vi.stackexchange.com/questions/16708/
autocmd!
autocmd FileType qf setl nobuflisted
augroup END
if exists('##TermClose')
augroup autoCloseTerminal " inspired by https://vi.stackexchange.com/questions/10292/
autocmd!
autocmd TermClose * if getline('$') == '' | bdelete! | endif
augroup END
endif
""" Section VI. Language Servers
if !exists('has_vsvim')
inoremap <expr> <S-Tab> pumvisible() ? "\<C-p>" : "\<S-Tab>"
inoremap <expr> <Tab> pumvisible() ? "\<C-n>" : "\<Tab>"
inoremap <expr> <cr> pumvisible() ? "\<C-y>" : "\<cr>"
nnoremap <silent> <A-0> :lua vim.diagnostic.open_float()<CR>
nnoremap <silent> <A-CR> :lua vim.lsp.buf.code_action()<CR>
nnoremap <silent> <F1> :lua vim.lsp.buf.hover()<CR>
nnoremap <silent> <F2> :lua vim.diagnostic.goto_next()<CR>
nnoremap <silent> <F7> :lua vim.lsp.buf.references()<CR>
endif
""" Section X. Functions
func! TogableMap(key, name, ...)
let cmd = a:name
if len(a:000)
let cmd = cmd.'", "'.join(a:000, '", "')
endif
execute 'nnoremap <silent> '.a:key.' :call Togable("'.cmd.'")<CR>'
execute 'tnoremap <silent> '.a:key.' <C-\><C-n>:call Togable("'.cmd.'")<CR>'
endfunc
func! Togable(name, ...) " inspired by pakutoma/toggle-terminal
let cmd = get(a:, 1, a:name)
let pos = get(a:, 2, 'botright')
let bufName = '<Togable> '.a:name
let bufNum = get(filter(range(1, bufnr('$')), 'bufname(v:val) == "'.bufName.'"'), 0, -1)
if bufNum == -1 || bufloaded(bufNum) != 1
execute 'silent '.pos.' split | term '.cmd
execute 'silent file '.bufName
execute 'silent set nobuflisted'
execute 'silent set nonumber'
startinsert!
else
let bufWinNum = bufwinnr(bufNum)
if bufWinNum == -1
execute 'silent '.pos.' sbuffer '.bufNum
startinsert!
else
execute 'silent '.bufWinNum.' wincmd w'
hide
endif
endif
endfunc
func! TogGitLog()
let bufName = '<Togable> git logg'
let bufNum = get(filter(range(1, bufnr('$')), 'bufname(v:val) == "<Togable> git logg"'), 0, -1)
if bufNum == -1 || bufloaded(bufNum) != 1
execute 'silent G log --all --graph --pretty=format:"%h - (%cr)%d %s <%an>" --abbrev-commit'
execute 'silent file '.bufName
execute 'silent set nobuflisted'
execute 'silent set filetype=gitlog'
else
let bufWinNum = bufwinnr(bufNum)
if bufWinNum != -1
execute 'silent '.bufWinNum.' wincmd w'
execute 'silent bdelete'
endif
endif
endfunc
" 关闭当前的 buffer, 并尽可能地保留 split layout, 保证以下特征:
" - 优先关闭 buffer, 最后关闭 window split, 不关闭最后一个 buffer
func! BufferClose()
if &filetype == 'gitlog' || &filetype == 'fugitive' || &filetype == 'fugitiveblame' || &filetype == 'qf'
execute 'silent bdelete!'
return
endif
let buffer_count = len(filter(range(1, bufnr('$')), 'buflisted(v:val)'))
let window_count = winnr('$')
if window_count > 1
if buffer_count > window_count
execute 'bp|bd! #'
else
execute 'bd!'
endif
return
endif
if buffer_count != 1
execute 'bp|bd! #'
endif
endfunc
func! GoInto()
if exists('b:NERDTree')
normal cdCD
else
lua vim.lsp.buf.definition()
endif
endfunc
func! GoIntoUrl()
let line = getline('.')
let url = matchstr(line, 'http[s]\?:\/\/[[:alnum:]%\/_#.-]*')
if len(url)
call jobstart('explorer '.url)
return
endif
let repo = matchstr(line, '[0-9a-zA-Z._-][0-9a-zA-z._/-]\+/[0-9a-zA-z._/-]\+[0-9a-zA-Z._-]')
if len(repo)
let group = split(repo, '/')[0]
call jobstart('explorer https://github.com/'.repo)
endif
endfunc
func! Reformat()
if &ft == 'python'
execute 'Black'
else
lua vim.lsp.buf.format()
endif
endfunc
func! GUISetup()
let g:netrw_browsex_viewer='start'
let g:netrw_gx='start'
set autowrite
set! guifont=FiraCode\ Nerd\ Font:h13
set guioptions=c
set langmenu=en_US
if eval('@%') == ''
cd ~/.vim
endif
nnoremap <silent> <S-F12> i
tnoremap <silent> <S-F12> <C-\><C-N>
call TogableMap('<A-t>', 'django shell', 'python manage.py shell')
call TogableMap('<A-S-t>', 'ipython', 'ipython')
call TogableMap('<A-$>', 'yarn', 'cmd /k "yarn start"')
call TogableMap('<A-4>', 'django runserver', 'python manage.py runserver')
call TogableMap('<A-F12>', 'gitbash', 'C:/Users/lirian/scoop/apps/git/current/bin/bash.exe -l -i')
endfunc
if !exists('*RunFile')
func! RunFile()
write
if &ft == 'python'
let $PYTHONPATH='.'
execute 'AsyncRun -raw python %'
elseif &ft == 'go'
execute 'AsyncRun -raw go run %'
elseif &ft == 'vim'
execute 'source $MYVIMRC'
endif
endfunc
endif
if exists('g:has_gui')
call GUISetup()
endif
if has('nvim')
lua << END
-- setup lualine
require('lualine').setup()
-- setup nvim-cmp
local cmp = require('cmp')
cmp.setup({
snippet = {
expand = function(args)
vim.fn["vsnip#anonymous"](args.body)
end,
},
window = {
completion = cmp.config.window.bordered(),
documentation = cmp.config.window.bordered(),
},
mapping = cmp.mapping.preset.insert({
['<C-b>'] = cmp.mapping.scroll_docs(-4),
['<C-f>'] = cmp.mapping.scroll_docs(4),
['<C-Space>'] = cmp.mapping.complete(),
['<C-e>'] = cmp.mapping.abort(),
['<CR>'] = cmp.mapping.confirm({ select = true }), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items.
['<Tab>'] = cmp.mapping(function(fallback)
if cmp.visible() then
local entry = cmp.get_selected_entry()
if not entry then
cmp.select_next_item({ behavior = cmp.SelectBehavior.Select })
else
cmp.confirm()
end
elseif vim.fn['vsnip#available'](1) == 1 then
feedkey('<Plug>(vsnip-expand-or-jump)', '')
else
fallback() -- The fallback function sends a already mapped key. In this case, it's probably `<Tab>`.
end
end, { 'i', 's', 'c' }),
['<S-Tab>'] = cmp.mapping(function()
if cmp.visible() then
cmp.select_prev_item()
elseif vim.fn['vsnip#jumpable'](-1) == 1 then
feedkey('<Plug>(vsnip-jump-prev)', '')
end
end, { 'i', 's', 'c' }),
}),
sources = cmp.config.sources({
{ name = 'nvim_lsp' },
{ name = 'vsnip' },
}, {
{ name = 'buffer' },
})
})
-- Use buffer source for `/`.
cmp.setup.cmdline('/', {
mapping = cmp.mapping.preset.cmdline(),
sources = {
{ name = 'buffer' }
}
})
-- Use cmdline & path source for ':'.
cmp.setup.cmdline(':', {
mapping = cmp.mapping.preset.cmdline(),
sources = cmp.config.sources({
{ name = 'path' }
}, {
{ name = 'cmdline' }
})
})
-- setup lspconfig.
local capabilities = require('cmp_nvim_lsp').default_capabilities()
-- setup mason.
require("mason").setup()
-- setup nvim python virtualenv
local function get_python_path(workspace)
-- Use activated virtualenv if any
if vim.env.VIRTUAL_ENV then
return require('lspconfig/util').path.join(vim.env.VIRTUAL_ENV, 'bin', 'python')
end
-- Fallback to system Python
return vim.fn.exepath('python3') or vim.fn.exepath('python') or 'python'
end
-- setup pyright with virtualenv
require('lspconfig').pyright.setup({
on_attach = function()
require('lsp_signature').on_attach {
hint_enable = false,
}
end,
on_init = function(_, config)
config.settings.python.pythonPath = get_python_path(config.root_dir)
end
})
END
endif