Skip to content

Instantly share code, notes, and snippets.

@lazydao
Last active April 9, 2025 07:40
Show Gist options
  • Save lazydao/b4e89d079538bd34966c5aab630ef273 to your computer and use it in GitHub Desktop.
Save lazydao/b4e89d079538bd34966c5aab630ef273 to your computer and use it in GitHub Desktop.
neovim
vim.opt.runtimepath:prepend("~/.vim")
-- 禁用 netrw,避免与 nvim-tree 冲突
vim.g.loaded_netrw = 1
vim.g.loaded_netrwPlugin = 1
-- 插件管理
vim.cmd([[
call plug#begin('~/.vim/plugged')
Plug 'neovim/nvim-lspconfig'
Plug 'hrsh7th/nvim-cmp'
Plug 'hrsh7th/cmp-nvim-lsp'
Plug 'hrsh7th/cmp-buffer'
Plug 'hrsh7th/cmp-path'
Plug 'hrsh7th/cmp-cmdline'
Plug 'hrsh7th/cmp-vsnip'
Plug 'hrsh7th/vim-vsnip'
Plug 'folke/tokyonight.nvim'
Plug 'rktjmp/lush.nvim'
Plug 'tpope/vim-fugitive'
Plug 'airblade/vim-gitgutter'
Plug 'spin6lock/vim_sproto'
Plug 'nvim-tree/nvim-tree.lua'
Plug 'ludovicchabant/vim-gutentags'
Plug 'Yggdroot/LeaderF'
Plug 'sbdchd/neoformat'
Plug 'nvim-lualine/lualine.nvim'
Plug 'nvim-tree/nvim-web-devicons'
Plug 'echasnovski/mini.move'
call plug#end()
]])
-- 界面设置
vim.opt.termguicolors = true -- 启用真彩色支持
vim.opt.background = "dark" -- "dark" | "light"
vim.opt.cursorline = true -- 高亮当前行
vim.opt.number = true -- 显示行号
vim.opt.relativenumber = true -- 相对行号显示
-- 主题设置
vim.cmd("colorscheme tokyonight")
-- 缩进设置
vim.opt.expandtab = true
vim.opt.tabstop = 4
vim.opt.shiftwidth = 4
vim.opt.softtabstop = 4
vim.opt.autoindent = true
vim.opt.smarttab = true -- 插入 tab 时使用 shiftwidth
-- 搜索设置
vim.opt.ignorecase = true
vim.opt.smartcase = true
vim.opt.incsearch = true
vim.opt.hlsearch = true
vim.opt.backspace = { "indent", "eol", "start" }
-- 实用设置
vim.opt.scrolloff = 8 -- 保持光标周围始终有8行显示
vim.opt.sidescrolloff = 8 -- 水平滚动时保持8列显示
-- 拼写检查设置
vim.opt.spell = true -- 默认开启拼写检查
vim.opt.spelllang = { "en", "cjk" } -- 设置拼写检查语言为英语,并支持中日韩文字
vim.opt.spelloptions = "camel" -- 支持驼峰命名的拼写检查
-- 其他设置
vim.opt.laststatus = 3
vim.opt.clipboard = "unnamedplus"
vim.opt.lazyredraw = true -- 延迟重绘以提高性能
vim.opt.mouse = 'a' -- 启用鼠标支持
-- lualine 配置
require('lualine').setup()
-- mini.move 配置
require('mini.move').setup()
-- nvim-cmp 配置
local cmp = require("cmp")
cmp.setup({
snippet = {
expand = function(args)
vim.fn["vsnip#anonymous"](args.body)
end,
},
mapping = cmp.mapping.preset.insert({
["<Tab>"] = cmp.mapping.select_next_item({ behavior = cmp.SelectBehavior.Insert }),
["<S-Tab>"] = cmp.mapping.select_prev_item({ behavior = cmp.SelectBehavior.Insert }),
["<C-b>"] = cmp.mapping.scroll_docs(-4),
["<C-f>"] = cmp.mapping.scroll_docs(4),
["<C-e>"] = cmp.mapping.abort(),
--["<CR>"] = cmp.mapping.confirm({ select = true }),
["<CR>"] = cmp.mapping(function(fallback)
if cmp.visible() and cmp.get_selected_entry() then
-- 如果补全菜单可见,并且确实选择了一个条目,则确认它
cmp.confirm({ select = true }) -- select = true 在这里是安全的,因为我们已经检查过有选中项
else
-- 否则,执行默认的回车行为(换行)
fallback()
end
end, { "i", "s" }), -- 应用于 insert 和 select 模式
}),
sources = cmp.config.sources({
{ name = "nvim_lsp" },
{ name = "vsnip" },
}, {
{ name = "buffer" },
}),
})
cmp.setup.cmdline({ "/", "?" }, {
mapping = cmp.mapping.preset.cmdline(),
sources = { { name = "buffer" } },
})
cmp.setup.cmdline(":", {
mapping = cmp.mapping.preset.cmdline(),
sources = cmp.config.sources({
{ name = "path" },
}, {
{ name = "cmdline" },
}),
})
-- LSP 配置
local lspconfig = require("lspconfig")
local capabilities = require("cmp_nvim_lsp").default_capabilities()
lspconfig.lua_ls.setup({ capabilities = capabilities })
lspconfig.pyright.setup({
capabilities = capabilities,
on_init = function(client)
local project_root = client.config.root_dir or vim.fn.getcwd()
local venv_path = project_root .. "/.venv/bin/python"
if vim.fn.filereadable(venv_path) == 1 then
client.config.settings.python.pythonPath = venv_path
end
end,
})
-- Gutentags 配置
vim.g.gutentags_enabled = 1
vim.g.gutentags_ctags_tagfile = "./tags"
-- LeaderF 配置
vim.g.Lf_WindowPosition = "popup"
vim.g.Lf_WorkingDirectoryMode = "Ac"
-- nvim-tree 配置
require("nvim-tree").setup()
-- Neoformat 配置
vim.g.neoformat_enabled_python = {'ruff'}
vim.g.neoformat_python_ruff = {
exe = 'ruff',
args = {'format', '-'},
stdin = 1
}
vim.g.neoformat_enabled_lua = {'stylua'}
vim.g.neoformat_lua_stylua = {
exe = 'stylua',
args = {'--search-parent-directories', '-'},
stdin = 1
}
-- 快捷键配置
-- nvim-tree 快捷键:隐藏、聚焦
vim.keymap.set("n", "<leader>t", ":NvimTreeToggle<CR>", { silent = true })
vim.keymap.set("n", "<leader>n", ":NvimTreeFocus<CR>", { silent = true })
-- 查找文件
vim.keymap.set('n', '<C-p>', ':Leaderf file<CR>', { silent = true })
-- 格式化快捷键
vim.keymap.set('n', '<leader>f', ':Neoformat<CR>', { silent = true })
-- 窗口切换
vim.keymap.set('n', '<C-h>', '<C-w>h')
vim.keymap.set('n', '<C-j>', '<C-w>j')
vim.keymap.set('n', '<C-k>', '<C-w>k')
vim.keymap.set('n', '<C-l>', '<C-w>l')
-- 快速保存
vim.keymap.set('n', '<leader>w', ':w<CR>')
vim.keymap.set('n', '<leader>q', ':q<CR>')

安装

参考:https://github.com/neovim/neovim/blob/master/INSTALL.md

推荐做法:

# 下载最新版本
wget https://github.com/neovim/neovim/releases/latest/download/nvim-linux64.tar.gz
# 解压
tar -zxvf nvim-linux64.tar.gz
# 通过 `ln -s` 将解压后的 nvim 可执行文件导入 PATH,例如
sudo ln -s ~/nvim-linux64/bin/nvim /usr/local/bin/nvim

配置

  • :checkhealth 检查配置状况
  • :PlugInstall to install the plugins
  • :PlugUpdate to install or update the plugins
  • :PlugDiff to review the changes from the last update
  • :PlugClean to remove plugins no longer in the list

环境依赖

sudo apt install curl python3-dev python3-venv python3-neovim python3-pynvim exuberant-ctags pipx vim-nox
sudo apt install nodejs npm -y
sudo npm install -g pyright
pipx install ruff
cargo install stylua
  • vim-plug 安装
    • sh -c 'curl -fLo "${XDG_DATA_HOME:-$HOME/.local/share}"/nvim/site/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim'
    • 如果 vim 中已经安装了(~/.vim/autoload/plug.vim),就不用再装了
  • lua-language-server 安装并添加至PATH

快捷键

主要列举配合插件使用的,自带的可以去看 vim 那边的 tips。

可以用:map查看已绑定快捷键。

默认情况下,<leader> 键的映射是反斜杠 \

快捷键 动作
<leader>b * :LeaderfBuffer
[d Jump to the previous diagnostic(neovim专属)
]d Jump to the next diagnostic(neovim专属)
[c @(GitGutterPrevHunk)
]c @(GitGutterNextHunk)
[s 跳转到上一个拼写错误(neovim专属)
]s 跳转到下一个拼写错误(neovim专属)
z= 显示建议的正确拼写(neovim专属)
zg 将光标下的单词标记为正确(neovim专属)
zw 将光标下的单词标记为错误(neovim专属)
Ctrl-] 跳转到光标下符号的定义。
Ctrl-t 返回上一个位置。
:tag <symbol> 直接跳转到指定符号。
:ts <symbol> 查找所有匹配的符号。

nvim-tree 中使用的快捷键

  • a Create File Or Directory
  • S Search
  • R Refresh
  • W Collapse
  • x Cut
  • c Copy
  • p Paste
  • r Rename
  • d Delete
  • <C-X> Open: Horizontal Split
  • <C-V> Open: Vertical Split
  • <C-T> Open: New Tab
  • d Delete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment