posts-go/posts/2023-05-03-neovim.md

4.3 KiB

My neovim workflow

nvim-000

All configs are in my dotfiles.

Trick or treat

Search current word: *

Search multiple words:

:/\vword1|word2|word3

Replace word:

:%s/word1/word2/g

Delete all lines contain word:

:g/word/d

Delete all lines not contain word:

:g!/word/d

:v/word/d

Play macro a (after selecting lines):

:norm! @a

Sort lines (after selecting lines):

:sort -u

Reverse lines (after selecting lines):

:!tail -r

Column-ize lines:

:!column -t

Jumpo

Basic:

  • gg: first line
  • G: last line
  • 0: first character of line
  • $: last character of line
  • w, b: word forward/backward
  • e, ge: end of word current/before
  • W, B: WORD (word with special char) forward/backward
  • E, gE: end of WORD current/before
  • f{char}, F{char}: find forward/backward character
  • %: jump between matching pair of (), [], {}

Advance:

  • CTRL-O, CTRL-I: cursor position backward/forward
  • {, }: paragraph backward/forward
  • H: top of screen
  • M: middle of screen
  • L: bottom of screen
  • CTRL-], CTRL-T: jump to tag/jump back from tag

Keymap

Use both \ and ; as leader key:

vim.keymap.set("n", ";", "<Leader>", { remap = true })
vim.keymap.set("n", "'", "<Leader>", { remap = true })

Ground rules:

  • <Leader> prefix: prefer global keymap

  • <Space> prefix: prefer lsp keymap, for coding of course :D

  • Native neovim:

    • gcc: comment/uncomment current line
    • gc: comment/uncomment selected lines
    • gx: open file/url under cursor
    • Q, @: execute last macro
    • ]d, [d: next/previous diagnostic
    • K: hover
  • Black hole register:

    • <Leader>d: d without yank
    • <Leader>c: c without yank
    • <Leader>x: x without yank
  • With ibhagwan/fzf-lua:

    • <Leader>f: find files
    • <Leader>l: find lines
    • <Leader>rg: grep files
    • <Leader>g: git status
    • With neovim/nvim-lspconfig helps:
      • <Space>s: list lsp symbols
      • <Space>r, gr: list references
      • <Space>i, gi: list implementation
      • <Space>ca: list code action
  • With hrsh7th/nvim-cmp:

    • <C-Space>: trigger completion
  • With nvim-tree/nvim-tree.lua, inside nvim-tree:

    • <C-n>: toggle
    • <Leader>n: locate file
    • a: create
    • d: delete
    • r: rename
    • x: cut
    • c: copy
    • p: paste
    • U: toggle hidden
  • With tpope/vim-projectionist

    • :A: open alternate file
  • With echasnovski/mini.nvim

    • With mini-git
    • ]C, [C, ]c, [c: next/previous git change
    • With mini-surround
      • sa: add surround
      • sd: delete surround
      • sr: replace surround
  • With stevearc/conform.nvim:

    • <Space>f: format code
  • With neovim/nvim-lspconfig:

    • <Space>e: current diagnostic
    • <Space>lr: restart lsp server
    • gd: go to definition
    • <Space>k, gk: hover
    • <F2>: rename
  • With github/copilot.vim:

    • <M-Right>: completion, replace Tab

References / Thanks