Neovim 使用经验积累

本文主要记录了 Neovim 的使用经验,包括 Vim 使用基础知识,Neovim 基础使用以及 Neovim 配置 Python 开发环境等。

插件 快捷键 功能
Comment gcc 注释当前行
VimTeX <leader>ll 编译与停止编译文档
copilot.vim <C-J> 补全推荐的代码

Help - Neovim docs

Vim 使用基础知识

Vim 的几种模式

Mode 说明
Normal Mode 正常模式 使用 Vim 刚打开文件时一般会处于正常模式
Insert Mode 输入模式 在正常模式的情况下,按下 i, I, a, A, o, O 任意一个键就会进入输入模式,输入内容。
Visual Mode 可视模式 在正常模式下按下 V 或者 v 或者 Ctrl + v 进入可视模式,在这个模式里会有点像是用鼠标操作的感觉,有时候会很方便。

有博客说有四个模式,还包括一个命令模式,但是在 VScode Vim 下没有这个模式,在正常模式下即可以直接输入命令。常用的命令有::q(退出)、:q!(强制退出)、:w(保存)、:wq(保存并退出)。

Vim 移动命令

  • 光标移动到行首:0
  • 光标移动到行尾:$

vim-movement-commands

Vim 查找与替换命令

  • 基本操作命令 注:联想 Y 7000 没有 Home 键和 End 键
命令 操作
J 合并两行
x 删除光标所在处的内容
dw 删除一个词

Vim 正则表达式

1
%s/[0-9]\./- /c

vim 查找替换及正则表达式的使用 - Cooper’s Blog (tanqisen.github.io)

应用实例 :替换 Markdown 文件里失效的 Gitee 图床链接

  • 命令 %表示对当前文件进行替换
1
:%s/gitee.com\/xxxxx\/image_bed\/raw\/master\/img/raw.githubusercontent.com\/xxxxx\/image_bed\/main\/img_gitee/gc
  • 查找
1
/\<gitee.com\/latexalpha\/image_bed\/raw\/master\/img\>

Lua 语言基础知识

Everything you need to know to configure neovim using lua

Neovim 基础使用

1
2
3
4
:echo stdpath('config') # 查看 Nvim 的配置文件目录,也就是 init.vim 的路径
:h init.vim # 查看初始配置文件的帮助文档
:h xdg # 查看基本目录 (base-directories) 的帮助文档
vim.keymap.set() # 快捷键映射
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
-- 初始 init.lua 备份
local options = { noremap = true }
vim.keymap.set("i", "jk", "<ESC>", options)
vim.keymap.set("t", "jk", "<C-\\><C-n>", options)

if vim.g.vscode then
-- VSCode extension
else
-- ordinary Neovim
end

local set = vim.opt -- set options
set.tabstop = 4
set.softtabstop = 4
set.shiftwidth = 4

Neovim 配置前准备工作

Neovim 配置主题

ASCII Text Art Generator

Neovim 配置 Markdown 编辑器

虽然有插件,但是需要借助浏览器渲染,使用起来比较麻烦,还是建议直接使用 Obsidian, 功能强大,有插件社区,可扩展性强。

Neovim 配置 Python 开发环境

Neovim Spaghetti - LSP Servers, Linters, Formatters, and Treesitter – roobert

Autocomplete and IntelliSense

IntelliSense is a general term for code editing features that relate to code completion.

Editing Python Code in Visual Studio Code

利用 nvim-cmp 插件,还需要 Language server 提供支持 。

GitHub - hrsh7th/nvim-cmp: A completion plugin for neovim coded in Lua.

Official page for Language Server Protocol (microsoft.github.io)

Linting

Linting highlights syntactical and stylistic problems in your Python source code, which often helps you identify and correct subtle programming errors or unconventional coding practices that can lead to errors.

Linting is distinct from Formatting because linting analyzes how the code runs and detects errors whereas formatting only restructures how code appears.

Linting Python in Visual Studio Code

利用 nvim-lspconfig 插件配置,pyrightruff-lsp提供支持,ruff-lsp 需要提前 pip install ruff-lsp 安装。

Python Formatting

Formatting makes source code easier to read by human beings. By enforcing particular rules and conventions such as line spacing, indents, and spacing around operators, the code becomes more visually organized and comprehensible.

Formatting Python in Visual Studio Code

利用null-ls插件配置,black提供支持。

由于pyright 不提供 formatting 功能,所以需要使用 blackblack需要通过 Mason 插件安装。

How to setup formatter for Python ??? : r/neovim (reddit.com)

formatting - How to configure neovim to properly format python code? - Vi and Vim Stack Exchange

Debugging

Debugging configurations for Python apps in Visual Studio Code

利用 nvim-dap 插件配置,debugpy 提供支持。

Environments

An “environment” in Python is the context in which a Python program runs that consists of an interpreter and any number of installed packages.

Using Python Environments in Visual Studio Code

利用 nvim-dap 插件配置,重点是设置 Python 解释器的路径,这里用 Conda 的优势就很明显了。由于 Conda 运行的时候会设置 CONDA_PREFIX 环境变量,因此可以直接利用环境变量设置 Python 解释器的路径。

Testing

Testing Python in Visual Studio Code

Neovim 配置 LaTeX 编辑器

利用 VimTeX 插件

Real-time LaTeX using Vim/Neovim, VimTeX, and snippets | ejmastnak

  • 反向搜索
1
cmd /c start /min "" nvim --headless -c "VimtexInverseSearch %l '%f'"

LSP

利用 nvim-lspconfig 配置,lua_ls 提供支持。

LaTeX Formatting

利用 null-ls 配置,latexindent 提供支持。

Neovim Snippets 设置

利用 LuaSnip 插件来实现。

LuaSnip Plugin Guide for LaTeX | Vim and LaTeX Series Part 2 | ejmastnak

遇到的问题与解决方案

Treesitter and LSP 的差别

Neovim 在 Windows Terminal 上出现白边

作者

Shangyu ZHAO

发布于

2024-01-11

更新于

2024-04-19

许可协议