正在加载,请稍候…

Vim 与 Neovim 精通:编辑速度与插件生态

掌握 Vim 和 Neovim 进行专业开发,学习移动命令、宏、寄存器、LSP 集成以及 Telescope 和 nvim-tree 等流行插件。

Vim 与 Neovim 精通:编辑速度与插件生态

Vim 与 Neovim 精通

基本移动命令

" 水平移动
w/b     - 下一个/上一个单词开头
e/ge    - 下一个/上一个单词结尾
f{char} - 行内向前查找字符
F{char} - 行内向后查找字符
t{char} - 向前到字符前
;/,     - 重复 f/F/t/T

" 垂直移动
{/}     - 段落上/下
[{/]}   - 跳转到开/闭括号
gg/G    - 文件顶部/底部
5G      - 第5行
ctrl+d/u - 向下/上滚动半页
ctrl+f/b - 向下/上滚动整页

" 智能定位
%       - 跳转到匹配的括号
*/#     - 向前/向后搜索光标下的单词
gd      - 转到定义
gi      - 转到上次插入位置

Vim 与 Neovim 精通:编辑速度与插件生态插图

文本对象

" 内部 vs 周围
ci"     - 修改引号内内容
ca"     - 修改引号周围内容(包括引号)
cit     - 修改 HTML 标签内内容
dat     - 删除标签周围内容
=ip     - 重新缩进段落内部
yap     - 复制段落周围内容

" 使用 targets.vim 自定义文本对象
cin,    - 修改下一个逗号分隔项内内容
ciq     - 修改任意引号内内容

宏与寄存器

" 录制宏到寄存器 'a'
qa             " 开始录制
<操作>
q              " 停止录制
@a             " 回放宏
10@a           " 回放10次
@@             " 回放上次宏

" 编辑寄存器中的宏
:let @a = substitute(@a, 'old', 'new', 'g')

" 常用寄存器
""  - 默认(未命名)寄存器
"0  - 上次复制内容
"*  - 系统剪贴板(macOS)
"+  - 系统剪贴板(Linux)
"%  - 当前文件名
"/  - 上次搜索模式
":  - 上次命令

" 追加到寄存器
qA  " 追加到寄存器 'a'(大写字母)

Vim 与 Neovim 精通:编辑速度与插件生态插图

使用 Lua 配置 Neovim

-- ~/.config/nvim/init.lua

-- 引导 lazy.nvim(插件管理器)
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({"git", "clone", "--filter=blob:none",
    "https://github.com/folke/lazy.nvim.git", "--branch=stable", lazypath})
end
vim.opt.rtp:prepend(lazypath)

require("lazy").setup({
  -- LSP
  { "neovim/nvim-lspconfig",
    dependencies = {
      "williamboman/mason.nvim",
      "williamboman/mason-lspconfig.nvim",
      "hrsh7th/nvim-cmp",
      "hrsh7th/cmp-nvim-lsp",
    }
  },

  -- 模糊查找器
  { "nvim-telescope/telescope.nvim",
    dependencies = { "nvim-lua/plenary.nvim" }
  },

  -- 语法高亮
  { "nvim-treesitter/nvim-treesitter",
    build = ":TSUpdate"
  },

  -- 文件浏览器
  { "nvim-tree/nvim-tree.lua" },

  -- Git 集成
  { "lewis6991/gitsigns.nvim" },
  { "tpope/vim-fugitive" },
})

LSP 配置

-- 在 mason 设置之后
local lspconfig = require('lspconfig')
local capabilities = require('cmp_nvim_lsp').default_capabilities()

local on_attach = function(client, bufnr)
  local opts = { buffer = bufnr, noremap = true, silent = true }
  vim.keymap.set('n', 'gd', vim.lsp.buf.definition, opts)
  vim.keymap.set('n', 'gr', vim.lsp.buf.references, opts)
  vim.keymap.set('n', 'K', vim.lsp.buf.hover, opts)
  vim.keymap.set('n', '<leader>rn', vim.lsp.buf.rename, opts)
  vim.keymap.set('n', '<leader>ca', vim.lsp.buf.code_action, opts)
  vim.keymap.set('n', '<leader>f', function()
    vim.lsp.buf.format({ async = true })
  end, opts)
end

-- TypeScript
lspconfig.ts_ls.setup({
  capabilities = capabilities,
  on_attach = on_attach,
})

-- Python
lspconfig.pyright.setup({
  capabilities = capabilities,
  on_attach = on_attach,
})

Vim 与 Neovim 精通:编辑速度与插件生态插图

Telescope 快捷键

local telescope = require('telescope.builtin')
local opts = { noremap = true, silent = true }

vim.keymap.set('n', '<leader>ff', telescope.find_files, opts)
vim.keymap.set('n', '<leader>fg', telescope.live_grep, opts)
vim.keymap.set('n', '<leader>fb', telescope.buffers, opts)
vim.keymap.set('n', '<leader>fh', telescope.help_tags, opts)
vim.keymap.set('n', '<leader>fs', telescope.lsp_document_symbols, opts)
vim.keymap.set('n', '<leader>fr', telescope.lsp_references, opts)
vim.keymap.set('n', '<leader>fd', telescope.diagnostics, opts)
vim.keymap.set('n', '<leader>gc', telescope.git_commits, opts)

效率倍增器

" 常用转换的宏
" 将 snake_case 转换为 camelCase
:s/\(_\([a-z]\)\)/\\u\\2/g

" 在行尾添加分号
:%s/\([^;]\)$/\\1;/

" 排序并去重
:sort u

" 在选中行上执行 vim 命令
:'<,'>normal @a   " 在选区上运行宏

" 全局命令模式
:g/pattern/d           " 删除匹配行
:g/pattern/y A         " 将匹配行复制到寄存器 A
:g/^\s*$/d             " 删除空行
:v/pattern/d           " 删除不匹配行