Neovim开发环境搭建(2021.07.01)

一、搭建环境

  • Ubuntu 21.04
  • Neovim 0.4.4
下载 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 插件管理工具,通过它我们可以非常方便的安装插件。

使用 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
使用 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'

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

示例:

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

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

" 结束
call plug#end()

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

四、开发环境搭建

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

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

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

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

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 ' to make sure tab is not mapped by
" other plugin before putting this into your config.

inoremap
      \ pumvisible() ? "\" :
      \ check_back_space() ? "\" :
      \ coc#refresh()
inoremap  pumvisible() ? "\" : "\"

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

" Use  to trigger completion.

if has('nvim')
  inoremap   coc#refresh()
else
  inoremap   coc#refresh()
endif

" Make  auto-select the first completion item and notify coc.nvim to
" format on enter,  could be remapped by other vim plugin
inoremap   pumvisible() ? coc#_select_confirm()
                              \: "\u\\=coc#on_enter()\"

" Use [g and ]g to navigate diagnostics
" Use :CocDiagnostics to get all diagnostics of current buffer in location list.

nmap  [g (coc-diagnostic-prev)
nmap  ]g (coc-diagnostic-next)

" GoTo code navigation.

nmap  gd (coc-definition)
nmap  gy (coc-type-definition)
nmap  gi (coc-implementation)
nmap  gr (coc-references)

" Use K to show documentation in preview window.

nnoremap  K :call show_documentation()

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

" Highlight the symbol and its references when holding the cursor.

autocmd CursorHold * silent call CocActionAsync('highlight')

" Symbol renaming.

nmap rn (coc-rename)

" Formatting selected code.

xmap f  (coc-format-selected)
nmap f  (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: aap for current paragraph
xmap a  (coc-codeaction-selected)
nmap a  (coc-codeaction-selected)

" Remap keys for applying codeAction to the current buffer.

nmap ac  (coc-codeaction)
" Apply AutoFix to problem on the current line.

nmap qf  (coc-fix-current)

" Map function and class text objects
" NOTE: Requires 'textDocument.documentSymbol' support from the language server.

xmap if (coc-funcobj-i)
omap if (coc-funcobj-i)
xmap af (coc-funcobj-a)
omap af (coc-funcobj-a)
xmap ic (coc-classobj-i)
omap ic (coc-classobj-i)
xmap ac (coc-classobj-a)
omap ac (coc-classobj-a)

" Remap  and  for scroll float windows/popups.

if has('nvim-0.4.0') || has('patch-8.2.0750')
  nnoremap   coc#float#has_scroll() ? coc#float#scroll(1) : "\"
  nnoremap   coc#float#has_scroll() ? coc#float#scroll(0) : "\"
  inoremap   coc#float#has_scroll() ? "\=coc#float#scroll(1)\" : "\"
  inoremap   coc#float#has_scroll() ? "\=coc#float#scroll(0)\" : "\"
  vnoremap   coc#float#has_scroll() ? coc#float#scroll(1) : "\"
  vnoremap   coc#float#has_scroll() ? coc#float#scroll(0) : "\"
endif

" Use CTRL-S for selections ranges.

" Requires 'textDocument/selectionRange' support of language server.

nmap   (coc-range-select)
xmap   (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', )

" 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  a  :CocList diagnostics
" Manage extensions.

nnoremap  e  :CocList extensions
" Show commands.

nnoremap  c  :CocList commands
" Find symbol of current document.

nnoremap  o  :CocList outline
" Search workspace symbols.

nnoremap  s  :CocList -I symbols
" Do default action for next item.

nnoremap  j  :CocNext
" Do default action for previous item.

nnoremap  k  :CocPrev
" Resume latest coc list.

nnoremap  p  :CocListResume

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

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

注1:当没有发现 jdt.ls 时,该扩展会自动进行下载。
注2:如果 jdt下载较慢,可以手动下载 jdt 并解包到 coc-java的数据文件夹下,可以在 neovim(或者 vim)中通过 :echo coc#util#extension_root().'/coc-java-data/server'命令获取文件路径。

五、其它扩展

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

:CocInstall coc-json

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

:CocInstall coc-sh

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

Plug 'jiangmiao/auto-pairs'

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

Plug 'luochen1990/rainbow'

相关配置:

let g:rainbow_active = 1 " 激活rainbow

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

Plug 'mhinz/vim-startify'

状态栏插件, 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

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

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

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

Plug 'arcticicestudio/nord-vim'

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

Plug 'morhetz/gruvbox'

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

Plug 'Yggdroot/indentLine'

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

Plug 'preservim/nerdcommenter'

相关配置:

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

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

Plug 'godlygeek/tabular'

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

Plug 'airblade/vim-gitgutter'

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

Plug 'sbdchd/neoformat'

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

Plug 'preservim/nerdtree'

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

Plug 'sainnhe/sonokai'

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

Plug 'metakirby5/codi.vim'

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

Plug 'heavenshell/vim-pydocstring'

相关配置:

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

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

Plug 'preservim/tagbar'

相关配置:

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

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

Plug 'puremourning/vimspector'

相关配置:

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

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

Plug 'jayli/vim-easycomplete'

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

Plug 'SirVer/ultisnips'

代码片段, 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

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

Original: https://www.cnblogs.com/xiaoQQya/p/16313814.html
Author: Hit不死的小强
Title: Neovim开发环境搭建(2021.07.01)

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/562914/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球