Neovim 使用经验积累
本文主要记录了 Neovim 的使用经验,包括 Vim 使用基础知识,Neovim 基础使用以及 Neovim 配置 Python 开发环境等。
插件 | 快捷键 | 功能 |
---|---|---|
Comment | gcc | 注释当前行 |
VimTeX | <leader>ll | 编译与停止编译文档 |
copilot.vim | <C-J> | 补全推荐的代码 |
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(保存并退出)。
- CS107 The Vim Editor (stanford.edu)
- The four things you must be able to do in Vim | Enable Sysadmin (redhat.com)
- What are Vim Modes? How to Change Them? (linuxhandbook.com)
- How to use Vim in Linux | TechTarget
- What are the 7 Vim / Vi Modes? (warp.dev)
Vim 移动命令
- 光标移动到行首:0
- 光标移动到行尾:$
Vim 查找与替换命令
- 基本操作命令 注:联想 Y 7000 没有 Home 键和 End 键
命令 | 操作 |
---|---|
J | 合并两行 |
x | 删除光标所在处的内容 |
dw | 删除一个词 |
Vim 命令里 search and replace 内容中有 slash 该怎么办
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 | :echo stdpath('config') # 查看 Nvim 的配置文件目录,也就是 init.vim 的路径 |
1 | -- 初始 init.lua 备份 |
Neovim 配置前准备工作
安装 Nerd Font, Windows Terminal 最佳字体为 CaskaydiaCove NFM
Nerd Fonts - Iconic font aggregator, glyphs/icons collection, & fonts patcher
安装 wget 和 PowerShell 7 Mason Core 插件使用
How to use Wget: Install, Commands and Examples (Mac & Windows) (jcchouinard.com)
安装 Node. Js 和 npm Mason Languages 插件使用
Install Node.js and NPM on Windows 10 or 11 using command line (how2shout.com)
安装 c++ 编译器 Treesitter 插件使用,,
按照官方指引,安装
LLVM-MinGW
随后添加其bin
文件夹到 PATH。安装 ripgrep 和 fd Telescope 插件使用
GitHub - sharkdp/fd: A simple, fast and user-friendly alternative to ‘find’
1
2winget install BurntSushi.ripgrep.MSVC # ripgrep
winget install sharkdp.fd # fd导出全部快捷键到文件,需提前创建文件
1
nvim --headless '+redir! > ~\nvim_keymaps.md' '+silent verbose map' '+redir END' +qa
Neovim 配置主题
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 插件配置,pyright 和ruff-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 功能,所以需要使用 black,black需要通过 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 modern features: treesitter and LSP
- What’s the difference between tree-sitter and LSP[0]? They both seem to provide … | Hacker News
- https://github.com/williamboman/mason-lspconfig.nvim
- Trying to Configure pyls through nvim-lspconfig
- ASDF+Neovim+Pyright
Neovim 在 Windows Terminal 上出现白边
需要调两个控制,一个是 padding,一个是 scrollbarState。
1
Terminal -> Settings -> Defaults(Profiles) -> Appearance(Additional settings) -> Window
但是调整完了之后在底部还是会有一小部分白边残余,这是因为终端的像素数量不是使用字体的整数倍。
Neovim 使用经验积累