其他分享
首页 > 其他分享> > Neovim开发环境搭建(2021.07.01)

Neovim开发环境搭建(2021.07.01)

作者:互联网

Neovim开发环境搭建(2021.07.01)

一、搭建环境

二、Neovim安装

# 下载 neovim,如遇网络问题可以采用 https://hub.fastgit.org 镜像进行加速下载
# curl -LO https://hub.fastgit.org/neovim/neovim/releases/latest/download/nvim.appimage
$ curl -LO https://github.com/neovim/neovim/releases/latest/download/nvim.appimage

# 为 neovim 添加执行权限
$ chmod +x nvim.appimage

# 将 neovim 移至 /usr/local/bin 目录下,并重命名为 nvim
$ sudo mv nvim.appimage /usr/local/bin/nvim

# 测试 neovim 使用
$ nvim

# 配置 vim 和 vi 使用 nvim(可选)
# 为 vi 和 vim 添加 nvim 可选项
$ sudo update-alternatives --install /usr/bin/vi vi /usr/local/bin/nvim 30
$ sudo update-alternatives --install /usr/bin/vim vim /usr/local/bin/nvim 30
# 配置 vi 和 vim 使用 nvim,选择列表中 nvim 对应的序号即可
$ sudo update-alternatives --config vi
$ sudo update-alternatives --config vim

三、vim-plug安装

vim-plug是一个极简的 vim 插件管理工具,通过它我们可以非常方便的安装插件。

3.1 安装

3.1.1 Vim

# 使用 vim 的安装方式,如遇网络问题可以采用 https://raw.fastgit.org 镜像加速下载
# curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
#     https://raw.fastgit.org/junegunn/vim-plug/master/plug.vim
$ curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
      https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

3.1.2 Neovim

# 使用 neovim 的安装方式,如遇网络问题可以采用 https://raw.fastgit.org 镜像加速下载
# sh -c 'curl -fLo "${XDG_DATA_HOME:-$HOME/.local/share}"/nvim/site/autoload/plug.vim --create-dirs \
#        https://raw.fastgit.org/junegunn/vim-plug/master/plug.vim'
$ 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'

3.3 使用

添加vim-plug的配置到neovim的配置文件~/.config/nvim/init.vim中(vim配置文件路径为~/.vimrc)。

  1. call plug#begin('~/.vim/plugged')开始;
  2. Plug命令列出所有需要安装插件;
  3. call plug#end()结束;

示例:

" ~/.vim/plugged 为插件保存的路径,可自定义
call plug#begin('~/.vim/plugged')

" 用 Plug 命令列出所有需要安装的插件
Plug 'jiangmiao/auto-pairs' " 自动补全括号插件
Plug 'neoclide/coc.nvim', {'branch': 'release'} " lsp语言服务协议

" 结束
call plug#end()

重新加载配置文件通过:PlugInstall命令来安装插件。

四、开发环境搭建

4.1 coc.nvim

4.1.1 安装

coc.nvim扩展使neovim(或者vim)支持各种语言服务协议,从而实现智能提示等功能。

采用vim-plug的方式进行安装,在.vimrc或者init.vim文件中添加以下内容:

Plug 'neoclide/coc.nvim', {'branch': 'release'}

重新加载配置文件通过:PlugInstall命令来安装插件。

注:coc.nvim 需要依赖 node, 关于 node 的安装请自行查阅,此处不再赘述。

4.1.2 配置

coc.nvim的配置较为复杂,此处采用默认配置,如有需求可以自行修改。

.vimrc或者init.vim添加以下配置,以使 coc.nvim更好的工作:

" Set internal encoding of vim, not needed on neovim, since coc.nvim using some
" unicode characters in the file autoload/float.vim
set encoding=utf-8

" TextEdit might fail if hidden is not set.
set hidden

" Some servers have issues with backup files, see #649.
set nobackup
set nowritebackup

" Give more space for displaying messages.
set cmdheight=2

" Having longer updatetime (default is 4000 ms = 4 s) leads to noticeable
" delays and poor user experience.
set updatetime=300

" Don't pass messages to |ins-completion-menu|.
set shortmess+=c

" Always show the signcolumn, otherwise it would shift the text each time
" diagnostics appear/become resolved.
if has("nvim-0.5.0") || has("patch-8.1.1564")
  " Recently vim can merge signcolumn and number column into one
  set signcolumn=number
else
  set signcolumn=yes
endif

" Use tab for trigger completion with characters ahead and navigate.
" NOTE: Use command ':verbose imap <tab>' to make sure tab is not mapped by
" other plugin before putting this into your config.
inoremap <silent><expr> <TAB>
      \ pumvisible() ? "\<C-n>" :
      \ <SID>check_back_space() ? "\<TAB>" :
      \ coc#refresh()
inoremap <expr><S-TAB> pumvisible() ? "\<C-p>" : "\<C-h>"

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

" Use <c-space> to trigger completion.
if has('nvim')
  inoremap <silent><expr> <c-space> coc#refresh()
else
  inoremap <silent><expr> <c-@> coc#refresh()
endif

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

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

" 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 CocAction('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     CocAction('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>

4.2 Python补全支持

coc.pyrightcoc.nvim的一个插件,通过它可以实现python语言的智能提示等。

  1. 通过以下命令安装扩展;

    :CocInstall coc-pyright
    
  2. 配置python开发环境(如果已经配置跳过此步);

  3. 打开.py文件即可自动识别实现提示补全等功能;

4.2 Java补全支持

coc-javacoc.nvim的一款插件,通过它可以实现java语言的智能提示等。

  1. 通过以下命令安装扩展;

    :CocInstall coc-java
    
  2. 安装 JDK 并正确配置环境变量(如果已经配置跳过此步);

  3. 打开.java文件将会自动激活;

注1:当没有发现 jdt.ls 时,该扩展会自动进行下载。

注2:如果jdt下载较慢,可以手动下载 jdt 并解包到coc-java的数据文件夹下,可以在neovim(或者vim)中通过:echo coc#util#extension_root().'/coc-java-data/server'命令获取文件路径。

五、其它扩展

5.1 coc.nvim

5.1.1 coc-json

json语言服务,安装命令如下:

:CocInstall coc-json

5.1.2 coc-sh

sh语言服务,安装命令如下:

:CocInstall coc-sh

5.2 auto-pairs

自动插入或者删除另一半括号,vim-plug方式安装:

Plug 'jiangmiao/auto-pairs'

5.3 rawbow

嵌套彩虹括号,vim-plug方式安装:

Plug 'luochen1990/rainbow'

相关配置:

let g:rainbow_active = 1 " 激活rainbow

5.4 vim-startify

文件打开记录,vim-plug方式安装:

Plug 'mhinz/vim-startify'

5.5 vim-airline

状态栏插件,vim-plug方式安装:

Plug 'vim-airline/vim-airline'

相关配置:

let g:airline_theme = 'nord' " 设置airline使用的主题
let g:airline_powerline_fonts = 1 " 使airline正常显示箭头

" 设置 neovim 或者 vim 的 tabline
let g:airline#extensions#coc#enabled = 1
let g:airline#extensions#tabline#enabled = 1
let g:airline#extensions#tabline#tab_nr_type = 1
let g:airline#extensions#tabline#show_tab_nr = 1
let g:airline#extensions#tabline#formatter = 'default'
let g:airline#extensions#tabline#buffer_nr_show = 0
let g:airline#extensions#tabline#fnametruncate = 16
let g:airline#extensions#tabline#fnamecollapse = 2
let g:airline#extensions#tabline#buffer_idx_mode = 1

5.6 vim-airline-themes

状态栏主题插件,vim-plug方式安装:

Plug 'vim-airline/vim-airline-themes'

5.7 nord-vim

nord配色主题插件,vim-plug方式安装:

Plug 'arcticicestudio/nord-vim'

5.8 gruvbox

gruvbox配色主题插件,vim-plug方式安装:

Plug 'morhetz/gruvbox'

5.9 indentLine

缩进线插件,vim-plug方式安装:

Plug 'Yggdroot/indentLine'

5.10 nerdcommenter

注释插件,支持多语言,vim-plug方式安装:

Plug 'preservim/nerdcommenter'

相关配置:

let g:NERDSpaceDelims = 1 " 设置在注释分隔符后添加空格
let g:NERDCompactSexyComs = 1 " 设置多行使用简洁语法注释
let g:NERDTrimTrailingWhitespace = 1 " 设置允许注释清理行尾的空格

5.11 tabular

文本对齐插件,vim-plug方式安装:

Plug 'godlygeek/tabular'

5.12 vim-gitgutter

git diff插件,vim-plug方式安装:

Plug 'airblade/vim-gitgutter'

5.13 neoformat

代码格式化插件,支持多语言,vim-plug方式安装:

Plug 'sbdchd/neoformat'

5.14 nerdtree

文件目录树插件,vim-plug方式安装:

Plug 'preservim/nerdtree'

5.15 sonokai

代码高亮配色主题,vim-plug方式安装:

Plug 'sainnhe/sonokai'

5.16 codi.vim

实时显示python脚本执行结果,vim-plug方式安装:

Plug 'metakirby5/codi.vim'

5.17 vim-pydocstring

自动生成python文档字符串,vim-plug方式安装:

Plug 'heavenshell/vim-pydocstring'

相关配置:

let g:pydocstring_doq_path = '~/.local/share/virtualenvs/python3.8/bin/doq' " pydocstring依赖doq的路径

5.18 tagbar

显示文件类结构层次,vim-plug方式安装:

Plug 'preservim/tagbar'

相关配置:

let g:tagbar_ctags_bin = '/usr/bin/ctags' " tagbar依赖ctags的路径

5.19 vimspector

类似vscode布局的 GUI 调试扩展,vim-plug方式安装:

Plug 'puremourning/vimspector'

相关配置:

let g:vimspector_enable_mappings = 'VISUAL_STUDIO' " vimspector快捷键方案

5.20 vim-easycomplete

自动补全扩展,功能类似coc.nvimvim-plug方式安装:

Plug 'jayli/vim-easycomplete'

5.21 ultisnips

代码片段,vim-plug方式安装:

Plug 'SirVer/ultisnips'

5.22 vim-snippets

代码片段,vim-plug方式安装:

Plug 'honza/vim-snippets'

六、其它配置

set relativenumber " 设置显示相对行号
set number " 设置显示行号
set expandtab " 设置TAB使用空格
set tabstop=4 " 设置TAB缩进的空格数量
set shiftwidth=4 " 自动缩进的宽度
set fileencodings=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936 " 解决中文乱码
set termencoding=utf-8
set encoding=utf-8

" 插入模式键映射
inoremap kk <Esc>

" 普通模式键映射
" 打开文件树
nnoremap <leader>e :CocCommand explorer<CR>
" 保存
nnoremap <leader>w :w<CR>
" 删除buffer
nnoremap <leader>bd :bd<CR>
" 下一个buffer
nnoremap <leader>bn :bn<CR>
" 上一个buffer
nnoremap <leader>bp :bp<CR>
" 关闭tab
nnoremap <leader>tc :tabclose<CR>

标签:Neovim,plug,01,2021.07,Plug,vim,c#,coc,nvim
来源: https://blog.csdn.net/qq_44797987/article/details/118404656