neovim使用
2021-10-11 20:53:08 # other

安装NeoVim

1
2
3
4
5
6
7
8
9
sudo apt install python-dev python-pip python3-dev python3-pip
sudo apt install software-properties-common
sudo apt update
sudo add-apt-repository ppa:neovim-ppa/stable
sudo apt install neovim
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt update
sudo apt install yarn

创建配置文件

1
2
3
mkdir -p  ~/.config/nvim
cd ~/.config/nvim
touch init.vim

NeoVim配置

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
" ========================开始基础配置========================
" 显示行号
set number

" TAB键缩进
set tabstop=4
set softtabstop=4
set autoindent

" 禁止备份文件
set nobackup
set nowritebackup
set noswapfile

" 高亮当前行
set cursorline

" << >>移动长度
set shiftwidth=4

" 边输入边搜索
set incsearch

" 高亮搜索
set hlsearch

" 正则表达式不用加转义
set magic

" 允许未保存的修改时切换缓冲区,修改vim自动保存
set hidden

" 新行自动缩进
set smartindent
set backspace=indent,eol,start

" 命令行高度
set cmdheight=2

" 外部修改当前文件自动重载
set autoread

" 文件编码
set encoding=utf-8
set termencoding=utf-8
set fencs=utf-8,gbk

" 关闭vi兼容模式
set nocompatible

" 自动语法高亮
syntax on

" 终端颜色256
set t_Co=256

" 补全提示不自动选择第一个
set completeopt =menu,menuone

" 按键延迟
set updatetime=300
set ttimeoutlen=100

" 状态栏内容设置
set statusline^=%{coc#status()}%{get(b:,'coc_current_function','')}
" ========================结束基础配置========================

" ========================开始插件安装========================
filetype plugin on
filetype off
call plug#begin('~/.config/nvim/plugged')
" 注释
Plug 'tpope/vim-commentary'
" 搜索
Plug 'Yggdroot/LeaderF', { 'do': './install.sh' }
" 格式化代码
Plug 'rhysd/vim-clang-format'
" 彩虹括号
Plug 'luochen1990/rainbow'
" 状态栏
Plug 'vim-airline/vim-airline'
Plug 'vim-airline/vim-airline-themes'
" coc管理器
Plug 'neoclide/coc.nvim', {'branch': 'master', 'do': 'yarn install --frozen-lockfile'}
" C++关键字高亮
Plug 'octol/vim-cpp-enhanced-highlight'
" 目录树
Plug 'scrooloose/nerdtree'
call plug#end()
filetype plugin indent on
" ========================结束插件安装========================

" ========================LeaderF插件设置开始========================
let g:Lf_Gtagslabel = 'native-pygments'
let g:Lf_GtagsAutoGenerate = 1
let g:Lf_ShortcutF = '<C-P>'
let g:Lf_StlSeparator = { 'left': '', 'right': '', 'font': '' }
let g:Lf_RootMarkers = ['.project', '.root', '.svn', '.git']
let g:Lf_WorkingDirectoryMode = 'Ac'
let g:Lf_WindowHeight = 0.30
let g:Lf_ShowRelativePath = 0
let g:Lf_HideHelp = 1
let g:Lf_UseCache = 0
let g:Lf_UseMemoryCache = 0
let g:Lf_UseVersionControlTool = 0
let g:Lf_IgnoreCurrentBufferName = 1
let g:Lf_PreviewResult = {'Function': 0, 'BufTag': 0 }
let g:Lf_JumpToExistingWindow = 0
" let g:Lf_WindowPosition = 'popup'
" let g:Lf_PreviewInPopup = 1

let g:Lf_WildIgnore = {
\'dir': ['build', 'bin'],
\'file': ['*.a', '*.lib', '*.so', '*.pb', '*.out', '*.o', '*.dll', '*.exe']
\}

let g:Lf_RgConfig = [
\ "--max-columns=150",
\ "--glob=!build/*",
\ "--glob=!bin/*",
\ "--glob=!lib/*",
\ "--glob=!cmake/*",
\ ]

noremap <leader>gs :<C-U><C-R>=printf("Leaderf! rg --current-buffer -e %s ", expand("<cword>"))<CR><CR>
noremap <leader>gg :<C-U><C-R>=printf("Leaderf! rg -e %s ", expand("<cword>"))<CR><CR>
xnoremap gv :<C-U><C-R>=printf("Leaderf! rg -F -e %s ", leaderf#Rg#visual())<CR>
noremap gz :Leaderf! rg -F -e "
noremap <F5> :Leaderf gtags --update <CR>
noremap <F6> :Leaderf --recall <CR>

noremap <leader>fb :<C-U><C-R>=printf("Leaderf buffer %s", "")<CR><CR>
noremap <leader>fm :<C-U><C-R>=printf("Leaderf mru %s", "")<CR><CR>
noremap <leader>ft :<C-U><C-R>=printf("Leaderf bufTag %s", "")<CR><CR>
" ========================LeaderF插件设置结束========================

" ========================ClangFormat插件设置开始========================
let g:clang_format#command = 'clang-format'
autocmd FileType c,cpp ClangFormatAutoEnable
" ========================ClangFormat插件设置开始========================

" ========================Rainbow插件设置开始========================
let g:rainbow_active = 1
" ========================Rainbow插件设置开始========================

" ========================Airline插件设置开始========================
let g:airline_powerline_fonts = 1
let g:airline#extensions#tabline#enabled = 1
let g:airline#extensions#tabline#left_sep = ''
let g:airline#extensions#tabline#left_alt_sep = ''
let g:airline#extensions#tabline#formatter = 'unique_tail'
let g:airline_theme='onedark'
" ========================Airline插件设置结束========================

" ========================cpphighlight插件设置开始========================
let g:cpp_class_scope_highlight = 1
let g:cpp_member_variable_highlight = 1
let g:cpp_posix_standard = 1
let g:cpp_concepts_highlight = 1
" ========================cpphighlight插件设置结束========================

" ========================coc-nvim插件设置开始========================
" 符号跳转
nmap <leader>gd <Plug>(coc-definition)
nmap <leader>gy <Plug>(coc-type-definition)
nmap <leader>gi <Plug>(coc-implementation)
nmap <leader>gr <Plug>(coc-references)

" 符号重命名
nmap <leader>rn <Plug>(coc-rename)

" 符号信息查看
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

" 多个诊断问题切换
nmap <silent> [g <Plug>(coc-diagnostic-prev)
nmap <silent> ]g <Plug>(coc-diagnostic-next)

inoremap <silent><expr> <TAB>
\ coc#pum#visible() ? coc#pum#next(1) :
\ CheckBackspace() ? "\<Tab>" :
\ coc#refresh()
inoremap <expr><S-TAB> coc#pum#visible() ? coc#pum#prev(1) : "\<C-h>"

" 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>"

function! CheckBackspace() abort
let col = col('.') - 1
return !col || getline('.')[col - 1] =~# '\s'
endfunction

" Make <CR> auto-select the first completion item and notify coc.nvim to
" format on enter, <cr> could be remapped by other vim plugin
inoremap <silent><expr> <cr> pumvisible() ? coc#_select_confirm()
\: "\<C-g>u\<CR>\<c-r>=coc#on_enter()\<CR>"
" ========================coc-nvim插件设置结束========================

" ========================NERDTree插件设置开始========================
" nerdtree
map <leader>t :NERDTreeToggle<CR>
let NERDTreeWinSize = 38
let g:NERDTreeHidden = 0
let NERDTreeMinimalUI = 1
let g:NERDTreeDirArrowExpandable = '+'
let g:NERDTreeDirArrowCollapsible = '-'
let NERDTreeIgnore = [
\ '\.pyc$','\.out','\.a','\.so','\.exe','\.dll','build',
\ ]
" ========================NERDTree插件设置结束========================

" ========================其他快捷键设置开始========================
" Buffer页签切换
nnoremap <leader>[ :bp<CR>
nnoremap <leader>] :bn<CR>

" Buffer页签关闭
nmap <silent><leader>c :bd<CR>
" ========================其他快捷键设置结束========================

安装Vim-Plug

1
2
3
4
sh -c 'curl -fLo "${XDG_DATA_HOME:-$HOME/.local/share}"/nvim/site/autoload/plug.vim --create-dirs \
https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim'
nvim
:PlugInstall