forked from amix/vimrc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_configs.vim
512 lines (451 loc) · 16.2 KB
/
my_configs.vim
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
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Vundle: vim plugin manager
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
let s:my_plugins_path = expand('<sfile>:p:h') . '/my_plugins'
call vundle#begin(s:my_plugins_path)
Plugin 'VundleVim/Vundle.vim'
Plugin 'ervandew/supertab'
" Plugin 'davidhalter/jedi-vim'
Plugin 'easymotion/vim-easymotion'
Plugin 'nathanaelkane/vim-indent-guides'
Plugin 'octol/vim-cpp-enhanced-highlight'
Plugin 'tpope/vim-obsession'
Plugin 'vim-scripts/ReplaceWithRegister'
" Plugin 'Valloric/YouCompleteMe'
" Plugin 'scrooloose/nerdtree' " file/directory tree
" Plugin 'tomasr/molokai'
" Plugin 'morhetz/gruvbox'
" Plugin 'Lokaltog/vim-powerline'
" Plugin 'fholgado/minibufexpl.vim'
Plugin 'vim-scripts/LargeFile'
call vundle#end()
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => General
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 让vimrc配置变更立即生效
" autocmd BufWritePost $MYVIMRC source $MYVIMRC
" 关闭兼容模式
set nocompatible
" vim 自身命令行模式智能补全
set wildmenu
"=============================
" => 自定义常用快捷键
"=============================
" 快速连按两次<Leader>键,等价于<ESC>
inoremap <Leader><Leader> <ESC>
" 定义快捷键保存当前窗口内容
nmap <Leader>w :w<CR>
" :W 表示用 sudo 保存,具体配置见 basic.vim
" 在结对符之间跳转 记忆:match
nmap <Leader>m %
" 高亮显示匹配的括号
set showmatch
" 去除空行
nnoremap <F2> :g/^\s*$/d<CR>
"=============================
" => 鼠标
"=============================
" 可以在任何地方使用鼠标
set mouse=a
set selection=exclusive
set selectmode=mouse,key
"=============================
" => Tab页, Buffer, Window
"=============================
" => Buffer
" 垂直(线)分屏打开buffer
:cnoremap vb vertical rightbelow sb
" => Window
nmap <Leader>q :q<CR>
" 保存所有窗口内容,并退出 vim
nmap <Leader>wq :wa<CR>:q<CR>
nmap <Leader>Q :qa!<CR>
" 依次遍历子窗口 记忆:next window
nnoremap nw <C-W><C-W>
nnoremap <Leader>h <C-W>h
nnoremap <Leader>j <C-W>j
nnoremap <Leader>k <C-W>k
nnoremap <Leader>l <C-W>l
"=============================
" => 显示:布局、高亮
"=============================
" 禁止光标闪烁
set gcr=a:block-blinkon0
" 显示光标当前位置
set ruler
" 折行
set wrap " 当一行在屏幕上显示不下时,换行显示
" 禁止显示滚动条
set guioptions-=l
set guioptions-=L
set guioptions-=r
set guioptions=R
" 禁止显示菜单和工具条
set guioptions-=m
set guioptions-=T
" 总是显示状态栏
set laststatus=2
" 开启行号显示
set number
" 设置相对行号
set rnu
" 高亮显示当前行/列
set cursorline
set cursorcolumn
" 开启语法高亮功能
syntax enable
" 允许用指定语法高亮配色方案替换默认方案
syntax on
"=============================
" => 显示:主题、字体
"=============================
"set term=xterm
set t_Co=256
set background=dark
colorscheme gruvbox
" colorscheme solarized
" colorscheme molokai
" colorscheme peaksea
" let g:solarized_termtrans=1
" let g:solarized_termcolors=256
if has('gui_running')
set background=light
else
set background=dark
endif
call togglebg#map("<F5>")
" 设置 gvim 显示字体
"set guifont=YaHei\ Consolas\ Hybrid\ 11.5
set guifont=Ubuntu\ Mono\ Regular\ 13
"=============================
" => 编码
"=============================
" Vim 中文本的默认编码
set encoding=utf-8
" 打开文件时,会逐个尝试以下编码
set fileencodings=utf-8,gbk,big5,cp936,gb18030,gb2312,utf-16
set termencoding=utf-8
" 东亚地区易混淆字符的宽度
set ambiwidth=double
"=============================
" => 空格和制表符
"=============================
" 将制表符扩展为空格
set expandtab
" 设置编辑时制表符占用空格数
set tabstop=4
" 设置格式化时制表符占用空格数
set shiftwidth=4
" 让 vim 把连续数量的空格视为一个制表符
set softtabstop=4
"=============================
" => 复制粘贴
"=============================
" set backspace=2
set pastetoggle=<F3>
" 设置快捷键将选中文本块复制至系统剪贴板
vnoremap <Leader>y "+y
" 设置快捷键将系统剪贴板内容粘贴至 vim
nmap <Leader>p "+p
"=============================
" => 搜索替换
"=============================
" 开启实时搜索功能
set incsearch
" 搜索时大小写不敏感
set ignorecase
" 高亮显示搜索结果
set hlsearch
" 用 // 搜索 选中的文本
vnoremap // y/\V<C-R>=escape(@",'/\')<CR><CR>
" 用 Ctrl-r 搜索并替换(y确认, n跳过, a全部确认)选中的文本
vnoremap <C-r> "hy:%s/<C-r>h//gc<left><left><left>
"=============================
" => 代码折叠
"=============================
"set foldmethod=indent
set foldmethod=syntax
set foldnestmax=6
" 启动 vim 时关闭折叠代码
set nofoldenable
"=============================
" => 缩进快捷键
"=============================
nnoremap <Tab> >>_
nnoremap <S-Tab> <<_
vnoremap <Tab> >
vnoremap <S-Tab> <
" Ref: https://vim.fandom.com/wiki/Shifting_blocks_visually#Mappings
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => 各种插件配置
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"=============================
" => Plugin: vim-indent-guides
"=============================
" 随 vim 自启动
let g:indent_guides_enable_on_vim_startup=1
" 从第二层开始可视化显示缩进
let g:indent_guides_start_level=2
" 色块宽度
let g:indent_guides_guide_size=1
" 快捷键 <leader>i 开/关缩进可视化
:nmap <silent> <Leader>i <Plug>IndentGuidesToggle
"=============================
" => Plugin: easymotion-vim
"=============================
" 以下配置参考了 GitHub 仓库首页说明
let g:EasyMotion_do_mapping = 0 " Disable default mappings
map <Leader><Leader>w <Plug>(easymotion-w)
map <Leader><Leader>b <Plug>(easymotion-b)
" Jump to anywhere you want with minimal keystrokes, with just one key binding.
" `s{char}{label}`
nmap s <Plug>(easymotion-overwin-f)
" or
" `空格{char}{char}{label}` 通过两个字符快速定位到目标位置
" Need one more keystroke, but on average, it may be more comfortable.
nmap <Space> <Plug>(easymotion-overwin-f2)
" Turn on case-insensitive feature
let g:EasyMotion_smartcase = 1 " 大小写不敏感
" JK motions: 快速行移动 Line motions
map <Leader><Leader>j <Plug>(easymotion-j)
map <Leader><Leader>k <Plug>(easymotion-k)
"=============================
" => Plugin: NERDTree
"=============================
" 使用 NERDTree 插件查看工程文件。设置快捷键,速记:file list
nmap <Leader>fl :NERDTreeToggle<CR>
" 设置NERDTree子窗口宽度, default is 31
let NERDTreeWinSize=30
" 设置NERDTree子窗口位置
let NERDTreeWinPos="right"
let NERDTreeChDirMode=1
"显示书签"
let NERDTreeShowBookmarks=1
"设置忽略文件类型"
let NERDTreeIgnore=['\~$', '\.pyc$', '\.swp$']
" 显示隐藏文件
let NERDTreeShowHidden=1
" NERDTree 子窗口中不显示冗余帮助信息,但会同时屏蔽掉目录:.. (up a dir)
" let NERDTreeMinimalUI=1
" 最小化菜单:变成一行,不需要再滚动选择
let NERDTreeMinimalMenu=1
" 删除文件时自动删除文件对应 buffer
let NERDTreeAutoDeleteBuffer=1
" 启动vim且只是进入某个目录(没有打开文件),则自动打开NERDTree
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 0 && !exists("s:std_in") | NERDTree | endif
autocmd VimEnter * if argc() == 1 && isdirectory(argv()[0]) && !exists("s:std_in") | exe 'NERDTree' argv()[0] | wincmd p | ene | endif
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif
"=============================
" => Plugin: ALE
"=============================
" 所有的 linter 和 fixer,都需要先安装了才能使用,ALE 只是 LSP client
" Linting:
let g:ale_lint_on_enter = 1
let g:ale_sign_error = '✗'
let g:ale_sign_warning = '⚠️'
let g:ale_sign_column_always = 1
let g:ale_linters = {
\ 'python': ['pyright', 'jedils'],
\ 'cpp': ['gcc', 'ccls', 'cppcheck'],
\}
nmap <silent> <Leader>a <Plug>(ale_next_wrap)
" Fixing:
let g:ale_fixers = {
\ 'javascript': ['eslint'],
\ 'python': ['yapf'],
\ '*': ['remove_trailing_lines', 'trim_whitespace'],
\}
let g:ale_fix_on_save = 0
" Completion:
let g:ale_completion_enabled = 1
let g:ale_completion_autoimport = 1
" imap <Leader><Space> <Plug>(ale_complete)
" Rename:
nmap <Leader>r :ALERename<Return>
" Go To Definition:
nmap gd <Plug>(ale_go_to_definition)
" Find References:
nmap <silent> <leader>n :ALEFindReferences -relative<Return>
" Hovering:
nmap <silent> K :ALEHover<Return>
"=============================
" => Plugin: lightline-ale
"=============================
let g:lightline.component_expand = {
\ 'linter_checking': 'lightline#ale#checking',
\ 'linter_infos': 'lightline#ale#infos',
\ 'linter_warnings': 'lightline#ale#warnings',
\ 'linter_errors': 'lightline#ale#errors',
\ 'linter_ok': 'lightline#ale#ok',
\}
let g:lightline.component_type = {
\ 'linter_checking': 'right',
\ 'linter_infos': 'right',
\ 'linter_warnings': 'warning',
\ 'linter_errors': 'error',
\ 'linter_ok': 'right',
\}
let g:lightline.active = {
\ 'right': [ [ 'lineinfo' ],
\ [ 'percent' ],
\ [ 'filetype', 'fileencoding'],
\ [ 'linter_checking', 'linter_errors', 'linter_warnings', 'linter_infos', 'linter_ok' ]
\ ]}
"=============================
" => Other Plugins
"=============================
" let g:jedi#completions_command = "<Leader><Space>"
let g:snipMate = { 'snippet_version' : 1 }
nnoremap <silent> <leader>z :Goyo<cr>
" for big File more than 1MB, turn off syntax highlighting
let g:LargeFile = 1
"=============================
" => Plugin: MiniBufExplorer
"=============================
" " 显示/隐藏 MiniBufExplorer 窗口
" map <Leader>bl :MBEToggle<cr>
" " buffer 切换快捷键
" map <C-Tab> :MBEbn<cr>
" map <C-S-Tab> :MBEbp<cr>
" " 设置环境保存项
" set sessionoptions="blank,buffers,globals,localoptions,tabpages,sesdir,folds,help,options,resize,winpos,winsize"
" " 保存 undo 历史
" set undodir=~/.undo_history/
" set undofile
" " 保存快捷键
" map <leader>ss :mksession! my.vim<cr> :wviminfo! my.viminfo<cr>
" " 恢复快捷键
" map <leader>rs :source my.vim<cr> :rviminfo my.viminfo<cr>
" supertab
" let g:SuperTabMappingForward = '<nul>'
" let g:SuperTabMappingBackward = '<s-nul>'
" let g:SuperTabMappingForward = '<s-tab>'
" let g:SuperTabMappingBackward = '<tab>'
"=============================
" => Plugin: YCM
"=============================
""" 菜单
"highlight Pmenu ctermfg=2 ctermbg=3 guifg=#005f87 guibg=#EEE8D5
" 选中项
"highlight PmenuSel ctermfg=2 ctermbg=3 guifg=#AFD700 guibg=#106900
"let g:ycm_global_ycm_extra_conf='~/.vim/bundle/YouCompleteMe/.ycm_extra_conf.py'
"" 允许 vim 加载 .ycm_extra_conf.py 文件,不再提示
"let g:ycm_confirm_extra_conf=0
"set completeopt=longest,menu
""python解释器路径"
"let g:ycm_path_to_python_interpreter='/usr/bin/python3'
""是否开启语义补全"
"let g:ycm_seed_identifiers_with_syntax=1
""是否在注释中也开启补全"
"let g:ycm_complete_in_comments=1
"" 在字符串输入中也能补全
"let g:ycm_complete_in_strings = 1
"let g:ycm_collect_identifiers_from_comments_and_strings = 0
"" 从第二个键入字符才开始罗列匹配项
"let g:ycm_min_num_of_chars_for_completion=2
""补全后自动关闭预览窗口"
"let g:ycm_autoclose_preview_window_after_completion=1
"" 禁止缓存匹配项,每次都重新生成匹配项
"let g:ycm_cache_omnifunc=0
""离开插入模式后自动关闭预览窗口"
"autocmd InsertLeave * if pumvisible() == 0|pclose|endif
""回车即选中当前项"
"inoremap <expr> <CR> pumvisible() ? '<C-y>' : '\<CR>'
""上下左右键行为"
"inoremap <expr> <Down> pumvisible() ? '\<C-n>' : '\<Down>'
"inoremap <expr> <Up> pumvisible() ? '\<C-p>' : '\<Up>'
"inoremap <expr> <PageDown> pumvisible() ? '\<PageDown>\<C-p>\<C-n>' : '\<PageDown>'
"inoremap <expr> <PageUp> pumvisible() ? '\<PageUp>\<C-p>\<C-n>' : '\<PageUp>'
""" 开启 YCM 标签补全引擎
""let g:ycm_collect_identifiers_from_tags_files=1
""" 引入 C++ 标准库tags
""set tags+=/data/misc/software/misc./vim/stdcpp.tags
""" YCM 集成 OmniCppComplete 补全引擎,设置其快捷键
""inoremap <leader>; <C-x><C-o>
""" 补全内容不以分割子窗口形式出现,只显示补全列表
""set completeopt-=preview
""" 语法关键字补全
""let g:ycm_seed_identifiers_with_syntax=1
""" 禁止使用
""let g:ycm_key_list_select_completion = ['<TAB>']
""let g:ycm_key_list_previous_completion = ['<S-TAB>']
""" let g:ycm_key_list_stop_completion = ['<C-y>']
""" Default: ['<C-y>'] 取消自动补全的默认快捷键
"nnoremap <C-J> :YcmCompleter GoToDefinitionElseDeclaration<CR>| " 跳转到定义处
"<<< -------------------- 结束 YCM 配置 ------------------------------
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Miscellaneous
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"=============================
" => Filetype
"=============================
" 开启文件类型侦测
" filetype on
" 根据侦测到的不同类型加载对应的插件
filetype plugin on
" 自适应不同语言的智能缩进
filetype indent on
" => Filetype: Python
let python_highlight_all=1
au Filetype python set tabstop=4 " au[tocmd]: meaning defining autocommands
au Filetype python set softtabstop=4 " au [group] {event} {file pattern} [nested] {cmd}
au Filetype python set shiftwidth=4
"au Filetype python set textwidth=79
au Filetype python set expandtab
au Filetype python set autoindent
au Filetype python set fileformat=unix
autocmd Filetype python set foldmethod=indent
autocmd Filetype python set foldlevel=99
"=============================
" => Helper functions
"=============================
" 将外部命令 wmctrl 控制窗口最大化的命令行参数封装成一个 vim 的函数
fun! ToggleFullscreen()
call system("wmctrl -ir " . v:windowid . " -b toggle,fullscreen")
endf
" 全屏开/关快捷键
map <silent> <F11> :call ToggleFullscreen()<CR>
" 启动 vim 时自动全屏
"autocmd VimEnter * call ToggleFullscreen()
"=============================
" => 文件开头模板
"=============================
"新建.c,.h,.sh,.java文件,自动插入文件头
autocmd BufNewFile *.cpp,*.[ch],*.sh,*.java,*.py exec ":call SetTitle()"
""定义函数SetTitle,自动插入文件头
function! SetTitle()
"如果文件类型为.sh文件
if &filetype == 'sh'
call setline(1,"\#########################################################################")
call append(line("."), "\#!/bin/bash")
call append(line(".")+1, "\# Author: WangGengxin")
call append(line(".")+2, "\# Mail: [email protected]")
call append(line(".")+3, "\# Created Time: ".strftime("%Y-%m-%d",localtime()))
call append(line(".")+4, "\#########################################################################")
call append(line(".")+5, "")
elseif &filetype == 'cpp'
call append(line(".")+5, "#include <iostream>")
call append(line(".")+6, "using namespace std;")
call append(line(".")+7, "")
elseif &filetype == 'c'
call append(line(".")+5, "#include<stdio.h>")
call append(line(".")+6, "")
elseif &filetype == 'python'
call setline(1, "#-*- coding: utf-8 -*-")
call append(line("."),"")
call append(line(".")+1, "\# Author: WangGengxin")
call append(line(".")+2, "\# mail: [email protected]")
call append(line(".")+3, "\# Created Time: ".strftime("%Y-%m-%d",localtime()))
call append(line(".")+4, "")
else
call setline(1, "/*************************************************************************")
call append(line("."), " > Author: WangGengxin")
call append(line(".")+1, " > Mail: [email protected]")
call append(line(".")+2, " > Created Time: ".strftime("%Y-%m-%d",localtime()))
call append(line(".")+3, " ************************************************************************/")
call append(line(".")+4, "")
endif
"新建文件后,自动定位到文件末尾
autocmd BufNewFile * normal G
endfunction