forked from laomafeima/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.vimrc
458 lines (417 loc) · 17 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
set nocompatible " 去除VI一致性,必须
" Automatic installation
if empty(glob('~/.vim/autoload/plug.vim'))
silent !curl -fLo ~/.vim/autoload/plug.vim --create-dirs
\ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
autocmd VimEnter * PlugInstall --sync | source $MYVIMRC
endif
call plug#begin('~/.vim/plugged')
" 插件列表
Plug 'scrooloose/nerdtree', { 'on': 'NERDTreeToggle' }
Plug 'jistr/vim-nerdtree-tabs', { 'on': 'NERDTreeToggle' }
Plug 'scrooloose/nerdcommenter'
Plug 'cespare/vim-toml', { 'for': 'toml' }
Plug 'Yggdroot/indentLine', { 'on': 'IndentLinesToggle' }
Plug 'Shougo/denite.nvim'
Plug 'roxma/nvim-yarp'
Plug 'roxma/vim-hug-neovim-rpc'
Plug 'Shougo/echodoc.vim'
Plug 'Shougo/deoplete.nvim'
Plug 'autozimu/LanguageClient-neovim', {
\ 'branch': 'next',
\ 'do': 'bash install.sh && pip3 install neovim --upgrade && pip3 install python-language-server --upgrade && rustup component add rls-preview rust-analysis rust-src --toolchain nightly && cargo +nightly install racer',
\ }
Plug 'terryma/vim-multiple-cursors'
Plug 'luochen1990/rainbow'
Plug 'skywind3000/asyncrun.vim', { 'on': ['Asyncrun', 'Run', 'RunSelf'] }
Plug 'w0rp/ale'
Plug 'easymotion/vim-easymotion'
Plug 'jiangmiao/auto-pairs'
Plug 'christoomey/vim-tmux-navigator'
Plug 'laomafeima/run.vim', { 'on': ['Run', 'RunSelf'] }
Plug 'laomafeima/osc52yank.vim', { 'on': ['Osc52YankLines', 'Osc52YankSelected'] }
call plug#end()
syntax on " 开启语法高亮
filetype on " 自动检测文件类型
filetype plugin indent on " 插件文件类型和缩进
set mouse= " 禁止鼠标
colorscheme default " 使用默认配色
set t_Co=256 " 开启256色
set fileencodings=utf-8,gbk,cp936,ucs-bom,utf8 " 文件编码
set fileformat=unix " 设置文件的兼容格式为unix,避免换行符问题
set tabstop=4 " tab 的宽度
set expandtab " 用空格代替 tab
set shiftwidth=4 "自动缩进的时候 tab 的宽度
set softtabstop=4 "退格键的时候 tab 宽度
set number " 显示行号
set autoindent " 设置自动缩进。
set backspace=indent,eol,start " 兼容 vi 模式下无法删除的问题
set cursorline " 突出显示当前行
set hlsearch " 高亮显示搜索结果
" set ignorecase " 设置默认进行大小写不敏感查找
set scrolloff=2 " 光标到最后两行后自动滚动
set laststatus=2 " 默认显示状态栏
set ruler " 在编辑过程中,在右下角显示光标位置的状态行
set cc=80 " 设置 80 列显示线
autocmd FileType qf setlocal nonumber colorcolumn=0 " 设置QuickFix 里面不显示80列线和行号
autocmd BufReadPost * call setpos(".", getpos("'\"")) " 下次打开时将光标移动到上次位置
set statusline=%<%n\ %f\ %m%r%h%w%{(&fenc!=''?&fenc:&enc).':'.&ff}\ %LL\ %Y%=%{GetMyStatusLine()}\ %l,%v\ %p%%\ " 设置状态栏显示项目
hi VertSplit cterm=NONE term=NONE gui=NONE " 设置分屏线样式
" 修改 Markdown 文件中 单个 '_' 高亮的问题
autocmd FileType markdown syn clear markdownError
autocmd FileType markdown syn match markdownErrorNotDisplay "\w\@<=_\w\@="
au BufRead,BufNewFile *.php set indentexpr= | set smartindent " 针对 PHP 禁用基于 PHP 语法的缩进
autocmd FileType go set formatoptions+=r " 针对 Golang 的注释优化
noremap <silent> <C-P> <C-W>p; " 两个窗口之间来回跳转
" Ctrl-C 映射为 Esc 按键方便无 Esc 按键键盘
inoremap <C-C> <ESC>
if has('gui_running')
set guioptions-=T " 隐藏菜单栏
set guioptions-=m " 隐藏菜单栏
set guioptions-=L " 隐藏左侧滚动条
set guioptions-=r " 隐藏右侧滚动条
set guioptions-=e " 使用命令行下标签页样式
set lines=30 columns=110 " 设置启动时窗口大小
endif
" 进入插入模式时改变状态栏颜色
autocmd InsertEnter * hi StatusLine guibg=red guifg=black gui=none ctermbg=red ctermfg=black cterm=none
autocmd InsertLeave * hi StatusLine guibg=green guifg=black gui=none ctermbg=green ctermfg=black cterm=none
hi StatusLine guibg=green guifg=black gui=none ctermbg=green ctermfg=black cterm=none " 默认状态栏颜色
" Visual 模式下的颜色
hi Visual term=reverse ctermbg=DarkGrey guibg=LightGrey
" 设置菜单颜色
hi CursorLine ctermbg=darkgray cterm=none " 高亮当前行
autocmd BufWinEnter,WinEnter,BufEnter * setlocal cursorline " 自动启动高亮当前行
autocmd BufWinLeave,WinLeave,BufLeave * setlocal nocursorline " 自动取消高亮当前行
hi ColorColumn ctermbg=darkgray guibg=darkgray " 设置80 列 线的颜色
hi Pmenu guibg=darkslategray ctermbg=lightgray " 下拉菜单的颜色
hi PmenuSel ctermfg=white ctermbg=darkgray guibg=Grey cterm=bold
" 自定义标签栏显示样式
function! GetShortName(name)
let words = split(a:name, "/")
if len(words) <= 4
return a:name
endif
let filename = remove(words, -1)
let last_path = remove(words, -1)
let s_words = map(words, {key, val -> strpart(val, 0, 2)})
call add(s_words, last_path)
call add(s_words, filename)
return join(s_words, "/")
endfunction
" 标签控制
function! MyTabLabel(n)
let buflist = tabpagebuflist(a:n)
let winnr = tabpagewinnr(a:n)
if a:n == tabpagenr() " 当前 tab 显示完整路径
let result = a:n . ":" . (bufname(buflist[winnr - 1]) == "" ? "[No Name]" : bufname(buflist[winnr - 1]))
else
let result = a:n . ":" . (bufname(buflist[winnr - 1]) == "" ? "[No Name]" : GetShortName(bufname(buflist[winnr - 1])))
endif
for bufnr in buflist
if getbufvar(bufnr, "&modified")
let result .= (bufnr == buflist[winnr - 1] ? "+" : "~")
endif
endfor
return result
endfunction
" 标签栏控制
function! MyTabLine()
let s = ''
for i in range(tabpagenr('$'))
if i + 1 == tabpagenr()
let s .= '%#TabLineSel#'
else
let s .= '%#TabLine#'
endif
let s .= '%' . (i + 1) . 'T'
let s .= ' %{MyTabLabel(' . (i + 1) . ')} '
endfor
let s .= '%#TabLineFill#%T'
if tabpagenr('$') > 1
let s .= '%=%#TabLine#'
endif
return s
endfunction
set tabline=%!MyTabLine()
" 设置 tab bar 颜色
hi TabLineFill ctermfg=black ctermbg=black
hi TabLine ctermfg=gray ctermbg=black cterm=none
hi TabLineSel ctermfg=darkgreen ctermbg=black
" 标签页快捷键设置
noremap <silent><C-T> :tabnew<cr>
noremap <silent><tab>t :tabnew<cr>
noremap <silent><tab>w :tabclose<cr>
noremap <silent><leader>1 :tabn 1<cr>
noremap <silent><leader>2 :tabn 2<cr>
noremap <silent><leader>3 :tabn 3<cr>
noremap <silent><leader>4 :tabn 4<cr>
noremap <silent><leader>5 :tabn 5<cr>
noremap <silent><leader>6 :tabn 6<cr>
noremap <silent><leader>7 :tabn 7<cr>
noremap <silent><leader>8 :tabn 8<cr>
noremap <silent><leader>9 :tabn 9<cr>
noremap <silent><leader>0 :tabn 10<cr>
noremap <silent><s-tab> :tabnext<CR>
inoremap <silent><s-tab> <ESC>:tabnext<CR>
" 关闭方向键, 强迫自己用 hjkl
map <Left> :echoe "Use 'h'"<CR>
map <Right> :echoe "Use 'l'"<CR>
map <Up> :echoe "Use 'j'"<CR>
map <Down> :echoe "Use 'k'"<CR>
set incsearch " 搜索结果实时显示
" 禁用高亮
autocmd CursorHold * :set nohlsearch
autocmd InsertEnter * :set nohlsearch
" 当输入查找命令时,再启用高亮
noremap n :set hlsearch<cr>n
noremap N :set hlsearch<cr>N
noremap / :set hlsearch<cr>/
noremap :/ :set hlsearch<cr>:/
noremap ? :set hlsearch<cr>?
noremap * *:set hlsearch<cr>
noremap # #:set hlsearch<cr>
" NerdTree 配置
function! g:MyNERDTreeToggle()
if exists(":NERDTreeMirrorToggle")
execute ":NERDTreeMirrorToggle"
else
execute ":NERDTreeToggle"
endif
endfunction
command! -nargs=0 MyNERDTreeToggle call g:MyNERDTreeToggle()
nmap <silent> <C-E> :MyNERDTreeToggle<CR>; " Ctrl+E 开启和关闭 NERDTree
let NERDTreeShowBookmarks=1 " 默认显示书签
let NERDTreeWinSize=24 " 设置目录窗口宽度
let g:nerdtree_tabs_focus_on_files=1 " 设置 打开文件后文件窗口获得焦点
let g:nerdtree_tabs_smart_startup_focus=2 " 启动时焦点自动调整
let NERDTreeMinimalUI=1 " 不显示帮助信息
let g:NERDTreeMapJumpNextSibling = '' " 禁止 ctrl j 快捷键
let g:NERDTreeMapJumpPrevSibling = '' " 禁止 ctrl k 快捷键
let NERDTreeIgnore = ['.pyc$', '.pyo$', '.class$', '^__pycache__$'] " 隐藏部分后缀文件
" Denite 配置
noremap <silent> <C-B> :Denite file_rec<cr>; " 浏览当前路径下文件
noremap <silent> <C-F> :Denite grep<cr>; " 搜索当前路径下所有文件
call denite#custom#map(
\ 'insert', '<C-T>', '<denite:do_action:tabopen>', 'noremap')
call denite#custom#map(
\ 'insert', '<C-x>', '<denite:do_action:split>', 'noremap')
call denite#custom#map(
\ 'insert', '<C-v>', '<denite:do_action:vsplit>', 'noremap')
call denite#custom#map(
\ 'normal', 't', '<denite:do_action:tabopen>', 'noremap')
call denite#custom#map(
\ 'normal', 'x', '<denite:do_action:split>', 'noremap')
call denite#custom#map(
\ 'normal', 'v', '<denite:do_action:vsplit>', 'noremap')
call denite#custom#map(
\ 'insert', '<C-j>', '<denite:move_to_next_line>', 'noremap')
call denite#custom#map(
\ 'insert', '<C-k>', '<denite:move_to_previous_line>', 'noremap')
" Ripgrep command on grep source
call denite#custom#var('grep', 'command', ['rg'])
call denite#custom#var('grep', 'default_opts',
\ ['--vimgrep', '--no-heading'])
call denite#custom#var('grep', 'recursive_opts', [])
call denite#custom#var('grep', 'pattern_opt', ['--regexp'])
call denite#custom#var('grep', 'separator', ['--'])
call denite#custom#var('grep', 'final_opts', [])
" Change ignore_globs
call denite#custom#filter('matcher/ignore_globs', 'ignore_globs',
\ [ '.git/', '.ropeproject/', '__pycache__/', '*.swp',
\ 'venv/', 'images/', '*.log.*', '*.min.*', 'img/', 'fonts/'])
" Change file_rec command
call denite#custom#var('file_rec', 'command',
\ ['rg', '--files', '--glob', '!.git'])
" Change default prompt
call denite#custom#option('default', 'prompt', '>')
" 添加注释的时候增加一个空格
let g:NERDSpaceDelims = 1
" easymotion 配置
" Use uppercase target labels and type as a lower case
let g:EasyMotion_use_upper = 1
" type `l` and match `l`&`L`
let g:EasyMotion_smartcase = 1
" Smartsign (type `3` and match `3`&`#`)
let g:EasyMotion_use_smartsign_jp = 1
map <Leader>w <Plug>(easymotion-w)
map <Leader>b <Plug>(easymotion-b)
nmap s <Plug>(easymotion-s2)
map <Leader>j <Plug>(easymotion-j)
map <Leader>k <Plug>(easymotion-k)
map <leader><leader>. <Plug>(easymotion-repeat)
" indentLine
let g:indentLine_enabled = 0
let g:indentLine_char = '|'
" nmap <silent> <C-I> :IndentLinesToggle<cr>" Ctrl I开启关闭对齐线
" Run.vim
" 快捷输入
command! -nargs=? R Run <f-args>
command! -nargs=? T Run test
nmap <leader>t :T<CR>
autocmd FileType qf nmap <silent> <C-C> :RunStop<CR>; " QuickFix 框 Ctrl C 停止异步运行,非运行时会关闭 QuickFix
" OSC52Yank
autocmd VimEnter * call SetYankMap()
function! SetYankMap()
if getregtype("*") == ""
vnoremap Y :Osc52YankSelected<cr>
nnoremap Y :Osc52YankLines<cr>
else
vnoremap Y "*y
nnoremap Y "*yy
endif
nnoremap P "*p
endfunction
" vim-multiple-cursors 配置
function! g:Multiple_cursors_before()
let g:deoplete#disable_auto_complete = 1
endfunction
function! g:Multiple_cursors_after()
let g:deoplete#disable_auto_complete = 0
endfunction
set selection=inclusive
" 彩虹括号
let g:rainbow_active = 1
let g:rainbow_conf = {'guifgs': ['red', 'orange', 'yellow', 'green', 'cyan', 'blue', 'purple'], 'ctermfgs': ['red', 'darkyellow', 'yellow', 'green', 'cyan', 'blue', 'darkmagenta'],}
" ALE 配置
let g:ale_statusline_format = ['✘%d', '!%d', '✔']
let g:ale_lint_on_save = 1
let g:ale_lint_on_text_changed = 1
let g:ale_sign_error = '>>'
let g:ale_sign_warning = '>>'
highlight SignColumn ctermbg=none
highlight link ALEErrorSign Constant
highlight link ALEWarningSign LineNr
" echodoc 配置
set noshowmode
let g:echodoc_enable_at_startup = 1
" LSP 配置
let g:deoplete#enable_at_startup = 0 " 关闭自动开启
let g:LanguageClient_diagnosticsList = "Location" "数据存储在 location list
autocmd InsertEnter * call deoplete#enable() " 启动后开启自动补全,加快启动速度
set completeopt-=preview " 自动补全的时候不显示预览窗口
" 支持 tab 选择
inoremap <silent><expr> <TAB>
\ pumvisible() ? "\<C-n>" :
\ <SID>check_back_space() ? "\<TAB>" :
\ deoplete#mappings#manual_complete()
function! s:check_back_space() abort "{{{
let col = col('.') - 1
return !col || getline('.')[col - 1] =~ '\s'
endfunction"}}}
set hidden
let g:LanguageClient_serverCommands = {
\'python': ['pyls'],
\'rust': ['rustup', 'run', 'nightly', 'rls'],
\'go': ['go-langserver','-diagnostics','-func-snippet-enabled','-gocodecompletion'],
\}
let g:LanguageClient_autoStart = 0
" 在使用 LSP 的时候禁用 ALE
for LSPTypeItem in keys(g:LanguageClient_serverCommands)
execute "autocmd FileType ".LSPTypeItem." :ALEDisableBuffer"
let g:LanguageClient_autoStart = 1
endfor
let g:LanguageClient_diagnosticsDisplay = {
\1: { "name": "Error","texthl": "ALEError", "signText": g:ale_sign_error, "signTexthl": "ALEErrorSign",},
\2: { "name": "Warning","texthl": "ALEWarning", "signText": g:ale_sign_warning, "signTexthl": "ALEWarningSign",},
\3: { "name": "Information","texthl": "ALEInfo","signText": g:ale_sign_warning,"signTexthl": "ALEInfoSign",},
\4: { "name": "Hint","texthl": "ALEInfo","signText": g:ale_sign_warning,"signTexthl": "ALEInfoSign",},
\}
let g:LanguageClientLintResult = {} " 每个文件的检查结果
function! LanguageClientLintStatusLine() abort
let l:curBufNr = bufnr("%")
call LanguageClientLintStatusLineUpdateLintResult(l:curBufNr)
return LanguageClientLintStatusLineStr(l:curBufNr)
endfunction
function! LanguageClientLintStatusLineUpdateLintResult(curBufNr) abort
let l:msg = getloclist(".")
let l:lintResult = {}
for l:item in l:msg
let l:bufnr = get(l:item, "bufnr")
let l:type = get(l:item, "type")
if l:bufnr < 1 || (l:type != "E" && l:type != "W")
continue
endif
if has_key(l:lintResult, l:bufnr) == 0
let l:lintResult[l:bufnr] = {"E":0, "W":0}
endif
let l:lintResult[l:bufnr][l:type]+=1
endfor
for l:bufnrKey in keys(l:lintResult)
let g:LanguageClientLintResult[l:bufnrKey] = l:lintResult[l:bufnrKey]
endfor
if has_key(l:lintResult, a:curBufNr) == 0 && has_key(g:LanguageClientLintResult, a:curBufNr)
unlet g:LanguageClientLintResult[a:curBufNr]
endif
endfunction
function! LanguageClientLintStatusLineStr(curBufNr) abort
if has_key(g:LanguageClientLintResult, a:curBufNr) == 0 || (g:LanguageClientLintResult[a:curBufNr]["E"] == 0 && g:LanguageClientLintResult[a:curBufNr]["W"] == 0)
return g:ale_statusline_format[2]
else
return MakeStatusLineStr(g:LanguageClientLintResult[a:curBufNr]["E"], g:LanguageClientLintResult[a:curBufNr]["W"])
endif
endfunction
function! MakeStatusLineStr(errno, warno) abort
if a:errno == 0 && a:warno == 0
return g:ale_statusline_format[2]
else
let l:str = ""
if a:errno > 0
let l:str .= printf(g:ale_statusline_format[0], a:errno)
endif
if a:warno > 0
let l:str .= printf(g:ale_statusline_format[1], a:warno)
endif
return l:str
endif
endfunction
function! ALELinterStatus() abort
let l:counts = ale#statusline#Count(bufnr(''))
let l:all_errors = l:counts.error + l:counts.style_error
let l:all_non_errors = l:counts.total - l:all_errors
return MakeStatusLineStr(all_errors, all_non_errors)
endfunction
" 兼容 ALE 和 LSP
function! GetMyStatusLine() abort
" 状态栏显示语法信息,同时支持 LSP 和 ALE
if index(keys(g:LanguageClient_serverCommands), &filetype) > -1
return LanguageClientLintStatusLine()
else
return ALELinterStatus()
endfunction
function! MyErrorNext() abort
if index(keys(g:LanguageClient_serverCommands), &filetype) > -1
try
execute "lne"
catch /E553:
execute "lr"
endtry
else
execute "normal \<Plug>(ale_next_wrap)"
endif
endfunction
function! MyErrorPrev() abort
if index(keys(g:LanguageClient_serverCommands), &filetype) > -1
try
execute "lp"
catch /E553:
execute "lla"
endtry
else
execute "normal \<Plug>(ale_previous_wrap)"
endif
endfunction
function! TextDocumentHoverToggle() abort
try
execute "wincmd P"
execute "pclose"
catch /E441:
call LanguageClient_textDocument_hover()
endtry
endfunction
nnoremap <silent> <Leader>H :call TextDocumentHoverToggle()<CR>
nnoremap <silent> <C-]> :call LanguageClient_textDocument_definition()<CR>
" 查看当前方法属性的所有引用
nnoremap <silent> <Leader>ref :call LanguageClient_textDocument_references() <bar> :lli<cr>
nnoremap <silent> <Leader>ep :call MyErrorPrev()<cr>; " 跳转到上一个错误/引用
nnoremap <silent> <Leader>en :call MyErrorNext()<cr>; " 跳转到下一个错误/引用