参考阅读-awesome-neovim

安装neovim

bash
 1
 2
 3
 4
 5
 6
 7
 8
 9
10

# 安装编译需要的依赖
sudo apt-get install ninja-build gettext cmake unzip curl build-essential


git clone https://github.com/neovim/neovim.git
cd neovim

make CMAKE_BUILD_TYPE=RelWithDebInfo
sudo make install

安装前需要查看针对你正在使用的Linux发行版需要的安装依赖

编译安装后,默认安装目录为 /usr/local/bin,如果你的系统适用了 update-alternatives管理vi编辑器的软链接,那么更新软链接的方法如下:

bash
1
2
3
4
5
6

# 添加新的软链接到vi编辑器
sudo update-alternatives --install /usr/bin/vi vi /usr/local/bin/nvim 35

# 切换适用新添加的nvim 编辑器
sudo update-alternatives --config vi

切换后的效果类似如下:

text
1
2
3
4
5
6
7
8
9
# update-alternatives --config vi
有 3 个候选项可用于替换 vi (提供 /usr/bin/vi)。

  选择       路径               优先级  状态
------------------------------------------------------------
* 0            /usr/local/bin/nvim   35        自动模式
  1            /usr/bin/nvim         30        手动模式
  2            /usr/bin/vim.tiny     15        手动模式
  3            /usr/local/bin/nvim   35        手动模式

配置过程

选择包管理器: lazy.nvim 是目前nvim使用最多的包管理器。

适用lazy.nvim的要求:

  • Neovim >= 0.8.0 (needs to be built with LuaJIT)
  • Git >= 2.19.0 (for partial clones support)
  • a Nerd Font (optional)

而很多Linux发行版提供的neovim版本可能比这个要求要低。因此,通常我们需要自己手动编译安装最新版本的Neovim,否则是无法正常使用lazy.nvim的。

配置init.lua

lua
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
-- load options
require('options')


-- load Packervim
require('plugins')


-- load keymappings
require('keymaps')


-- Set colorscheme
require('colorscheme')


-- Set LSP
require('lsp')

选项options.lua配置

主要包括功能:

  • Tab替换空格处理(格式统一)
  • 界面美化
  • 适用fzf功能搜索
lua
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
-- Hint: use `:h <option>` to figure out the meaning if needed
vim.opt.clipboard = 'unnamedplus' -- use system clipboard
vim.opt.completeopt = { 'menu', 'menuone', 'noselect' }
vim.opt.mouse = 'a' -- allow the mouse to be used in Nvim

-- Tab
vim.opt.tabstop = 4 -- number of visual spaces per TAB
vim.opt.softtabstop = 4 -- number of spacesin tab when editing
vim.opt.shiftwidth = 4 -- insert 4 spaces on a tab
vim.opt.expandtab = true -- tabs are spaces, mainly because of python

-- UI config
vim.opt.number = true -- show absolute number
vim.opt.relativenumber = true -- add numbers to each line on the left side
vim.opt.cursorline = true -- highlight cursor line underneath the cursor horizontally
vim.opt.splitbelow = true -- open new vertical split bottom
vim.opt.splitright = true -- open new horizontal splits right
vim.opt.termguicolors = true -- enabl 24-bit RGB color in the TUI
vim.opt.showmode = false -- we are experienced, wo don't need the "-- INSERT --" mode hint

-- Searching
vim.opt.incsearch = true -- search as characters are entered
vim.opt.hlsearch = false -- do not highlight matches
vim.opt.ignorecase = true -- ignore case in searches by default
vim.opt.smartcase = true -- but make it case sensitive if an uppercase is entered

-- for nvim-tree
-- disable netrw at the very start of your init.lua (strongly advised)
vim.g.loaded_netrw = 1
vim.g.loaded_netrwPlugin = 1

快捷键绑定

包含功能:

  • Tab页面操作 Ctrl + t 新建/+n 下一页/+p 上一页/+w 关闭当前页
  • 用 <C-h/j/k/l> 快速在多窗口之间移动光标
  • 用 Ctrl + 方向键进行窗口大小的调整
  • 选择模式下可以一直用 Tab 或者 Shift-Tab 改变缩进
lua
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
-- define common options
local opts = {
    noremap = true, -- non-recursive
    silent = true, -- do not show message
}

-----------------
-- Normal mode --
-----------------

-- tabs
vim.keymap.set('n', '<C-t>', ':tabnew', opts)
vim.keymap.set('n', '<C-n>', ':tabNext<CR>', opts)
vim.keymap.set('n', '<C-p>', ':tabPrev<CR>', opts)
vim.keymap.set('n', '<C-w>', ':tabclose<CR>', opts)

-- Hint: see `:h vim.keymap.set()`
-- Better window navigation
vim.keymap.set('n', '<C-h>', '<C-w>h', opts)
vim.keymap.set('n', '<C-j>', '<C-w>j', opts)
vim.keymap.set('n', '<C-k>', '<C-w>k', opts)
vim.keymap.set('n', '<C-l>', '<C-w>l', opts)

-- Resize with arrows
-- delta: 2 lines
vim.keymap.set('n', '<C-Up>', ':resize -2<CR>', opts)
vim.keymap.set('n', '<C-Down>', ':resize +2<CR>', opts)
vim.keymap.set('n', '<C-Left>', ':vertical resize -2<CR>', opts)
vim.keymap.set('n', '<C-Right>', ':vertical resize +2<CR>', opts)

-- for nvim-tree
-- default leader key: \
vim.keymap.set('n', '<leader>e', ':NvimTreeToggle<CR>', opts)

-----------------
-- Visual mode --
-----------------

-- Hint: start visual mode with the same area as the previous area and the same mode
vim.keymap.set('v', '<', '<gv', opts)
vim.keymap.set('v', '>', '>gv', opts)