0%

neovim相关配置

一、前言

neovim是基于vim二次开发的,快捷键配置大部分可以复用vim的,这里就只列举一些插件的配置和处理

一个很不错的nvim的开源配置 https://github.com/ayamir/nvimdots

二、特殊配置

1. 剪贴板

  • 没看到neovim有clipboard特性安装上,就先用系统剪贴板共享的方式
  • 需要安装xclip软件,然后设置set clipboard+=unnamedplus就可以直接共享nvim的剪贴板和系统剪贴板

2. 禁用鼠标

  • set mouse=就可以禁用鼠标

三、插件安装

1. packer.nvim 管理插件的插件

1.1. 安装

1) linux

  • linux上直接执行下面的语句
1
2
git clone --depth 1 https://github.com/wbthomason/packer.nvim\
~/.local/share/nvim/site/pack/packer/start/packer.nvim
  • 然后建立目录~/.config/nvim/lua
  • 在里面添加文件plugins.lua,添加自己需要的插件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
vim.cmd [[packadd packer.nvim]]

return require('packer').startup(function(use)
-- packer可以自己管理自己的版本
use 'wbthomason/packer.nvim'

use 'akinsho/toggleterm.nvim'
use 'neoclide/coc.nvim'
use 'scrooloose/nerdcommenter'
use 'preservim/nerdtree'
use 'Xuyuanp/nerdtree-git-plugin'
use {
'nvim-telescope/telescope.nvim',
requires = { {'nvim-lua/plenary.nvim'} }
}
use 'vim-airline/vim-airline'
use 'vim-airline/vim-airline-themes'
use 'bronson/vim-trailing-whitespace'
use 'jiangmiao/auto-pairs'
end)
  • 编辑~/.config/nvim/init.vim
1
2
" 加载lua/plugins.lua文件
lua require('plugins')
  • 重新打开nvim
  • 执行:PackerInstall

2) windows

  • windows自动加载目录在%LocalAppData%\nvim-data\site\pack\packer\start
  • 配置在%LocalAppData%\nvim\init.vim

1.2. 几个常用的命令

  • PackerSync: 跟最新的git仓库同步,类似与update

2. 离线插件安装

  • 插件默认加载位置在~/.local/share/nvim/site/pack/plugins/start
  • 直接解压到此目录就会在启动时自动加载

四、好用插件整理

1. nvim-telescope/telescope.nvim 文件搜索打开插件

  • 需要依赖nvim-lua/plenary.nvim

1.1. 配置说明

1
2
3
4
5
6
7
8
9
10
11
12
13
" Telescope
" 'Ctrl + p' to find file and open in current tab
if filereadable(fnamemodify('.git/config', ':p'))
nmap <c-p> :Telescope git_files<cr>
else
nmap <c-p> :Telescope find_files<cr>
endif
" 'Ctrl + f' to find line in current buffer
map <c-f> :Telescope current_buffer_fuzzy_find<cr>
" 'Ctrl + t' to find tags in current buffer
map <c-t> :Telescope tags<cr>
" 'Ctrl + b' to find buffers
map <leader>b :Telescope buffers<cr>

2. neoclide/coc.nvim 代码补全提示插件

2.1. 安装

  • 插件装好后需要在目录下执行
1
2
yarn install
yarn build
  • 本身此插件有一个自己的插件管理,需要安装对应的lsp
  • 可以通过coc-marketplace包进行类似查找的方式安装
  • 安装插件的命令如下
1
:CocInstall coc-marketplace

离线安装

  • 离线配置yarn和npm的镜像,然后再进行执行
  • yarn镜像配置
1
yarn config set registry https://xxx
  • npm镜像配置,coc.nvim:registry的镜像和yarn是一样的
1
npm config set coc.nvim:registry https://xxx

2.2. 配置和快捷键

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
" coc
inoremap <expr> <Tab> pumvisible() ? "\<C-y>" : "\<Tab>"
inoremap <silent><expr> <CR> coc#pum#visible() ? coc#pum#confirm()
\: "\<C-g>u\<CR>\<c-r>=coc#on_enter()\<CR>"
nmap <silent> gd <Plug>(coc-definition)
nmap <silent> gr <Plug>(coc-references)
" Use K to show documentation in preview window
nnoremap <silent> K :call ShowDocumentation()<CR>
function! ShowDocumentation()
if CocAction('hasProvider', 'hover')
call CocActionAsync('doHover')
else
call feedkeys('K', 'in')
endif
endfunction
" Formatting selected code
xmap <c-k><c-f> <Plug>(coc-format-selected)
nmap <M-F> :CocCommand editor.action.formatDocument<cr>
" go to diagnostic
nmap <silent> <leader>dp <Plug>(coc-diagnostic-prev)
nmap <silent> <leader>dn <Plug>(coc-diagnostic-next)
" Highlight the symbol and its references when holding the cursor
autocmd CursorHold * silent call CocActionAsync('highlight')
  • Ctrl + n: 下一个候选
  • Ctrl + p: 上一个候选

2.3. 使用marketplace

  • 直接输入:CocList marketplace就可以输入选择自己想要安装的插件

2.4. 列举几个好用的插件

coc-clangd c/c++/oc的代码提示lsp

  • 需要本地有clangd命令

coc-cmake cmake代码提示lsp

coc-prettier 格式化插件

2.5. coc-settings.json配置

1
2
3
4
5
6
7
8
9
10
11
{
"languageserver": {
// golang的配置
"golang": {
"command": "gopls",
"rootPatterns": ["go.mod"],
"filetypes": ["go"]
}
},
"prettier.tabWidth": 4 // prettier的tab宽度为4
}

3. akinsho/toggleterm.nvim

3.1. 安装

  • packer安装,里面的setup是必须的
1
2
3
use {"akinsho/toggleterm.nvim", tag = '*', config = function()
require("toggleterm").setup()
end}

3.2. 配置

1
2
3
" Toggleterm
" use ctrl + t to open a terminal at bottom
map <c-t> :ToggleTerm<cr>

3.3. 快捷键

  • 在终端内部需要用Ctrl + \,Ctrl + n来回到窗口模式,然后就按照窗口处理

4. lewis6991/gitsigns.nvim git信息显示

4.1. 配置

  • 在当前行显示git blame,显示格式配置
1
2
" gitsigns
lua require('gitsigns').setup { current_line_blame = true, current_line_blame_formatter = '<author> [<author_time:%Y-%m-%d>] * <summary>' }

5. nvim-tree/nvim-tree.lua 文件树

5.1. 安装

  • nvim-tree安装很简单,主要是想要图标需要做点工作
  • nvim-tree/nvim-web-devicons是图标,但是需要安装nerd font
  • 去下载 https://www.nerdfonts.com/font-downloads,找一下自己字体对应的nerd font
  • 安装到系统后,终端的需要调整终端的字体为这个才能显示图标

5.2. 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
" nvim-tree
lua require("nvim-tree").setup({
\ sort_by = "case_sensitive",
\ })
" set F10 to show or hide NERDTree
func TreeToggle()
if !filereadable(expand("%"))
NvimTreeToggle
return
endif
NvimTreeFindFileToggle
endfunc
map <F10> :call TreeToggle()<cr>

5.3. 快捷键

  • ctrl + ]: 设定光标下为当前目录
  • ctrl + v: 横向分屏打开
  • ctrl + x: 纵向分屏打开
  • ctrl + t: 新标签页打开
  • shift + I: 显示/隐藏非git跟踪的文件
  • shift + H: 显示/隐藏隐藏文件
  • a: 创建
  • d: 删除
  • c: 拷贝节点
  • p: 粘贴
  • shift + P: 跳转到当前节点的父节点
  • r: rename此节点
  • shift + R: 刷新此节点
  • g, y: 拷贝绝对路径到剪贴板
  • y: 拷贝文件名到系统剪贴板
  • shift + Y: 拷贝相对路径到剪贴板

6. ray-x/go.nvim golang的好用的插件

  • 配置
1
2
" ray-x/go.nvim
lua require('go-nvim')
1
2
3
4
5
6
7
8
9
10
11
12
13
-- 初始化go.nvim的插件
require('go').setup()

-- 配置保存自动格式化
local format_sync_grp = vim.api.nvim_create_augroup("GoImport", {})
vim.api.nvim_create_autocmd("BufWritePre", {
pattern = "*.go",
callback = function()
-- 使用gofmt + goimport一起格式化
require('go.format').goimport()
end,
group = format_sync_grp,
})