neovim 配置

今夕 / 2024-10-23 / 原文

install
curl -LO https://github.com/neovim/neovim/releases/latest/download/nvim-linux64.tar.gz # 或直接到 releases 中下载,linux 系统要注意 glibc 版本
sudo rm -rf /opt/nvim
sudo tar -C /opt -xzf nvim-linux64.tar.gz
export PATH="\(PATH:/opt/nvim-linux64/bin" # in .bashrc or .zshrc 安装 node 时需要注意与 glibc 版本对应,node 的安装方法请参考文章末尾的参考资料。另外使用 nvm 管理 node 版本,使用时需通过 nvm use {version_num} 来指定。 \)ldd --version
插件
nvim-tree
nvim-tree/nvim-tree.lua: A file explorer tree for neovim written in lua (github.com)
API:nvim-tree.lua/doc/nvim-tree-lua.txt at master · nvim-tree/nvim-tree.lua (github.com)
使用 :NvimTreeOpen 可以调出文件树窗口,使用 g? 可以展示快捷键帮助
在 lua/keymaps.lua 进行配置,通过 e 可以开启或关闭文件树,通过 t 可以将光标聚焦于文件树。
在 lua/config/nvim-tree.lua 中进行一些常用的快捷键设置
<C-]> 将根目录切换至当前目录
在文件树窗口打开当前文件
展示文件信息
重命名文件
打开一个新的 tab
垂直分割窗口打开文件
水平分割窗口打开文件
关闭文件夹(退格键)
打开文件
预览文件
< 移动至当前文件夹内的上一个文件

  			移动至当前文件夹内的下一个文件

. 执行命令

  •   			将根目录切换至当前文件的父目录
    

a 创建新文件或文件夹
B 切换是否只显示已缓存的文件,默认是 false
c 拷贝一个文件或文件夹
C 切换是否只显示有 git 状态的文件
[c 导航至上一个 item 并显示 git status
]c 导航至上一个 item 并显示 git status
d 从文件系统中删除某个文件或文件夹
D 将文件或文件夹删除至 nvim 垃圾桶
E 展开所有文件夹
e 重命名文件或文件夹的基础名字(忽略扩展名)
[e 导航至上一个有问题的文件
]e 导航至下一个有问题的文件
z
再一次重装 Nvim-Tree 后,lazy setup 时配置了 event = "VimEnter" 选项,导致每次 nvim . 后不会打开目录树。
bufferline
Telescope
链接:https://github.com/nvim-telescope/telescope.nvim

auto-pairs
jiangmiao/auto-pairs: Vim plugin, insert or delete brackets, parens, quotes in pair (github.com)
自动插入、删除括号、引号
Nvim-Treesitter
nvim-treesitter/nvim-treesitter: Nvim Treesitter configurations and abstraction layer (github.com)
require 'nvim-treesitter.install'.compilers = { 'clang++'}
需要注意,使用 TSInstall cpp 时,需要将 compilers 设置为 clang。如果使用 clang++ 编译,则会遇到报错。
使用 :checkhealth nvim-treesitter 检查是否安装成功。
注释
terrortylor/nvim-comment: A comment toggler for Neovim, written in Lua (github.com)
Either use the command CommentToggle, e.g.:
● CommentToggle comment/uncomment current line
● 67,69CommentToggle comment/uncomment a range
● '<,'>CommentToggle comment/uncomment a visual selection
Or use the default mappings:
● gcc comment/uncomment current line, this does not take a count, if you want a count use the gc{count}{motion}
● gc{motion} comment/uncomment selection defined by a motion (as lines are commented, any comment toggling actions will default to a linewise):
○ gcip comment/uncomment a paragraph
○ gc4w comment/uncomment current line
○ gc4j comment/uncomment 4 lines below the current line
○ dic delete comment block
○ gcic uncomment commented block
Which-key
folke/which-key.nvim: 💥 Create key bindings that stick. WhichKey helps you remember your Neovim keymaps, by showing available keybindings in a popup as you type. (github.com)

主题配置
● 主题:trending colorschemes | vimcolorschemes

LSP 配置
lspconfig
● [d 跳至上一个有错误的地方
● ]d 跳至下一个有错误的地方
nvim-cmp:https://github.com/hrsh7th/nvim-cmp
详解nvim内建LSP体系与基于nvim-cmp的代码补全体系-腾讯云开发者社区-腾讯云 (tencent.com)
mason:https://github.com/williamboman/mason-lspconfig.nvim
mason-lspconfig:https://github.com/williamboman/mason-lspconfig.nvim
nvim-lspconfig:https://github.com/neovim/nvim-lspconfig/tree/master
nvim-lspconfig 支持的 LSP:https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md

剪切板配置
安装 xclip:Releases · astrand/xclip (github.com)
./bootstrap
./configure
make
make install
过程中如果报错:configure: error: *** X11/Xmu/Atoms.h is missing ***
则需要安装 libXmu-devel:sudo yum install libXmu-devel
Mac 端安装 How to install XQuartz on macOS for SSH X11 forwarding - nixCraft (cyberciti.biz)
注意要安装 xuath。
然后,需要将服务器的 sshd_config 中 X11Forwarding 打开,然后重启 ssh 服务(很危险,执行之前最好保证配置项正确):
$sudo cat /etc/ssh/sshd_config | grep 'X11'
X11Forwarding yes
X11DisplayOffset 10
X11UseLocalhost yes
然后将客户端的 ssh_config 中 ForwardX11 打开:
ForwardX11 yes # 9.19 add:看起来不加也可以

X11Forwarding yes

XAuthLocation /opt/X11/bin/xauth

接着,修改 nvim 配置:
-- 这里是 vim 配置
let g:clipboard = {
\ 'name': 'xclip',
\ 'copy': {
\ '+': 'xclip -selection clipboard',
\ '': 'xclip -selection clipboard',
\ },
\ 'paste': {
\ '+': 'xclip -selection clipboard -o',
\ '
': 'xclip -selection clipboard -o',
\ },
\ }
-- 这里是 lua 配置脚本
vim.g.clipboard = {
name = 'xclip',
copy = {
['+'] = 'xclip -selection clipboard',
[''] = 'xclip -selection clipboard',
},
paste = {
['+'] = 'xclip -selection clipboard -o',
['
'] = 'xclip -selection clipboard -o',
},
}
启动 mac 上的 Xquartz,并保证其设置 > 安全性 > 允许从网络客户端链接设置为 true。
安装成功后,控制台查看 DISPLAY 环境变量,可以正常输出。
[hanyaodong.hyd@obvos-dev-g3 /home/hanyaodong.hyd]
$echo $DISPLAY
localhost:12.0
终端
:terminal
:vsplit | terminal
:split | terminal
退出 terminal 的 insert 模式:ctrl+\ 再按下 ctrl + n 即可退出。因为 esc 在终端的行为和文本编辑模式中有所不同,esc 可能会被视为发送字符给终端,而不是用于切换模式。
终端插件:akinsho/toggleterm.nvim: A neovim lua plugin to help easily manage multiple terminal windows (github.com)
使用方法
ToggleTerm size=40 dir=~/.config/nvim direction=horizontal name=desktop
ToggleTermToggleAll 打开之前所有的 term
TermExec

参考资料
● Lazy 文档:https://lazy.folke.io/usage
● Lua 语法:https://learnxinyminutes.com/docs/lua/
● 推荐的配置:https://github.com/ayamir/nvimdots
● 插件配置教程:neovim入门指南(二):常用插件 - youngxhui - 博客园 (cnblogs.com)
● SmithJson/nvim: IDE VIM Configuration (github.com)
● https://martinlwx.github.io/zh-cn/config-neovim-from-scratch
● https://zhuanlan.zhihu.com/p/382092667
● neovim doc:https://neovim.io/doc/
● Node:https://nodejs.org/en/download/package-manager