捕获

vim 正则 捕获

在正规表达式中使用 ( 和 ) 符号括起正规表达式,即可在后面使用\1 \2等变量来访问捕获的内容。

将捕获内容前后交换,如下:

1
:s/\(\haha\)\(hehe\)/\2\1/

.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
"基本设置
filetype on "开启文件类型侦测
filetype plugin on "根据侦测到的不同类型加载对应的插件
filetype indent on "自动缩进

syntax on "允许用指定语法高亮配色方案替换默认方案
syntax enable "开启语法高亮功能

set backspace=indent,eol,start "解决insert模式下 backspace 键失效

set mouse=a "设置开启鼠标操作
set bg=dark "设置背景颜色为暗色
"set bg=light "设置背景颜色为亮色
set encoding=utf-8 "设置 vim 展示文本时的编码格式
set fileencoding=utf-8 "设置 vim 写入文件时的编码格式
set relativenumber number "显示行号和相对行号

set ts=4 "设置缩进
set shiftwidth=4
set softtabstop=4
set expandtab "替换tab为空格

set laststatus=2 "总是显示状态栏
set nocompatible "不与vi兼容 关闭兼容模式
set incsearch "开启实时搜索功能
set ignorecase "搜索忽略大小写
set wildmenu "vim命令自动补全
set autoread "若文件发生变化 自动更新显示内容
set ruler "在状态栏显示光标位置
set cursorline "高亮显示当前行
set hlsearch "高亮显示搜索结果 set backspace=2 "开启删除
set scrolloff=5 "最低显示倒数第5行


let mapleader="\<space>" "设置 leader 键为空格

"快捷键设置 nore 的意思是非递归
inoremap jj <esc>
cnoremap jj <esc>
vnoremap jn <esc>

set clipboard^=unnamed,unnamedplus "设置系统剪切板 使vim可以与系统交互剪切板内容
vnoremap <C-C> "+y "设置复制到系统剪切板快捷键

inoremap <C-j> <Down>
inoremap <C-k> <Up>
inoremap <C-h> <Left>
inoremap <C-l> <Right>

"进入shell
nnoremap <F8> :vertical terminal<CR>

"设置vim 主题
"colorscheme everforest

" 改变vim光标形式,插入模式下和命令模式下做区分
if has("autocmd")
au VimEnter,InsertLeave * silent execute '!echo -ne "\e[1 q"' | redraw!
au InsertEnter,InsertChange *
\ if v:insertmode == 'i' |
\ silent execute '!echo -ne "\e[5 q"' | redraw! |
\ elseif v:insertmode == 'r' |
\ silent execute '!echo -ne "\e[3 q"' | redraw! |
\ endif
au VimLeave * silent execute '!echo -ne "\e[ q"' | redraw!
endif

"插件管理

" vundle 环境设置
filetype off
set rtp+=~/.vim/bundle/Vundle.vim
" vundle 管理的插件列表必须位于 vundle#begin() 和 vundle#end() 之间
call vundle#begin()
Plugin 'VundleVim/Vundle.vim'
Plugin 'scrooloose/nerdtree' "文件浏览
Plugin 'octol/vim-cpp-enhanced-highlight' "对c++语法高亮增强
Plugin 'neoclide/coc.nvim', {'branch': 'release'} "自动补全
Plugin 'preservim/nerdcommenter' "多行注释,leader键+cc生成, leader+cu删除注释 cs 以”性感”的方式注释 注意要设置leader键位否则不起作用
Plugin 'skywind3000/asyncrun.vim' "执行 shell 命令 主要用于编译运行代码
Plugin 'honza/vim-snippets' "常用的代码片段 位置 .vim/bundle/vim-snippets/UltiSnips
Plugin '907th/vim-auto-save' "自动保存插件
Plugin 'Yggdroot/indentLine' "缩进线显示
Plugin 'Lokaltog/vim-powerline' "状态栏status美化
" 插件列表结束
call vundle#end()
filetype on


" Powerline 设置-------------------------------------------------------------------
let g:Powerline_colorscheme='solarized256' " 设置状态栏主题风格

"自动保存-------------------------------------------------------------------------------------
let g:auto_save = 1
let g:auto_save_events = ["InsertLeave", "TextChanged", "TextChangedI", "CursorHoldI", "CompleteDone"]


"Coc.nvim 设置-------------------------------------------------------------------------

set pumheight=15 "设置补全窗口的长度
set hidden " TextEdit might fail if hidden is not set.
set updatetime=100 "相应更快
set shortmess+=c "减少无关内容输出
set signcolumn=number "行号与coc信息重合

"面板颜色基本配置 参考 256 色 https://www.ditig.com/256-colors-cheat-sheet
"ctermfg 是终端的配色,256色 ; gui 是Gvim的配色??? 总之gui 似乎不起作用
"PMenu 是面板的颜色 PMenuSel 是选中内容的颜色
"不要用1-15号 系统颜色 貌似会变

"下面这两句 更新之后貌似不起作用了
"highlight PMenu ctermfg=250 ctermbg=240 guifg=#000000 guibg=#bcbcbc
"highlight PMenuSel ctermfg=190 ctermbg=237 guifg=#d7ff00 guibg=#3a3a3a

" 设置 coc 面板颜色 和 报错面板颜色
highlight CocFloating ctermfg=255 ctermbg=237
highlight CocErrorFloat ctermfg=190

"补全相关设置 https://github.com/neoclide/coc.nvim/wiki/Completion-with-sources
" 设置 tab 选择补全项
inoremap <expr> <Tab> coc#pum#visible() ? coc#pum#next(1) : "\<Tab>"
inoremap <expr> <S-Tab> coc#pum#visible() ? coc#pum#prev(1) : "\<S-Tab>"

" 设置确认补全快捷键 <CR> 表示 enter 不是 ctrl 。。。
" Make <CR> to accept selected completion item or notify coc.nvim to format
" <C-g>u breaks current undo, please make your own choice.
inoremap <silent><expr> <CR> coc#pum#visible() ? coc#pum#confirm()
\: "\<C-g>u\<CR>\<c-r>=coc#on_enter()\<CR>"

" 语法错误警告提示跳转
" Use `:CocDiagnostics` to get all diagnostics of current buffer in location list.
nmap <silent> g[ <Plug>(coc-diagnostic-prev)
nmap <silent> g] <Plug>(coc-diagnostic-next)

" GoTo code navigation. 跳转 ctrl+o 跳转回上一个位置
nmap <silent> gd <Plug>(coc-definition)
nmap <silent> gy <Plug>(coc-type-definition)
nmap <silent> gi <Plug>(coc-implementation)
nmap <silent> gr <Plug>(coc-references)

" Symbol renaming. 重命名
nmap <leader>rn <Plug>(coc-rename)

" coc 插件安装列表
let g:coc_global_extensions = ['coc-snippets', 'coc-pyright', 'coc-pairs', 'coc-json', 'coc-highlight', 'coc-cmake', 'coc-clangd','coc-marketplace']

" coc-snippets 设置 tab 跳转
let g:coc_snippet_next = '<tab>'

"Nerdtree--------------------------------------------------------------------------------

" 使用 NERDTree 插件查看工程文件设置快捷键
map <F3> :NERDTreeMirror<CR>
map <F3> :NERDTreeToggle<CR>

let NERDTreeWinSize=32 "设置NERDTree子窗口宽度
let NERDTreeWinPos="left" "设置NERDTree子窗口位置
let NERDTreeShowHidden=0 "显示隐藏文件
let NERDTreeMinimalUI=1 "NERDTree 子窗口中不显示冗余帮助信息
let NERDTreeAutoDeleteBuffer=1 "删除文件时自动删除文件对应 buffer
"autocmd vimenter * NERDTree "自动打开
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTreeType") &&b:NERDTreeType == "primary") | q | endif "最后一个窗口自动关闭


" 注释nerdcommenter-------------------------------------------------------------------------------
" Add spaces after comment delimiters by default
let g:NERDSpaceDelims = 1
" Use compact syntax for prettified multi-line comments
let g:NERDCompactSexyComs = 1
" Align line-wise comment delimiters flush left instead of following code indentation
let g:NERDDefaultAlign = 'left'
" Set a language to use its alternate delimiters by default
let g:NERDAltDelims_java = 1
" Add your own custom formats or override the defaults
let g:NERDCustomDelimiters = { 'c': { 'left': '/**','right': '*/' } }
" Allow commenting and inverting empty lines (useful when commenting a region)
let g:NERDCommentEmptyLines = 1
" Enable trimming of trailing whitespace when uncommenting
let g:NERDTrimTrailingWhitespace = 1
" Enable NERDCommenterToggle to check all selected lines is commented or not
let g:NERDToggleCheckAllLines = 1

"缩进线---------------------------------------------------------------------------------
" Vim
"let g:indentLine_color_term = 250

" GVim
"let g:indentLine_color_gui = '#A4E57E'
" none X terminal
"let g:indentLine_color_tty_light = 7 " (default: 4)
"let g:indentLine_color_dark = 1 " (default: 2)

" Background (Vim, GVim)
"let g:indentLine_bgcolor_term = 250
"let g:indentLine_bgcolor_gui = '#A4E57E'

"let g:indentLine_char_list = ['|', '¦', '┆', '┊']
let g:indentLine_char_list = ['┊']


" asyncrun 设置 ---------------------------------------------------------
" 自动打开 quickfix window ,高度为 6
let g:asyncrun_open = 10
" 任务结束时候响铃提醒
let g:asyncrun_bell = 1
" 设置 F10 打开/关闭 Quickfix 窗口
nnoremap <F10> :call asyncrun#quickfix_toggle(6)<cr>

" 绑定F5 cmake 编译运行 c++ 项目
nnoremap <silent> <F5> :AsyncRun time (cmake -B build && cmake --build build --parallel 8) && echo -e "Running Results↓↓↓" && echo -e "---------------------\n" && time ./bin/main $@ && echo -e "\n---------------------" <cr>


" Gvim 设置---------------------------------------------------------------
if has('gui_running')
" set guioptions-=m " no menu
" set guioptions-=T " no toolbar
set bg=light
colorscheme everforest
set lines=30 columns=70 linespace=-1
if has('gui_win32')
set guifont=DejaVu_Sans_Mono:h14:cANSI
else
set guifont=DejaVu\ Sans\ Mono\ 13
endif
endif
" Gvim 缩进颜色设置
"let g:indentLine_color_gui = '#A4E57E'
" none X terminal
"let g:indentLine_color_tty_light = 7 " (default: 4)
"let g:indentLine_color_dark = 1 " (default: 2)

" Background (Vim, GVim)
"let g:indentLine_bgcolor_term = 250
"let g:indentLine_bgcolor_gui = '#A4E57E'

"自定义函数---------------------------------------------------------------
function! CopyMatches(reg)
let hits = []
%s//\=len(add(hits, submatch(0))) ? submatch(0) : ''/gne
let reg = empty(a:reg) ? '+' : a:reg
execute 'let @'.reg.' = join(hits, "\n") . "\n"'
endfunction
command! -register CopyMatches call CopyMatches(<q-reg>)

coc.nvim 问题

无法补全 有补全菜单但是 无法触发补全

报错

1
2
[coc.nvim] build/index.js not found, please install dependencies and compile coc.nvim by: yarn install
Press ENTER or type command to continue

解决方法

1
2
3
4
5
sudo apt install nodejs
sudo npm install -g yarn
cd ~/.vim/bundle/coc.nvim/
yarn install
yarn build

https://blog.csdn.net/qq_34548075/article/details/120092169

.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
"基本设置
filetype on "开启文件类型侦测
filetype plugin on "根据侦测到的不同类型加载对应的插件
filetype indent on "自动缩进

syntax on "允许用指定语法高亮配色方案替换默认方案
syntax enable "开启语法高亮功能

set backspace=indent,eol,start "解决insert模式下 backspace 键失效

set mouse=a "设置开启鼠标操作
set bg=dark "设置背景颜色为暗色
"set bg=light "设置背景颜色为亮色
set encoding=utf-8 "设置 vim 展示文本时的编码格式
set fileencoding=utf-8 "设置 vim 写入文件时的编码格式
set relativenumber number "显示行号和相对行号

set ts=4 "设置缩进
set shiftwidth=4
set softtabstop=4
set expandtab "替换tab为空格

set laststatus=2 "总是显示状态栏
set nocompatible "不与vi兼容 关闭兼容模式
set incsearch "开启实时搜索功能
set ignorecase "搜索忽略大小写
set wildmenu "vim命令自动补全
set autoread "若文件发生变化 自动更新显示内容
set ruler "在状态栏显示光标位置
set cursorline "高亮显示当前行
set hlsearch "高亮显示搜索结果 set backspace=2 "开启删除
set scrolloff=5 "最低显示倒数第5行


let mapleader="\<space>" "设置 leader 键为空格

"快捷键设置 nore 的意思是非递归
inoremap jj <esc>
cnoremap jj <esc>
vnoremap jn <esc>

set clipboard^=unnamed,unnamedplus "设置系统剪切板 使vim可以与系统交互剪切板内容
vnoremap <C-C> "+y "设置复制到系统剪切板快捷键

inoremap <C-j> <Down>
inoremap <C-k> <Up>
inoremap <C-h> <Left>
inoremap <C-l> <Right>

"进入shell
nnoremap <F8> :vertical terminal<CR>

"设置vim 主题
"colorscheme everforest

" 改变vim光标形式,插入模式下和命令模式下做区分
if has("autocmd")
au VimEnter,InsertLeave * silent execute '!echo -ne "\e[1 q"' | redraw!
au InsertEnter,InsertChange *
\ if v:insertmode == 'i' |
\ silent execute '!echo -ne "\e[5 q"' | redraw! |
\ elseif v:insertmode == 'r' |
\ silent execute '!echo -ne "\e[3 q"' | redraw! |
\ endif
au VimLeave * silent execute '!echo -ne "\e[ q"' | redraw!
endif

"插件管理

" vundle 环境设置
filetype off
set rtp+=~/.vim/bundle/Vundle.vim
" vundle 管理的插件列表必须位于 vundle#begin() 和 vundle#end() 之间
call vundle#begin()
Plugin 'VundleVim/Vundle.vim'
"Plugin 'Valloric/YouCompleteMe' "自动补全感觉不如coc.nvim好用
Plugin 'neoclide/coc.nvim', {'branch': 'release'} "自动补全
Plugin 'puremourning/vimspector' "GUI调试器
Plugin 'octol/vim-cpp-enhanced-highlight' "对c++语法高亮增强
Plugin 'preservim/nerdcommenter' "多行注释,leader键+cc生成, leader+cu删除注释 cs 以”性感”的方式注释 注意要设置leader键位否则不起作用
Plugin 'scrooloose/nerdtree' "文件浏览
Plugin 'Yggdroot/indentLine' "缩进线显示
Plugin 'SirVer/ultisnips' "代码片段插件
Plugin 'honza/vim-snippets' "代码片段插件
Plugin '907th/vim-auto-save' "自动保存插件
Plugin 'Lokaltog/vim-powerline' "状态栏status美化
Plugin 'vim/killersheep' "vim 8.2 游戏
" 插件列表结束
call vundle#end()
filetype on


"代码片段设置 snippets Plugin settings
let g:UltiSnipsExpandTrigger = "<tab>"
let g:UltiSnipsListSnippets = "<c-tab>"
let g:UltiSnipsJumpForwardTrigger = "<tab>"
let g:UltiSnipsJumpBackwardTrigger = "<s-tab>"

" If you want :UltiSnipsEdit to split your window.
let g:UltiSnipsEditSplit="vertical"
"let g:UltiSnipsSnippetDirectories=[$HOME.'/.vim/UltiSnips']
let g:UltiSnipsSnippetDirectories=[$HOME.'/.vim/bundle/vim-snippets/UltiSnips']

" Powerline 设置-------------------------------------------------------------------
let g:Powerline_colorscheme='solarized256' " 设置状态栏主题风格

"自动保存-------------------------------------------------------------------------------------
let g:auto_save = 1
let g:auto_save_events = ["InsertLeave", "TextChanged", "TextChangedI", "CursorHoldI", "CompleteDone"]

""Coc.nvim-------------------------------------------------------------------------

" TextEdit might fail if hidden is not set.
set hidden
" Some servers have issues with backup files, see #649.
set nobackup
set nowritebackup

" Use `g[` and `g]` to navigate diagnostics
" Use `:CocDiagnostics` to get all diagnostics of current buffer in location list.
nmap <silent> g[ <Plug>(coc-diagnostic-prev)
nmap <silent> g] <Plug>(coc-diagnostic-next)

" GoTo code navigation. 定义跳转
nmap <silent> gd <Plug>(coc-definition)
nmap <silent> gy <Plug>(coc-type-definition)
nmap <silent> gi <Plug>(coc-implementation)
nmap <silent> gr <Plug>(coc-references)

" Use K to show documentation in preview window. normal 模式下显示文档
nnoremap <silent> K :call <SID>show_documentation()<CR>

function! s:show_documentation()
if (index(['vim','help'], &filetype) >= 0)
execute 'h '.expand('<cword>')
elseif (coc#rpc#ready())
call CocActionAsync('doHover')
else
execute '!' . &keywordprg . " " . expand('<cword>')
endif
endfunction

" Highlight the symbol and its references when holding the cursor.按住光标时突出显示符号及其引用
autocmd CursorHold * silent call CocActionAsync('highlight')

" Symbol renaming. 重命名
nmap <leader>rn <Plug>(coc-rename)

" Formatting selected code.
xmap <leader>f <Plug>(coc-format-selected)
nmap <leader>f <Plug>(coc-format-selected)

augroup mygroup
autocmd!
" Setup formatexpr specified filetype(s).
autocmd FileType typescript,json setl formatexpr=CocAction('formatSelected')
" Update signature help on jump placeholder.
autocmd User CocJumpPlaceholder call CocActionAsync('showSignatureHelp')
augroup end

" Applying codeAction to the selected region.
" Example: `<leader>aap` for current paragraph
xmap <leader>a <Plug>(coc-codeaction-selected)
nmap <leader>a <Plug>(coc-codeaction-selected)

" Remap keys for applying codeAction to the current buffer.
nmap <leader>ac <Plug>(coc-codeaction)
" Apply AutoFix to problem on the current line.
nmap <leader>qf <Plug>(coc-fix-current)

" Run the Code Lens action on the current line.
nmap <leader>cl <Plug>(coc-codelens-action)

" Map function and class text objects
" NOTE: Requires 'textDocument.documentSymbol' support from the language server.
xmap if <Plug>(coc-funcobj-i)
omap if <Plug>(coc-funcobj-i)
xmap af <Plug>(coc-funcobj-a)
omap af <Plug>(coc-funcobj-a)
xmap ic <Plug>(coc-classobj-i)
omap ic <Plug>(coc-classobj-i)
xmap ac <Plug>(coc-classobj-a)
omap ac <Plug>(coc-classobj-a)

" Remap <C-f> and <C-b> for scroll float windows/popups.
if has('nvim-0.4.0') || has('patch-8.2.0750')
nnoremap <silent><nowait><expr> <C-f> coc#float#has_scroll() ? coc#float#scroll(1) : "\<C-f>"
nnoremap <silent><nowait><expr> <C-b> coc#float#has_scroll() ? coc#float#scroll(0) : "\<C-b>"
inoremap <silent><nowait><expr> <C-f> coc#float#has_scroll() ? "\<c-r>=coc#float#scroll(1)\<cr>" : "\<Right>"
inoremap <silent><nowait><expr> <C-b> coc#float#has_scroll() ? "\<c-r>=coc#float#scroll(0)\<cr>" : "\<Left>"
vnoremap <silent><nowait><expr> <C-f> coc#float#has_scroll() ? coc#float#scroll(1) : "\<C-f>"
vnoremap <silent><nowait><expr> <C-b> coc#float#has_scroll() ? coc#float#scroll(0) : "\<C-b>"
endif

" Use CTRL-S for selections ranges.
" Requires 'textDocument/selectionRange' support of language server.
nmap <silent> <C-s> <Plug>(coc-range-select)
xmap <silent> <C-s> <Plug>(coc-range-select)

" Add `:Format` command to format current buffer.
command! -nargs=0 Format :call CocActionAsync('format')

" Add `:Fold` command to fold current buffer.
command! -nargs=? Fold :call CocAction('fold', <f-args>)

" Add `:OR` command for organize imports of the current buffer.
command! -nargs=0 OR :call CocActionAsync('runCommand', 'editor.action.organizeImport')

" Add (Neo)Vim's native statusline support.
" NOTE: Please see `:h coc-status` for integrations with external plugins that
" provide custom statusline: lightline.vim, vim-airline.
set statusline^=%{coc#status()}%{get(b:,'coc_current_function','')}

" Mappings for CoCList
" Show all diagnostics.
nnoremap <silent><nowait> <space>a :<C-u>CocList diagnostics<cr>
" Manage extensions.
nnoremap <silent><nowait> <space>e :<C-u>CocList extensions<cr>
" Show commands.
nnoremap <silent><nowait> <space>c :<C-u>CocList commands<cr>
" Find symbol of current document.
nnoremap <silent><nowait> <space>o :<C-u>CocList outline<cr>
" Search workspace symbols.
nnoremap <silent><nowait> <space>s :<C-u>CocList -I symbols<cr>
" Do default action for next item.
nnoremap <silent><nowait> <space>j :<C-u>CocNext<CR>
" Do default action for previous item.
nnoremap <silent><nowait> <space>k :<C-u>CocPrev<CR>
" Resume latest coc list.
nnoremap <silent><nowait> <space>p :<C-u>CocListResume<CR>

"YCM-----------------------------------------------------------------------------------------

"基本配置 参考 256 色 https://www.ditig.com/256-colors-cheat-sheet
"ctermfg 是终端的配色,256色 ; gui 是Gvim的配色??? 总之gui 似乎不起作用
"PMenu 是面板的颜色
"PMenuSel 是选中内容的颜色
"不要用1-15号 系统颜色 貌似会变
set t_Co=256
" highlight PMenu ctermfg=16 ctermbg=250 guifg=#000000 guibg=#bcbcbc
" highlight PMenuSel ctermfg=190 ctermbg=237 guifg=#d7ff00 guibg=#3a3a3a
highlight PMenu ctermfg=250 ctermbg=240 guifg=#000000 guibg=#bcbcbc
highlight PMenuSel ctermfg=190 ctermbg=237 guifg=#d7ff00 guibg=#3a3a3a


let g:ycm_show_diagnostics_ui = 0 "禁止语法检查
set completeopt+=popup
set completeopt=longest,menu "让Vim的补全菜单行为与一般IDE一致(参考VimTip1228)
autocmd InsertLeave * if pumvisible() == 0|pclose|endif "离开插入模式后自动关闭预览窗"回车即选中当前项
inoremap <expr> <Enter> pumvisible() ? "\<C-y><ESC>a" : "\<Enter>" "回车即选中当前项
nnoremap <leader>j :YcmCompleter GoToDefinitionElseDeclaration<CR> "跳转到定义处


let g:ycm_key_list_select_completion = ['<Down>'] "snippets 插件默认tab s-tab 和youcompleteme自动补全冲突
let g:ycm_key_list_previous_completion = ['<Up>']

"设置语义补全
let g:ycm_semantic_triggers = {
\ 'c,cpp,python,java,go,erlang,perl': ['re!\w{2}'],
\ 'cs,lua,javascript': ['re!\w{2}'],
\ }
"配置文件
let g:ycm_global_ycm_extra_conf='~/.vim/bundle/YouCompleteMe/third_party/ycmd/examples/.ycm_extra_conf.py'


"vimspector----------------------------------------------------------------------------------https://www.jianshu.com/p/ddcb374611e2

let g:vimspector_enable_mappings = 'VISUAL_STUDIO'
sign define vimspectorBP text=☛ texthl=Normal
sign define vimspectorBPDisabled text=☞ texthl=Normal
"sign define vimspectorPC text=¶ texthl=SpellBad

"Nerdtree--------------------------------------------------------------------------------

" 使用 NERDTree 插件查看工程文件设置快捷键
map <F3> :NERDTreeMirror<CR>
map <F3> :NERDTreeToggle<CR>

let NERDTreeWinSize=32 "设置NERDTree子窗口宽度
let NERDTreeWinPos="left" "设置NERDTree子窗口位置
let NERDTreeShowHidden=0 "显示隐藏文件
let NERDTreeMinimalUI=1 "NERDTree 子窗口中不显示冗余帮助信息
let NERDTreeAutoDeleteBuffer=1 "删除文件时自动删除文件对应 buffer
"autocmd vimenter * NERDTree "自动打开
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTreeType") &&b:NERDTreeType == "primary") | q | endif "最后一个窗口自动关闭


" 注释nerdcommenter-------------------------------------------------------------------------------
" Add spaces after comment delimiters by default
let g:NERDSpaceDelims = 1
" Use compact syntax for prettified multi-line comments
let g:NERDCompactSexyComs = 1
" Align line-wise comment delimiters flush left instead of following code indentation
let g:NERDDefaultAlign = 'left'
" Set a language to use its alternate delimiters by default
let g:NERDAltDelims_java = 1
" Add your own custom formats or override the defaults
let g:NERDCustomDelimiters = { 'c': { 'left': '/**','right': '*/' } }
" Allow commenting and inverting empty lines (useful when commenting a region)
let g:NERDCommentEmptyLines = 1
" Enable trimming of trailing whitespace when uncommenting
let g:NERDTrimTrailingWhitespace = 1
" Enable NERDCommenterToggle to check all selected lines is commented or not
let g:NERDToggleCheckAllLines = 1


"缩进线---------------------------------------------------------------------------------
" Vim
"let g:indentLine_color_term = 250

" GVim
"let g:indentLine_color_gui = '#A4E57E'
" none X terminal
"let g:indentLine_color_tty_light = 7 " (default: 4)
"let g:indentLine_color_dark = 1 " (default: 2)

" Background (Vim, GVim)
"let g:indentLine_bgcolor_term = 250
"let g:indentLine_bgcolor_gui = '#A4E57E'

"let g:indentLine_char_list = ['|', '¦', '┆', '┊']
let g:indentLine_char_list = ['┊']

" Gvim 设置---------------------------------------------------------------
if has('gui_running')
set guioptions-=m " no menu
set guioptions-=T " no toolbar
set bg=light
colorscheme everforest
set lines=40 columns=100 linespace=-1
if has('gui_win32')
set guifont=DejaVu_Sans_Mono:h14:cANSI
else
set guifont=DejaVu\ Sans\ Mono\ 13
endif
endif
" Gvim 缩进颜色设置
"let g:indentLine_color_gui = '#A4E57E'
" none X terminal
"let g:indentLine_color_tty_light = 7 " (default: 4)
"let g:indentLine_color_dark = 1 " (default: 2)

" Background (Vim, GVim)
"let g:indentLine_bgcolor_term = 250
"let g:indentLine_bgcolor_gui = '#A4E57E'

"自定义函数---------------------------------------------------------------
function! CopyMatches(reg)
let hits = []
%s//\=len(add(hits, submatch(0))) ? submatch(0) : ''/gne
let reg = empty(a:reg) ? '+' : a:reg
execute 'let @'.reg.' = join(hits, "\n") . "\n"'
endfunction
command! -register CopyMatches call CopyMatches(<q-reg>)


vim 插件

vim signature

1
2
3
4
5
6
mx        标记当前行
dmx 删除标记x
m<Space> 删除所有标记
m/ 列出所有标记
]` 跳转到下一个标记
[' 跳转到上一个标记

vim spector 安装配置

https://www.jianshu.com/p/ddcb374611e2
/

vim 自动保存

1 安装 vim-auto-save 插件

2 在vimrc中加入下面内容

1
2
3
"自动保存
let g:auto_save = 1
let g:auto_save_events = ["InsertLeave", "TextChanged", "TextChangedI", "CursorHoldI", "CompleteDone"]

https://www.cnblogs.com/awakenedy/p/9722361.html
https://github.com/907th/vim-auto-save

vim 折叠设置

https://www.cnblogs.com/welkinwalker/archive/2011/05/30/2063587.html

vim 跟!r w有关的命令

:!{cmd} 执行外部shell命令
:r[ead] !{cmd} 外部shell命令输出结果保存到文件
:w[rite] !{cmd} 文件内容作为输入到传递给外部shell命令 (但不知道为什么 echo 命令不起作用)
:w filename 另存为

改变 gnome 终端vim下的光标形式

https://vim.fandom.com/wiki/Change_cursor_shape_in_different_modes
https://blog.csdn.net/a1eafall/article/details/55803582

在.vimrc加入

1
2
3
4
5
6
7
8
9
10
if has("autocmd")
au VimEnter,InsertLeave * silent execute '!echo -ne "\e[1 q"' | redraw!
au InsertEnter,InsertChange *
\ if v:insertmode == 'i' |
\ silent execute '!echo -ne "\e[5 q"' | redraw! |
\ elseif v:insertmode == 'r' |
\ silent execute '!echo -ne "\e[3 q"' | redraw! |
\ endif
au VimLeave * silent execute '!echo -ne "\e[ q"' | redraw!
endif

自定义命令(转载)

作者:YYQ
链接:https://zhuanlan.zhihu.com/p/27389503
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
https://link.zhihu.com/?target=https%3A//gist.github.com/yyq123/8b0756ba00373bf36b6a8374fb1707c8%23file-scriptcommandmulti-vim

令Vim编辑器允许定义自己的命令,我们可以像执行内置命令一样来执行我们自己定义的命令。使用以下:command命令自定义命令:

1
:command Delete_first : 1delete

注意自定义命令的名称,必须以大写字母开头,而且不能包含下划线;如果我们执行:Delete_first自定义命令,那么Vim就会执行:1delete命令,从而删除第一行。可以使用!来强制重新定义同名的自定义命令:
1
:command! -nargs=+ Say : echo <args>

用户定义的命令可以指定一系列的参数,参数的个数由-nargs选项在命令行中指定。例如定义Delete_one命令没有参数:
1
:command Delete_one -nargs=0 1delete

默认情况下-nargs=0,所以可以省略。其他-nargs选项值如下:
-nargs=0 没有参数
-nargs=1 1个参数
-nargs=* 任何个数的参数
-nargs=? 零个或是一个参数
-nargs=+ 一个或是更多个参数

在命令定义中,参数是由关键字指定的:

1
:command -nargs=+ Say : echo "<args>"

输入以下自定义命令:
1
:Say Hello World

命令的执行结果显示:
1
Hello World

使用-range选项,可以指定一个范围作为自定义命令的参数。-range选项值如下:
-range允许范围,默认为当前行
-range=%允许范围,默认为当前文件(while file)
-range=count允许范围,单一的数字

当指定范围之后,就可以用关键字得到这个范围的第一行和最后一行。例如以下定义了SaveIt命令,用于将指定范围的文件写入文件save_file:

1
:command -range=% SaveIt : <line1>,<line2>write! save_file

关键字含有与关键字相同的信息,所不同的是它用于调用函数。例如以下自定义命令:
1
:command -nargs=* DoIt : call AFunction(<f-args>)

执行自定义命令:
1
:DoIt a b c

将会传递参数给调用的函数:

1
:call AFunction("a","b","c")

其他选项和关键字包括:
-count=number指定数量保存在关键字
-bang指定!修饰符存放在关键字
-register指定寄存器,默认为未命名寄存器,寄存器的定义保存在关键字
-bar其他命令可以用|跟随在此命令之后
-buffer命令仅对当前缓冲区有效

使用以下命令,首先分别创建一个用户自定义命令,然后再将两个命令组合起来。

1
2
3
command! -bar DelTab %s/	//
command! DelLF %s/\n//
command! FmtCode DelTab|DelLF

列示自定义命令使用以下命令,可以列出用户定义的命令:

1
:command

删除自定义命令使用以下:delcommand命令,可以删除用户定义的命令:

1
:delcommand Delete_one

使用以下命令,清除所有的用户定义的命令:
1
:comclear

日常使用 快捷键

ctrl + w + h 光标 focus 左侧树形目录
ctrl + w + l 光标 focus 右侧文件显示窗口
ctrl + w + w 光标自动在左右侧窗口切换
ctrl + w + r 移动当前窗口的布局位置

o 在已有窗口中打开文件、目录或书签,并跳到该窗口
go 在已有窗口 中打开文件、目录或书签,但不跳到该窗口
t 在新 Tab 中打开选中文件/书签,并跳到新 Tab
T 在新 Tab 中打开选中文件/书签,但不跳到新 Tab
i split 一个新窗口打开选中文件,并跳到该窗口
gi split 一个新窗口打开选中文件,但不跳到该窗口
s vsplit 一个新窗口打开选中文件,并跳到该窗口
gs vsplit 一个新 窗口打开选中文件,但不跳到该窗口
! 执行当前文件
O 递归打开选中 结点下的所有目录
x 合拢选中结点的父目录
X 递归 合拢选中结点下的所有目录
e Edit the current dif
双击 相当于 NERDTree-o
中键 对文件相当于 NERDTree-i,对目录相当于 NERDTree-e
D 删除当前书签
P 跳到根结点
p 跳到父结点
K 跳到当前目录下同级的第一个结点
J 跳到当前目录下同级的最后一个结点
k 跳到当前目录下同级的前一个结点
j 跳到当前目录下同级的后一个结点
C 将选中目录或选中文件的父目录设为根结点
u 将当前根结点的父目录设为根目录,并变成合拢原根结点
U 将当前根结点的父目录设为根目录,但保持展开原根结点
r 递归刷新选中目录
R 递归刷新根结点
m 显示文件系统菜单
cd 将 CWD 设为选中目录
I 切换是否显示隐藏文件
f 切换是否使用文件过滤器
F 切换是否显示文件
B 切换是否显示书签
q 关闭 NerdTree 窗口
? 切换是否显示 Quick Help

切换标签页

:tabnew [++opt选项] [+cmd] 文件 建立对指定文件新的tab
:tabc 关闭当前的 tab
:tabo 关闭所有其他的 tab
:tabs 查看所有打开的 tab
:tabp 前一个 tab
:tabn 后一个 tab
标准模式下:
gT 前一个 tab
gt 后一个 tab
MacVim 还可以借助快捷键来完成 tab 的关闭、切换
cmd+w 关闭当前的 tab
cmd+{ 前一个 tab
cmd+} 后一个 tab

NerdTree 在 .vimrc 中的常用配置

“ 在 vim 启动的时候默认开启 NERDTree(autocmd 可以缩写为 au)
autocmd VimEnter * NERDTree
“ 按下 F2 调出/隐藏 NERDTree
map :silent! NERDTreeToggle
“ 将 NERDTree 的窗口设置在 vim 窗口的右侧(默认为左侧)
let NERDTreeWinPos=”right”
“ 当打开 NERDTree 窗口时,自动显示 Bookmarks
let NERDTreeShowBookmarks=1

五、查找替换
/pattern 向后搜索字符串pattern

?pattern 向前搜索字符串pattern

“\c” 忽略大小写

“\C” 大小写敏感
n 下一个匹配(如果是/搜索,则是向下的下一个,?搜索则是向上的下一个)
N 上一个匹配(同上)
:%s/old/new/g 搜索整个文件,将所有的old替换为new
:%s/old/new/gc 搜索整个文件,将所有的old替换为new,每次都要你确认是否替换

六、退出编辑器
:w 将缓冲区写入文件,即保存修改
:wq 保存修改并退出
:x 保存修改并退出
:q 退出,如果对缓冲区进行过修改,则会提示
:q! 强制退出,放弃修改

键盘移动 (Move)

一切都从键盘的移动
k -> 上 up
j -> 下 down
h -> 左 left
l -> 右 right
z -> 重画屏幕,当前光标变成屏幕的第一行 (redraw current line at top of window)
CTRL-f -> 跳到下一页 (page down)
CTRL-b -> 跳到上一页 (page up)
跳跃指令 (jumps)

跳跃指令类似于游览器中的<前进><后退>按钮
CTRL-] -> 跟着link/tag转入 (follow link/tag)
CTRL-o -> 回到上一次的jump (go back)
CTRL-i -> 跳回下一个 (go forward)
:ju -> 显示所有的可以跳跃的地方 (print jump list)
重做/回复

u -> undo
CTRL-r -> redo
vim的undo是树结构的,你可以回到这个结构中的任何地方
:undo 2 -> undo 到结构的2层 (undo to tree 2)
:undolist -> 显示所有的undo列表 (show undo list)
:earlier 10s -> undo到10秒前的编辑 (undo to 10 seconds ago)
:earlier 10h -> undo到10小时前的编辑 (back to 10 hours ago)
:earlier 1m -> undo到1分钟前 (back to 1 minutes ago)
下面是undo的tree结构的解释
………..one
…………. |
……..change 1
…………. |
………one too
………. /……..\
…..change 2 ……. change 3
………… | ………………… |
…….one two ………. me too
……….. |
….. change 4
………..|
…… not two
视觉模式 (visual)

v -> 进入视觉模式
在视觉模式内可以作block的编辑
CTRL-v -> visual block
打印 (print)

:hardcopy -> 打印vim中的内容 (print text)
混合视觉模式 (visual) 可以选择打印的区域
没试过是否可以直接给值打印(应该可以)例如 :1,15hardcopy 打印前15行
将文件写成网页格式 (html)

:source $VIMRUNTIME/syntax/2html.vim -> change current open file to html
格式 (format)

dos/windows跟unix/linux对于文件的结束是不一样的。vim可以直接设定/更改格式
用纸令:set fileformats=unix,dos 可以改变文件的格式 (change format)

:set ff=unix -> 设定文件成unix格式 (set file in unix format)
:set ff=dos -> 设定文件成dos格式 (set file in dos format)
:set ff? -> 检查当前文件格式 (check the format of current file)
如果改变格式,直接:w存档就会存成新的格式了。
加密 (encryption)

vim可以给文件加密码
vim -x 文件名 (filename) -> 输入2次密码,保存后文件每次都会要密码才能进入 (encrypt the file with password)
vim 处理加密文件的时候,并不会作密码验证,也就是说,当你打开文件的时候,vim不管你输入的密码是否正确,直接用密码对本文进行解密。如果密码错误,你看 到的就会是乱码,而不会提醒你密码错误(这样增加了安全性,没有地方可以得知密码是否正确)当然了,如果用一个够快的机器作穷举破解,vim还是可以揭开 的
vim 语法显示 (syntax)

:syntax enable -> 打开语法的颜色显示 (turn on syntax color)
:syntax clear -> 关闭语法颜色 (remove syntax color)
:syntax off -> 完全关闭全部语法功能 (turn off syntax)
:syntax manual -> 手动设定语法 (set the syntax manual, when need syntax use :set syntax=ON)
输入特殊字符 (special character)

CTRL-v 编码就可以了
例如 CTRL-v 273 -> ÿ 得到 ÿ
二进 制文件 (binary file)

vim可以显示,编辑2进位文件

vim -b datafile
:set display=uhex -> 这样会以uhex显示。用来显示一些无法显示的字符(控制字符之类)(display in uhex play non-display char)

:%!xxd -> 更改当前文件显示为2进位 (change display to binary)
:%!xxd -r -> 更改二进位为text格式 (convert back to text)
自动完成 (auto-completion)

vim本身有自动完成功能(这里不是说ctag,而是vim内建的)
CTRL-p -> 向后搜索自动完成 (search backward)
CTRL-n -> 向前搜索自动完成 (search forward)
CTRL-x+CTRL-o -> 代码自动补全 (code completion)
自动备份 (backup)

vim可以帮你自动备份文件(储存的时候,之前的文件备份出来)
:set backup -> 开启备份,内建设定备份文件的名字是 源文件名加一个 ‘~’ (enable backup default filename+~)
:set backupext=.bak -> 设定备份文件名为源文件名.bak (change backup as filename.bak)

自动备份有个问题就是,如果你多次储存一个文件,那么这个你的备份文件会被不断覆盖,你只能有最后一次存文件之前的那个备份。没关系,vim还提 供了patchmode,这个会把你第一次的原始文件备份下来,不会改动
:set patchmode=.orig -> 保存原始文件为 文件名.orig (keep orignal file as filename.orig)
开启,保存与退出 (save & exit)

:w -> 保存文件 (write file)
:w! -> 强制保存 (force write)
:q -> 退出文件 (exit file without save)
:q! -> 强制退出 (force quite without save)
:e filename -> 打开一个文件名为filename的文件 (open file to edit)
:e! filename -> 强制打开一个文件,所有未保存的东西会丢失 (force open, drop dirty buffer)
:saveas filename -> 另存为 filename (save file as filename)
编辑指令 (edit)

a -> 在光表后插入 (append after cursor)
A -> 在一行的结尾插入 (append at end of the line)
i -> 在光标前插入 (insert before cursor)
I -> 在第一个非空白字符前插入 (insert before first non-blank)
o -> 光标下面插入一个新行 (open line below)
O -> 光标上面插入一个新行 (open line above)
x -> 删除光标下(或者之后)的东西 (delete under and after cursor)
例如x就是删除当前光标下,3x就是删除光标下+光标后2位字符
X -> 删除光标前的字符 (delete before cursor)
d -> 删除 (delete)
可以用dd删除一行,或者3dw删除3个词等等
J -> 将下一行提到这行来 (join line)
r -> 替换个字符 (replace characters)
R -> 替换多个字符 (replace mode – continue replace)
gr -> 不影响格局布置的替换 (replace without affecting layout)
c -> 跟d键一样,但是删除后进入输入模式 (same as “d” but after delete, in insert mode)
S -> 删除一行(好像dd一样)但是删除后进入输入模式 (same as “dd” but after delete, in insert mode)
s -> 删除字符,跟(d)一样,但是删除后进入输入模式 (same as “d” but after delete, in insert mode)
s4s 会删除4个字符,进入输入模式 (delete 4 char and put in insert mode)
~ -> 更改大小写,大变小,小变大 (change case upper-> lower or lower->upper)
gu -> 变成小写 (change to lower case)
例如 guG 会把光标当前到文件结尾全部变成小写 (change lower case all the way to the end)
gU -> 变成大写 (change to upper case)
例如 gUG 会把光标当前到文件结尾全部变成大写 (change upper case all the way to the end)
复制与粘贴 (copy & paste)

y -> 复制 (yank line)
yy -> 复制当前行 (yank current line)
“{a-zA-Z}y -> 把信息复制到某个寄存中 (yank the link into register {a-zA-Z})
例如我用 “ayy 那么在寄存a,就复制了一行,然后我再用“byw复制一个词在寄存b
粘贴的时候,我可以就可以选择贴a里面的东西还是b里面的,这个就好像是多个复制版一样
y -> 这个是把信息复制进系统的复制版(可以在其他程序中贴出来)(yank to OS buffer)
p -> 当前光标下粘贴 (paste below)
P -> 当前光标上粘贴 (paste above)
“{a-zA-Z}p -> 将某个寄存的内容贴出来 (paste from register)
例如“ap那么就在当前光标下贴出我之前在寄存a中 的内容。“bP就在当前光标上贴出我之前寄存b的内容
p -> 从系统的剪贴板中读取信息贴入vim (paste from OS buffer to vim)
reg -> 显示所有寄存中的内容 (list all registers)
书签 (Mark)

书签是vim中非常强大的一个功能,书签分为文件书签跟全局书签。文件书签是你标记文件中的不同位置,然后可以在文件内快速跳转到你想要的位置。 而全局书签是标记不同文件中的位置。也就是说你可以在不同的文件中快速跳转

m{a-zA-Z} -> 保存书签,小写的是文件书签,可以用(a-z)中的任何字母标记。大写的是全局 书签,用大写的(A-Z)中任意字母标记。(mark position as bookmark. when lower, only stay in file. when upper, stay in global)
‘{a-zA-Z} -> 跳转到某个书签。如果是全局书签,则会开启被书签标记的文件跳转至标记的行 (go to mark. in file {a-z} or global {A-Z}. in global, it will open the file)
’0 -> 跳转入现在编辑的文件中上次退出的位置 (go to last exit in file)
” -> 跳转如最后一次跳转的位置 (go to last jump -> go back to last jump)
‘” -> 跳转至最后一次编辑的位置 (go to last edit)
g’{mark} -> 跳转到书签 (jump to {mark})
:delm{marks} -> 删除一个书签 (delete a mark) 例如:delma那么就删除了书签a
:delm! -> 删除全部书签 (delete all marks)
:marks -> 显示系统全部书签 (show all bookmarks)
标志 (tag)

:ta -> 跳转入标志 (jump to tag)
:ts -> 显示匹配标志,并且跳转入某个标志 (list matching tags and select one to jump)
:tags -> 显示所有标志 (print tag list)
运行外部命令 (using an external program)

:! -> 直接运行shell中的一个外部命令 (call any external program)
:!make -> 就直接在当前目录下运行make指令了 (run make on current path)
:r !ls -> 读取外部运行的命令的输入,写入当然vim中。这里读取ls的输出 (read the output of ls and append the result to file)
:3r !date -u -> 将外部命令date -u的结果输入在vim的第三行中 (read the date -u, and append result to 3rd line of file)

:w !wc -> 将vim的内容交给外部指令来处理。这里让wc来处理vim的内容 (send vim’s file to external command. this will send the current file to wc command)
vim对于常用指令有一些内建,例如wc (算字数)(vim has some buildin functions, such like wc)
g CTRL-G -> 计算当前编译的文件的字数等信息 (word count on current buffer)
!!date -> 插入当前时间 (insert current date)
多个文件的编辑 (edit multifiles)

vim可以编辑多个文件,例如
vim a.txt b.txt c.txt 就打开了3个文件

:next -> 编辑下一个文件 (next file in buffer)
:next! -> 强制编辑下个文件,这里指如果更改了第一个文件 (force to next file in buffer if current buffer changed)
:wnext -> 保存文件,编辑下一个 (save the file and goto next)
:args -> 查找目前正在编辑的文件名 (find out which buffer is editing now)
:previous -> 编辑上个文件 (previous buffer)
:previous! -> 强制编辑上个文件,同 :next! (force to previous buffer, same as :next!)
:last -> 编辑最后一个文件 (last buffer)
:first -> 编辑最前面的文件 (first buffer)
:set autowrite -> 设定自动保存,当你编辑下一个文件的时候,目前正在编辑的文件如果改动,将会自动保存 (automatic write the buffer when you switch to next buffer)
:set noautowrite -> 关闭自动保存 (turn autowrite off)
:hide e abc.txt -> 隐藏当前文件,打开一个新文件 abc.txt进行编辑 (hide the current buffer and edit abc.txt)
:buffers -> 显示所有vim中的文件 (display all buffers)
:buffer2 -> 编辑文件中的第二个 (edit buffer 2)

vim中很多东西可以用简称来写,就不用打字那么麻烦了,例如 :edit=:e, :next=:n 这样.
分屏 (split)

vim提供了分屏功能(跟screen里面的split一样)
:split -> 将屏幕分成2个 (split screen)
:split abc.txt -> 将屏幕分成两个,第二个新的屏幕中显示abc.txt的内容 (split the windows, on new window, display abc.txt)
:vsplit -> 竖着分屏 (split vertically)
:{d}split -> 设定分屏的行数,例如我要一个屏幕只有20行,就可以下:20split (split the windows with {d} line. 20split: open new windows with 3 lines)
:new -> 分屏并且在新屏中建立一个空白文件 (split windows with a new blank file)
CTRL-w+j/k/h/l -> 利用CTRL加w加上j/k/h/l在不同的屏内切换 (switch, move between split screens)
CTRL-w+ -/+ -> 增减分屏的大小 (change split size)
CTRL-w+t -> 移动到最顶端的那个屏 (move to the top windows)
CTRL-w+b -> 移动到最下面的屏 (move to bottom window)
:close -> 关闭一个分出来的屏 (close splited screen)
: only -> 只显示光标当前屏 ,其他将会关闭(only display current active screen, close all others )
:qall -> 退出所有屏 (quite all windows)
:wall -> 保存所有屏 (write to all windows)
:wqall -> 保存并退出所有屏 (write and quite all windows)
:qall! -> 退出所有屏,不保存任何变动 (quite all windows without save)
开启文件的时候,利用 -o选项,就可以直接开启多个文件在分屏中 (with -o option from command line, it will open files and display in split mode)
vim -o a.txt b.txt

今天有人说不会看diff,其实vim也可以用来看diff,这个也是属于分屏的部分,这里也写一下。

vimdiff a.txt b.txt 如果直接给 -d选项是一样的 vim -d a.txt b.txt
:diffsplit abc.txt 如果你现在已经开启了一个文件,想vim帮你区分你的文件跟abc.txt有什么区别,可以在vim中用diffsplit的方式打开第二个文件,这个时 候vim会用split的方式开启第二个文件,并且通过颜色,fold来显示两个文件的区别
这样vim就会用颜色帮你区分开2个文件的区别。如果文件比较大(源码)重复的部分会帮你折叠起来(折叠后面会说)
现在来说patch
:diffpatch filename 通过:diffpatch 你的patch的文件名,就可以以当前文件加上你的patch来显示。vim会split一个新的屏,显示patch后的信息并且用颜色标明区别。
如果不喜欢上下对比,喜欢左右(比较符合视觉)可以在前面加vert,例如:
:vert diffsplit abc.txt
:vert diffpatch abc.txt
看完diff,用: only回到原本编辑的文件,觉 得diff的讨厌颜色还是在哪里,只要用:diffoff关闭就好了。
还有个常用的diff中的就是 :diffu 这个是 :diffupdate 的简写,更新用
TAB

除了split之外, vim还可以用 tab

:tab split filename -> 这个就用tab的方式来显示多个文件 (use tab to display buffers)
gt -> 到下一个tab (go to next tab)
gT -> 到上一个tab (go to previous tab)
vim大多数东西都是可一给数字来执行的,tab也是一样
0gt ->跳到第一个tab (switch to 1st tab)
5gt -> 跳到第五个tab (switch to 5th tab)
关闭所有的tab可以使用qall的指令。另外让vim在启动的时候就自动用tabnew的方式来开启多个文件,可以用alias
linux: 添加 alias vim=’vim -p’ 到 ~/.bashrc
windows: 自己写个vim.bat的文件,然后放在path中,文件内容:
@echo off
vim -p %*
当需要更改多个tab中的文件的时候,可以用 :tabdo 这个指令 这个就相当于 loop 到你的所有的 tab 中然后运行指令。
例如有5个文件都在tab里面,需要更改一个变量名称:abc 到 def, 就可以用 :tabdo %s/abc/def/g 这样所有的5个tab里面的abc就都变成def了

折叠 (folding)

vim的折叠功能。。。我记得应该是6版出来的时候才推出的吧。这个对于写程序的人来说,非常有用。
zfap -> 按照段落折叠 (fold by paragraph)
zo -> 打开一个折叠 (open fold)
zc -> 关闭一个折叠 (close fold)
zf -> 创建折叠 (create fold) 这个可以用v视觉模式,可以直接给行数等等
zr -> 打开一定数量的折叠,例如3rz (reduce the folding by number like 3zr)
zm -> 折叠一定数量(之前你定义好的折叠) (fold by number)
zR -> 打开所有的折叠 (open all fold)
zM -> 关闭所有的摺叠 (close all fold)
zn -> 关闭折叠功能 (disable fold)
zN -> 开启折叠功能 (enable fold)
zO -> 将光标下所有折叠打开 (open all folds at the cursor line)
zC -> 将光标下所有折叠关闭 (close all fold at cursor line)
zd -> 将光标下的折叠删除,这里不是删除内容,只是删除折叠标记 (delete fold at cursor line)
zD -> 将光标下所有折叠删除 (delete all folds at the cursor line)
按照tab来折叠,python最好用的 (ford by indent, very useful for python)
:set foldmethod=indent -> 设定后用zm 跟 zr 就可以的开关关闭了 (use zm zr)
保存 (save view)

对于vim来说,如果你设定了折叠,但是退出文件,不管是否保持文件,折叠部分会自动消失的。这样来说非常不方便。所以vim给你方法去保存折 叠,标签,书签等等记录。最厉害的是,vim对于每个文件可以保存最多10个view,也就是说你可以对同一个文件有10种不同的标记方法,根据你的需 要,这些东西都会保存下来。
:mkview -> 保存记录 (save setting)
:loadview -> 读取记录 (load setting)
:mkview 2 -> 保存记录在寄存2 (save view to register 2)
:loadview 3 -> 从寄存3中读取记录 (load view from register 3)
常用指令 (commands)

:set ic ->设定为搜索时不区分大小 写 (search case insensitive)
:set noic ->搜索时区分大小写。 vim内定是这个(case sensitive )
& -> 重复上次的”:s” (repeat previous “:s”)
. -> 重复上次的指令 (repeat last command)
K -> 在man中搜索当前光标下的词 (search man page under cursor)
{0-9}K -> 查找当前光标下man中的章节,例如5K就是同等于man 5 (search section of man. 5K search for man 5)
:history -> 查看命令历史记录 (see command line history)
q: -> 打开vim指令窗口 (open vim command windows)
:e -> 打开一个文件,vim可以开启http/ftp/scp的文件 (open file. also works with http/ftp/scp)
:e http://www.google.com/index.html -> 这里就在vim中打开google的index.html (open google’s index.html)
:cd -> 更换vim中的目录 (change current directory in vim)
:pwd -> 显示vim当前目录 (display pwd in vim)
gf -> 打开文件。例如你在vim中有一行写了#include 那么在abc.h上面按gf,vim就会把abc.h这个文件打开 (look for file. if you have a file with #include , then the cursor is on abc.h press gf, it will open the file abc.h in vim )
记录指令 (record)

q{a-z} -> 在某个寄存中记录指令 (record typed char into register)
q{A-Z} -> 将指令插入之前的寄存器 (append typed char into register{a-z})
q -> 结束记录 (stop recording)
@{a-z} -> 执行寄存中的指令 (execute recording)
@@ -> 重复上次的指令 (repeat previours :@{a-z})
还是给个例子来说明比较容易明白
我现在在一个文件中下qa指令,然后输入itest然后ESC然后q
这里qa就是说把我的指令记录进a寄存,itest实际是分2步,i 是插入 (insert) 写入的文字是 text 然后用ESC退回指令模式q结束记录。这样我就把itest记录再一个寄存了。
下面我执行@a那么就会自动插入test这个词。@@就重复前一个动作,所以还是等于@a
搜索 (search)

vim超级强大的一个功能就是搜索跟替换了。要是熟悉正表达(regular expressions)这个搜索跟后面的替换将会是无敌利器(支持RE的编辑器不多吧)

从简单的说起
# -> 光标下反向搜索关键词 (search the word under cursor backward)
* -> 光标下正向搜索关键词 (search the word under cursor forward)
/ -> 向下搜索 (search forward)
? -> 向上搜索 (search back)
这里可以用 /abc 或 ?abc的方式向上,向下搜索abc
% -> 查找下一个结束,例如在”(“下查找下一个”)”,可以找”()”, “[]” 还有shell中常用的 if, else这些 (find next brace, bracket, comment or #if/#else/#endif)

下面直接用几个例子说话
/a -> 这个会搜到 a aa aaa
/(ab)
-> 这个会搜到 ab abab ababab
/ab+ -> 这个会搜到 ab abb abbb
/folers\= -> 这个会搜到 folder folders
/ab{3,5} -> 这个会搜到 abbb abbbb abbbbb
/ab{-1,3} -> 这个会在abbb中搜到ab (will match ab in abbb)
/a.{-}b -> 这个会在axbxb中搜到axb (match ‘axb’ in ‘axbxb’)
/a.b -> 会搜索到任何a开头后面有b的 (match ab any)
/foo|bar -> 搜索foo或者bar,就是同时搜索2个词 (match ‘foo’ or ‘bar’)
/one|two|three -> 搜索3个词 (match ‘one’, ‘two’ or ‘three’)
/(foo|bar)+ -> 搜索foo, foobar, foofoo, barfoobar等等 (match ‘foo’, ‘foobar’, ‘foofoo’, ‘barfoobar’ … )
/end(if|while|for) -> 搜索endif, endwhile endfor (match ‘endif’, ‘endwhile’, ‘endfor’)
/forever\&… -> 这个会在forever中搜索到”for”但是不会在fortuin中搜索到”for” 因为我们这里给了&…的限制 (match ‘for’ in ‘forever’ will not match ‘fortuin’)

特殊字符前面加^就可以 (for special character, user “^” at the start of range)
/”"*”
这里解释一下
” 双引号先引起来 (double quote)
" 任何不是双引号的东西(any character that is not a double quote)

  • 所有的其他 (as many as possible)
    ” 结束最前面的引号 (double quote close)
    上面那个会搜到“foo” “3!x”这样的包括引号 (match “foo” -> and “3!x” include double quote)

更多例子,例如搜索车牌规则,假设车牌是 “1MGU103” 也就是说,第一个是数字,3个大写字幕,3个数字的格式。那么我们可以直接搜索所有符合这个规则的字符
(A sample license plate number is “1MGU103″. It has one digit, three upper case
letters and three digits. Directly putting this into a search pattern)
这个应该很好懂,我们搜索
\数字\大写字母\大写字母\大写字母\数字\数字\数字

/\d\u\u\u\d\d\d

另外一个方法,是直接定义几位数字(不然要是30位,难道打30个\u去?)
(specify there are three digits and letters with a count)

/\d\u{3}\d{3}

也可以用范围来搜索 (Using [] ranges)
/[0-9][A-Z]{3}[0-9]{3}

用到范围搜索,列出一些范围(range)
这个没什么好说了,看一下就都明白了,要全部记住。。。用的多了就记住了,用的少了就忘记了。每次看帮助,呵呵

/[a-z]
/[0123456789abcdef] = /[0-9a-f]
\e
\t
\r
\b
简写 (item matches equivalent)

\d digit [0-9]
\D non-digit 0-9
\x hex digit [0-9a-fA-F]
\X non-hex digit 0-9a-fA-F
\s white space [ ] ( and )
\S non-white characters (not and )
\l lowercase alpha [a-z]
\L non-lowercase alpha a-z
\u uppercase alpha [A-Z]
\U non-uppercase alpha A-Z

:help /[] –> 特殊的定义的,可以在vim中用用help来看 (everything about special)
:help /\s –> 普通的也可以直接看一下 (everything about normal)
替换 (string substitute) – RX

替换其实跟搜索是一样的。只不过替换是2个值,一个是你搜索的东西,一个是搜索到之后要替换的 string substitute (use rx)

%s/abc/def/ -> 替换abc到def (substitute abc to def)
%s/abc/def/c -> 替换abc到def,会每次都问你确定(substitute on all text with confirmation (y,n,a,q,l))
1,5s/abc/def/g -> 只替换第一行到第15行之间的abc到def (substitute abc to def only between line 1 to 5)
54s/abc/def/ -> 只替换第54行的abc到def (only substitute abc to def on line 54)

结合上面的搜索正表达式,这个替换功能。。。就十分只强大。linux中很多地方都是用正表达来做事请的,所以学会了受益无穷。
全局 (global)

这个不知道怎么翻译,反正vim是叫做global,可以对搜索到的东西执行一些vim的命令。我也是2-3个星期前因为读log中一些特殊的东 西,才学会用的。 (find the match pater and execute a command)

global具体自行方法是 g/pattern/command
:g/abc/p -> 查找并显示出只有abc的行 (only print line with “abc” )
:g/abc/d -> 删除所有有abc的行 (delete all line with “abc”)
:v/abc/d -> 这个会把凡是不是行里没有abc的都删掉 (delete all line without “abc”)
信息过滤 (filter)

vim又一强大功能

! -> 用!就是告诉vim,执行过滤流程 (tell vim to performing a filter operation)
!5G -> 从光标下向下5行执行过滤程序 (tell vim to start filter under cursor and go down 5 lines)

正式指令开始,这里用sort来做例子:
!5Gsort -> 从光标下开始执行sort,一共执行5行,就是说我只要sort5行而已 (this will sort the text from cursor line down to 5 lines)
!Gsort -k3 -> 可以直接代sort的参数,我要sort文字中的第三段 (sort to the end of file by column 3)
!! -> 值过滤当前的这行 (filter the current line)

如果觉得!这样的方法5G这样的方法用起来别扭(我是这么觉得),可以用标准的命令模式来做
!其实就是个:.,而已 (to type the command)
:.,start,end!sort 这里定义:.,起始行,结束行!运行指令
:.,$!sort -> 从当前这行一直执行至文件结束 (sort from current line to end)
:.0,$!sort -> 从文件的开始第一个行一直执行到文件结束 (sort from start of file to end)
:.10,15!sort -> 只在文件的第10行到第15行之间执行 (sort between line 10 to 15)

Vim 自动补全插件 YouCompleteMe 安装

需要挂代理,否则安装很慢。。。

设置git代理

1
2
3
4
5
6
git config --global http.proxy 'socks5://127.0.0.1:1080'
git config --global https.proxy 'socks5://127.0.0.1:1080'

git config --global https.proxy http://127.0.0.1:8118
git config --global https.proxy https://127.0.0.1:8118

这里的代理地址根据你自己梯子的设置而定,一般是127.0.0.1.1080
如果之后想取消,可以

1
2
git config --global --unset http.proxy
git config --global --unset https.proxy

安装Vundle —- vim 插件管理器

1
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim

更新 .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
" ***************** 全局设置 ************** "

" 开启文件类型侦测
filetype on
" 根据侦测到的不同类型加载对应的插件
filetype plugin on

" 自动缩进
filetype indent on
" 开启语法高亮功能
syntax enable
" 允许用指定语法高亮配色方案替换默认方案
syntax on
set nocompatible "关闭兼容模式
set incsearch "开启实时搜索功能
set ignorecase "搜索忽略大小写
set wildmenu "vim命令自动补全
set autoread "文件自动更新
set gcr=a:block-blinkon0 "禁止关闭闪烁
set laststatus=2 "总是显示状态栏
set ruler "显示光标位置
set number "显示行号
set cursorline "高亮显示当前行
"set cursorcolumn "高亮显示当前列
set hlsearch "高亮显示搜索结果
" set nowrap "禁止折行
set backspace=2 "回退键生效
"set backspace=indent,eol,start
set novisualbell

" 禁止显示滚动条
set guioptions-=l
set guioptions-=L
set guioptions-=r
set guioptions-=R
" 禁止显示菜单和工具条
set guioptions-=m
set guioptions-=T

" 将制表符扩展为空格
set expandtab
" 设置编辑时制表符占用空格数
set tabstop=4
" 设置格式化时制表符占用空格数
set shiftwidth=4
" 让 vim 把连续数量的空格视为一个制表符
set softtabstop=4
" 基于缩进或语法进行代码折叠
set foldmethod=syntax
" 启动 vim 时关闭折叠代码
set nofoldenable
" 交互式打开shell
set shell=bash\ -i
" *************** 设置全局快捷键 *****************

" 定义快捷键的前缀,即<Leader>
"let mapleader="\<space>"
let mapleader="'"

" 设置快捷键将选中文本块复制至系统剪贴板
vnoremap <Leader>y "+y

" 设置快捷键将系统剪贴板内容粘贴至 vim
nmap <Leader>p "+p

" 定义快捷键关闭当前分割窗口
nmap <Leader>q :q<CR>
" 定义快捷键保存当前窗口内容
nmap <Leader>w :w<CR>

" 定义快捷键保存所有窗口内容并退出 vim
"nmap <Leader>WQ :wa<CR>:q<CR>

" 不做任何保存,直接退出 vim
"nmap <Leader>Q :qa!<CR>

" 依次遍历子窗口
nnoremap nw <C-W><C-W>
" 跳转至右方的窗口
nnoremap <Leader>l <C-W>l

" 跳转至左方的窗口
nnoremap <Leader>h <C-W>h
" 跳转至上方的子窗口
nnoremap <Leader>k <C-W>k

" 跳转至下方的子窗口
nnoremap <Leader>j <C-W>j
" 定义快捷键在结对符之间跳转
nmap <Leader>M %

nnoremap <Leader>g <C-]>
nnoremap <Leader>b <C-t>


" ************** 插件管理与设置 ************ "

" vundle 环境设置
filetype off
set rtp+=~/.vim/bundle/Vundle.vim
" vundle 管理的插件列表必须位于 vundle#begin() 和 vundle#end() 之间
call vundle#begin()
Plugin 'VundleVim/Vundle.vim'
Plugin 'Lokaltog/vim-powerline' "status 美化
Plugin 'octol/vim-cpp-enhanced-highlight' "对c++语法高亮增强
Plugin 'kshenoy/vim-signature' "书签可视化的插件
Plugin 'vim-scripts/BOOKMARKS--Mark-and-Highlight-Full-Lines' "书签行高亮
Plugin 'majutsushi/tagbar' "taglist的增强版,查看标签,依赖于ctags
Plugin 'scrooloose/nerdcommenter' "多行注释,leader键+cc生成, leader+cu删除注释
Plugin 'scrooloose/nerdtree' "文件浏览
Plugin 'Valloric/YouCompleteMe' "自动补全
Plugin 'kien/ctrlp.vim' "搜索历史打开文件,在命令行模式下按ctrl+p触发
Plugin 'vim-scripts/grep.vim' "在命令行模式使用grep命令,:Grep
Plugin 'Lokaltog/vim-easymotion' "快速跳转,按两下leader键和f组合
Plugin 'vim-scripts/ShowTrailingWhitespace.git' "高亮显示行尾的多余空白字符
Plugin 'vim-scripts/indentpython.vim.git'
Plugin 'vim-scripts/Solarized.git' "主题方案
Plugin 'nathanaelkane/vim-indent-guides.git' "缩进对齐显示
"Plugin 'vim-scripts/indexer.tar.gz' "自动生成标签
"Plugin 'vim-scripts/DfrankUtil' "indexer 依赖
"Plugin 'vim-scripts/vimprj' "indexer 依赖
Plugin 'davidhalter/jedi-vim' "python 补全,不依赖于tags,但比较慢,可以使用indexer替换,但不能跳转项目外
Plugin 'vim-scripts/Markdown'
Plugin 'tpope/vim-surround'
Plugin 'ekalinin/Dockerfile.vim'
" 插件列表结束
call vundle#end()
filetype on

" Powerline 设置
" 设置状态栏主题风格
let g:Powerline_colorscheme='solarized256'



" 设置tagbar
" 设置 tagbar 子窗口的位置出现在主编辑区的左边
let tagbar_left=1
" " 设置显示/隐藏标签列表子窗口的快捷键。速记:identifier list by tag
nnoremap <Leader>t :TagbarToggle<CR>
" " 设置标签子窗口的宽度
let tagbar_width=32
" " tagbar 子窗口中不显示冗余帮助信息
let g:tagbar_compact=1
" " 设置 ctags 对哪些代码标识符生成标签

" signature设置
let g:SignatureMap = {
\ 'Leader' : "m",
\ 'PlaceNextMark' : "m,",
\ 'ToggleMarkAtLine' : "m.",
\ 'PurgeMarksAtLine' : "m-",
\ 'DeleteMark' : "dm",
\ 'PurgeMarks' : "mda",
\ 'PurgeMarkers' : "m<BS>",
\ 'GotoNextLineAlpha' : "']",
\ 'GotoPrevLineAlpha' : "'[",
\ 'GotoNextSpotAlpha' : "`]",
\ 'GotoPrevSpotAlpha' : "`[",
\ 'GotoNextLineByPos' : "]'",
\ 'GotoPrevLineByPos' : "['",
\ 'GotoNextSpotByPos' : "mn",
\ 'GotoPrevSpotByPos' : "mp",
\ 'GotoNextMarker' : "[+",
\ 'GotoPrevMarker' : "[-",
\ 'GotoNextMarkerAny' : "]=",
\ 'GotoPrevMarkerAny' : "[=",
\ 'ListLocalMarks' : "ms",
\ 'ListLocalMarkers' : "m?"
\ }

" 使用 NERDTree 插件查看工程文件。设置快捷键,速记:file list
nmap <Leader>f :NERDTreeToggle<CR>
" 设置NERDTree子窗口宽度
let NERDTreeWinSize=32
" 设置NERDTree子窗口位置
let NERDTreeWinPos="right"
" 显示隐藏文件
let NERDTreeShowHidden=1
" NERDTree 子窗口中不显示冗余帮助信息
let NERDTreeMinimalUI=1
" 删除文件时自动删除文件对应 buffer
let NERDTreeAutoDeleteBuffer=1

" YCM 补全菜单配色
" 菜单
"highlight Pmenu ctermfg=2 ctermbg=3 guifg=#005f87 guibg=#EEE8D5
" 选中项
"highlight PmenuSel ctermfg=2 ctermbg=3 guifg=#AFD700 guibg=#106900
" 补全功能在注释中同样有效
let g:ycm_complete_in_comments=1
" 允许 vim 加载 .ycm_extra_conf.py 文件,不再提示
let g:ycm_confirm_extra_conf=0
" 开启 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_min_num_of_chars_for_completion=1
" 禁止缓存匹配项,每次都重新生成匹配项
let g:ycm_cache_omnifunc=0
" 语法关键字补全
let g:ycm_seed_identifiers_with_syntax=1
""" color settings
set t_Co=256
if has("gui_running")
set guioptions=gR
set mousemodel=popup
set background=light
""hi LineNr cterm=bold guibg=black guifg=white
""hi CursorLine cterm=none ctermbg=lightgray ctermfg=none
""hi CursorColumn cterm=none ctermbg=lightgray ctermfg=none
else
set background=dark
""hi LineNr cterm=bold ctermbg=black ctermfg=white
""hi CursorLine cterm=none ctermbg=darkgray ctermfg=none
""hi CursorColumn cterm=none ctermbg=darkgray ctermfg=none
endif
colorscheme solarized
"colorscheme default
" indent guides
"let g:indent_guides_enable_on_vim_startup=1
" 从第二层开始可视化显示缩进
let g:indent_guides_start_level=2
"let g:indent_guides_auto_colors = 0
"hi IndentGuidesOdd guibg=red ctermbg=3
"hi IndentGuidesEven guibg=green ctermbg=4
" 色块宽度
let g:indent_guides_guide_size=1
" 快捷键 i 开/关缩进可视化
noremap <Leader>sj :IndentGuidesToggle<CR>

安装插件

打开 vim,输入

1
:PluginInstall

回车开始安装,安装完成之后显示The ycmd server SHUT DOWN (restart with ':YcmRestartServer'). YCM core library not detected; you need to compile YCM before using it. Follow the instructions in the documentation.

提示我们还要编译 YCM

编译YCM

需要cmake,clang, python3环境

1
2
3
4
5
sudo apt install cmake
sudo apt install clang
sudo apt install python3-dev
cd ~/.vim/bundle/YouCompleteMe
python3 install.py --clang-completer --system-libclang

安装缺失的python模块

随便用 vim 打开一个 .c 文件,提示找不到python模块,缺啥安装啥就好了

1
pip3 install xxx

设置语义补全

参考
https://zhuanlan.zhihu.com/p/33046090

在 .vimrc 中编辑,指定 ycm_extra_conf.py 的位置并设置自动语义补全

1
2
3
4
5
6
7

let g:ycm_global_ycm_extra_conf = '/home/qinjin/.vim/bundle/YouCompleteMe/third_party/ycmd/examples/.ycm_extra_conf.py'

let g:ycm_semantic_triggers = {
\ 'c,cpp,python,java,go,erlang,perl': ['re!\w{2}'],
\ 'cs,lua,javascript': ['re!\w{2}'],
\ }

最终效果


最近不知道为什么坏了,无法语义补全,于是重装了,就好了。。。。

1
2
cd ~/.vim/bundle/YouCompleteMe
python3 install.py --all

有时间再搞明白为什么把。。。

vim 8.2 源代码编译安装 以便可以使用 “+y 命令复制到系统剪切板

转:

https://www.fpira.com/blog/2020/11/install-vim-from-source/

I’ve removed +python (python2 support) from the ./configure script because it looks like you can’t use both versions 2 and 3 in the same instance. Also, as Ubuntu 20.04 comes with Python 3.8.5, I’m now linking against it and tell the script to use python3 (because in Ubuntu 20.04 system python is 2.7).

Hi everyone, while customizing my .vimrc (here), I found out Ubuntu 20.04 version of vim is a bit old and doesn’t come with +python3 support. So I wrote a quick snippet to build vim 8.2 from source with it, and a couple of features more.

Just check the images below for a quick comparison.

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
#!/bin/bash

# Remove old vim installs
sudo apt-get remove vim vim-runtime gvim vim-tiny \
vim-common vim-gui-common vim-nox gvim

# install vim-gtk deps
sudo apt-get build-dep vim-gtk

# install some more deps
sudo apt-get install build-essential \
libncurses5-dev libgtk-3-dev libatk1.0-dev \
libcairo2-dev libx11-dev libxpm-dev libxt-dev python2.7-dev \
python3-dev ruby-dev libperl-dev \
lua5.2 liblua5.2-0 liblua5.2-dev

# NOTE: if you don't have it already installed, get latest git
#sudo add-apt-repository ppa:git-core/ppa
#sudo apt-get update
#sudo apt-get install git

cd && \
mkdir vimtemp && cd vimtemp && \
git clone https://github.com/vim/vim.git && \
cd vim

make clean
make distclean

./configure \
--with-features=huge \
--enable-cscope \
--enable-multibyte \
--enable-perlinterp=dynamic \
--enable-rubyinterp=dynamic \
--enable-luainterp=dynamic \
--enable-python3interp \
--with-python3-config-dir=/usr/lib/python3.8/config-3.8-x86_64-linux-gnu \
--with-python3-command=python3 \
--enable-gui=auto \
--enable-gtk3-check \
--enable-gtk2-check \
--enable-gnome-check \
--with-x \
--disable-netbeans \
--with-compiledby="pirafrank <dev@fpira.com>" \
--enable-largefile \
--prefix=/usr/local \
--enable-terminal \
--enable-fontset \
--enable-fail-if-missing

make -j4 VIMRUNTIMEDIR=/usr/local/share/vim/vim82
sudo make install

sudo update-alternatives --install /usr/bin/editor editor /usr/local/bin/vim 1
sudo update-alternatives --set editor /usr/bin/vim editor /usr/local/bin/vim
update-alternatives --list editor # if you want to check your alternatives
sudo update-alternatives --install /usr/bin/vi vi /usr/local/bin/vim 1
sudo update-alternatives --set vi /usr/local/bin/vim
update-alternatives --list vi # if you want to check your alternatives
sudo update-alternatives --install /usr/bin/vim vim /usr/local/bin/vim 1
sudo update-alternatives --set vi /usr/local/bin/vim
update-alternatives --list vim # if you want to check your alternatives

# all done!

Please note that the scripts installs vim to /usr/local/bin and sets alternatives so that you can make it live along other vims, should you want to install other versions.

I hope it helps. Thanks for reading.

sudo make install 后面的这段没明白。。。。
主要就是 用他的 依赖和 configure 配置。。
总之 clipboard 变成了 + 号 可以复制到系统剪切板了。。。

1
2
"+y #复制到系统剪切板 
"+p #从系统剪切板粘贴,当然感觉 ctrl+shift+v 更好用。。。

vim 8.2 具有一些新特性 比如 kill羊游戏还是很好玩的。。。