正在加载,请稍候…

Vim and Neovim Mastery: Editing Speed and Plugin Ecosystem

Master Vim and Neovim for professional development. Learn motion commands, macros, registers, LSP integration, popular plugins like Telescope and nvim-tree.

Vim and Neovim Mastery

Essential Motion Commands

" Horizontal motion
w/b     - next/prev word start
e/ge    - next/prev word end
f{char} - find char forward on line
F{char} - find char backward
t{char} - before char forward
;/,     - repeat f/F/t/T

" Vertical motion
{/}     - paragraph up/down
[{/]}   - jump to opening/closing brace
gg/G    - top/bottom of file
5G      - line 5
ctrl+d/u - scroll half page down/up
ctrl+f/b - scroll full page

" Smart targeting
%       - jump to matching bracket/paren
*/#     - search word under cursor fwd/bck
gd      - go to definition
gi      - go to insert position

Text Objects

" Inner vs Around
ci"     - change inside quotes
ca"     - change around quotes (including quotes)
cit     - change inside HTML tag
dat     - delete around tag
=ip     - re-indent inside paragraph
yap     - yank around paragraph

" Custom text objects with targets.vim
cin,    - change inside next comma-separated item
ciq     - change inside any quotes

Macros and Registers

" Record macro to register 'a'
qa             " start recording
<operations>
q              " stop recording
@a             " replay macro
10@a           " replay 10 times
@@             " replay last macro

" Edit macro in register
:let @a = substitute(@a, 'old', 'new', 'g')

" Useful registers
""  - default (unnamed) register
"0  - last yank
"*  - system clipboard (macOS)
"+  - system clipboard (Linux)
"%  - current filename
"/  - last search pattern
":  - last command

" Append to register
qA  " Append to register 'a' (uppercase letter)

Neovim Config with Lua

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

-- Bootstrap lazy.nvim (plugin manager)
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",
    }
  },

  -- Fuzzy finder
  { "nvim-telescope/telescope.nvim",
    dependencies = { "nvim-lua/plenary.nvim" }
  },

  -- Syntax highlighting
  { "nvim-treesitter/nvim-treesitter",
    build = ":TSUpdate"
  },

  -- File explorer
  { "nvim-tree/nvim-tree.lua" },

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

LSP Configuration

-- After mason setup
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,
})

Telescope Shortcuts

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)

Productivity Multipliers

" Macros for common transformations
" Convert snake_case to camelCase
:s/(_([a-z]))/\u\2/g

" Add semicolons to end of lines
:%s/([^;])$/\1;/

" Sort and remove duplicates
:sort u

" Execute vim commands on selected lines
:'<,'>normal @a   " Run macro on selection

" Global command patterns
:g/pattern/d           " Delete matching lines
:g/pattern/y A         " Yank matching to register A
:g/^s*$/d             " Delete blank lines
:v/pattern/d           " Delete non-matching lines