Skip to main content

4. Neovim

历史

Neovim is a fork of the Vim editor. It was born in 2014, mainly due to the lack at the time of asynchronous job support in Vim. Written in the Lua language with the goal of modularizing the code to make it more manageable, Neovim was designed with the modern user in mind. As the official website states

Neovim is built for users who want the best parts of Vim, and more.

管理器比较

这里的比较很全面:Nvchad vs lazyvim

对于初学者来说,layz.nvim,Nvchad 都太复杂了,建议从 nvim-lua / kickstart.nvim开始。

安装

在 Linux 上安装最新版 Neovim
curl -LO https://github.com/neovim/neovim/releases/latest/download/nvim-linux64.tar.gz
sudo rm -rf /opt/nvim
sudo tar -C /opt -xzf nvim-linux64.tar.gz

然后增加执行路径:

PATH变量(~/.bashrc, ~/.zshrc, ...)
export PATH="$PATH:/opt/nvim-linux64/bin"

这样就安装好了,命令为:nvim。查看该命令:

which nvim
/opt/nvim-linux64/bin/nvim

参考

在 Linux 下安装最新版本,官方链接:Pre-built archives

包管理器 lazy.nvim

使用包管理器lazy.nvim

安装 Lua

目前最新版本是 Lua 5.4,如果你的 Ubuntu 里的最新 Lua 版本是 5.2 或 5.3,就修改下面的安装命令:

sudo apt update
sudo apt install lua5.4

安装必要软件

还需要安装必要的软件:

sudo apt install liblua5.4-dev
sudo apt install unzip
sudo apt install make

安装完成之后,就可以安装 LuaRocks 了。

安装 LuaRocks

LuaRocks 是 Lua 模块安装的管理器,lazy.nvim 使用了 LuaRocks,所以,先安装 LuaRocks。

官方教程:安装LuaRocks

安装 Kickstart

git clone https://github.com/nvim-lua/kickstart.nvim.git "${XDG_CONFIG_HOME:-$HOME/.config}"/nvim

安装完成之后,执行下面的命令:

nvim

启动 nvim,自动安装 lazy.nvim 和常用插件。

等安装完成之后,就可以开始使用 nvim 了。

指南

关于 Kickstart 的解释,可见:~/.config/nvim/init.lua

启动 nvim 之后,输入下面的命令,即可以查看指南:

:Tutor

新插件,以及如何记住位置

新安装的 kickstart 没有记住位置,

这里有一个讨论:How to get Nvim to remember last open buffers, splits and cursor position

似乎这个插件可以用:rmagatti / auto-session

直接修改 init.lua

  1. 把下面的代码插入 init.lua,就可以实现记住位置的功能:
vim.api.nvim_create_autocmd('BufRead', {
callback = function(opts)
vim.api.nvim_create_autocmd('BufWinEnter', {
once = true,
buffer = opts.buf,
callback = function()
local ft = vim.bo[opts.buf].filetype
local last_known_line = vim.api.nvim_buf_get_mark(opts.buf, '"')[1]
if
not (ft:match('commit') and ft:match('rebase'))
and last_known_line > 1
and last_known_line <= vim.api.nvim_buf_line_count(opts.buf)
then
vim.api.nvim_feedkeys([[g`"]], 'nx', false)
end
end,
})
end,
})
  1. 相对行号
vim.wo.relativenumber = true

Kickstart 的配置与扩展

下面这个文档比较详细:Configuration and Extension,复制在下面:

  • Inside of your copy, feel free to modify any file you like! It's your copy!

  • Feel free to change any of the default options in init.lua to better suit your needs.

  • For adding plugins, there are 3 primary options:

    • Add new configuration in lua/custom/plugins/* files, which will be auto sourced using lazy.nvim (uncomment the line importing the custom/plugins directory in the init.lua file to enable this)

    • Modify init.lua with additional plugins.

    • Include the lua/kickstart/plugins/* files in your configuration.

You can also merge updates/changes from the repo back into your fork, to keep up-to-date with any changes for the default configuration.

Example: Adding an autopairs plugin

Example: Adding a file tree plugin

学习 Lua

从 Neovim 0.9 版本开始,内置了 Lua 指南,使用下面的命令就可以查看:

:h lua-guide

Github 有翻译版本:nanotee / nvim-lua-guide

参考

Setup Neovim with kickstart.nvim

Ultimate Neovim Setup Guide: lazy.nvim Plugin Manager