跳转至

NeoVim安装与配置

概要: 在本文中,以 Ubuntu 22.04 (Docker) 平台为例,介绍NeoVim的初始化安装与配置,提供智能化的Python,Shell和Go语言编辑环境。

创建时间: 2022.09.24 22:23:32

更新时间: 2023.08.16 22:26:32

系统准备

本次NeoVim在Ubuntu系统上演示,首先启动Docker形式的Ubuntu系统,进入交互式终端

Bash
docker run -it --rm --hostname ubuntu2204 --name ubuntu-nvim ubuntu:22.04
确认Ubuntu版本
YAML
cat /etc/lsb-release
image.png

安装NeoVim

如果由于网络原因无法正常安装Ubuntu软件包,可以使用 Ubuntu中科大源 进行加速,此外我们还需要安装一些软件包来保证配置过程顺利

Bash
1
2
3
4
5
6
# replace Ubuntu mirror with USTC source
sed -i 's/archive.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list
# NeoVim
apt update && apt install neovim -y
# other packages
apt install curl wget git iputils-ping xz-utils -y
查看 NeoVim 版本信息
Bash
nvim --version
image.png

安装可选软件

安装Python

Ubuntu已经自带Python3.10,但不包含 pip 和 venv 软件包,如果启用coc.nvim 的Python功能增强,需要额外安装

Bash
apt install python3-pip python3.10-venv -y
image.png

安装Go

测试go语言环境,Ubuntu下使用此命令安装

Bash
apt install golang-go -y
image.png

安装NodeJS

由于coc.nvim使用NodeJS驱动,所以如果使用coc功能,必须安装NodeJS。此处使用了稳定版 nodejs v16.17.0, npm v8.15.0,采用直接从 NodeJS官网下载 的方式安装

Bash
# makesure no nodejs in your ubuntu
apt remove nodejs -y
# download
curl -fo ~/tmp/nodejs.tar.xz --create-dirs https://nodejs.org/dist/v16.17.0/node-v16.17.0-linux-x64.tar.xz
# unachieve
tar -xJf ~/tmp/nodejs.tar.xz -C /opt
# symble link
ln -s /opt/node-v16.17.0-linux-x64/bin/node /usr/bin/node
ln -s /opt/node-v16.17.0-linux-x64/bin/npm /usr/bin/npm
ln -s /opt/node-v16.17.0-linux-x64/bin/npx /usr/bin/npx
# show version
echo "node version: $(node --version)"
echo "npm version: $(npm --version)"
echo "npx version: $(npx --version)"
image.png

安装插件

安装Vim-Plug

Bash
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'

创建init.vim

此步骤仅用于安装必要的插件,配置将在稍后步骤进行

Bash
rm -rf ~/.config/nvim/init.vim
curl -fLo ~/.config/nvim/init.vim --create-dirs https://raw.githubusercontent.com/lz-wang/PublicFiles/main/NeoVim/just_plug/init.vim

安装插件

键入 nvim 进入 NeoVim 主界面,然后在普通模式下执行

Bash
:PlugInstall
如果需要更新插件,执行
Bash
:PlugUpdate
如果需要清理配置中不存在的插件,执行
Bash
:PlugClean
如果需要更新vim-plug自身,执行
Bash
:PlugUpgrade
更多的指令参考 junegunn/vim-plug: Minimalist Vim Plugin Manager

刷新插件配置

Bash
rm -rf ~/.config/nvim/init.vim
curl -fLo ~/.config/nvim/init.vim --create-dirs https://raw.githubusercontent.com/lz-wang/PublicFiles/main/NeoVim/init.vim

配置coc.nvim

注意:以下命令均在 NeoVim 普通模式下执行

安装工具

为NeoVim提供额外的功能支持

Bash
:CocInstall coc-marketplace coc-highlight coc-ci coc-lists coc-pairs coc-git coc-spell-checker coc-gitignore coc-format-json

安装LSP

为不同的编程语言提供代码智能提示和补全支持

Bash
:CocInstall coc-sh coc-python coc-pyright coc-pydocstring coc-go coc-yaml

COC配置

这里简要说明几款工具的使用方式,所有可用的coc插件参考 keywords:coc.nvim - npm search ,或者 Using coc extensions · neoclide/coc.nvim Wiki 页面

coc配置文件

文件位置 ~/.config/nvim/coc-settings.json,或者执行

Bash
:CocConfig
查看 coc.nvim 信息
Bash
:CocInfo
使用如下命令检查 NeoVim 健康状态
Bash
:checkhealth
其中关于coc的部分如下
image.png
卸载插件命令
Bash
:CocUninstall coc-json

coc-ci

提供中文分词的 w/b 移动功能,插件主页 coc-ci - npm

coc-lists

提供查看可用的coc命令列表功能

Bash
:CocList commands
image.png
在模糊匹配后,选中命令项,按下 TAB ,可以直接执行coc插件命令
image.png

coc-marketplace

提供管理coc插件功能

Bash
:CocList marketplace
等待加载完毕后,按 TAB 即可对选中的coc插件进行 安装/卸载
image.png
或者根据关键字直接进行检索
Bash
:CocList marketplace py
image.png
详细请参考 coc-marketplace - npm

coc-format-json

格式化JSON文本的工具,在普通模式下输入

Bash
:CocCommand formatJson --indent=4
详细请参考 coc-format-json - npm
before
image.png
after
image.png

参考

配置资料

Git仓库/软件包