Skip to main content

Editing Text in Vim

General

  • actions

  • modifiers

  • text objects

  • {operator}{motion}

  • {operator}{count}{motion}

  • {count}{operator}{motion}

Inserting text

Unlike most editors, in Vim’s normal mode, your cursor is always directly on top of a character. That’s why the idea of inserting text before or after the cursor becomes relevant in vim.

  • insert before cursor: i, I
  • insert after cursor: a, A

Deleting text

  • d = delete
  • dw doesn’t mean “delete a word”; it means “put me in delete-what-ever-i-touch mode and then move to the next word”; or, “turn my cursor into a force that deletes anything it travels through and then travel to the beginning of the next word”; so, you’re really just moving the cursor and having a “delete” effect on whatever you pass on your journey; it’s kind of like being in star mode in mario :)

Changing text

  • c = change (i.e. delete and enter insert mode)
  • r = replace character under cursor
  • O, o = add a blank line above or below cursor (requires vim-unimpaired)
  • Power of g • Vim Tips Wiki 📖
    • :[range]g/pattern/cmd = act on the specified range (default whole file) by executing cmd on each line matching pattern
    • Enable in VSCodeVim via the Neovim integration
  • How to survive without multiple cursors in vim • Great ideas for keymaps that make it easier to apply the same change multiple times • Heiker Curiel 📖

Selecting text

  • v = visual mode
  • V = visual line mode
  • Ctrl-v = visual block mode

Links:

Text Objects

  • Objects:
    • w = word
    • s = sentence (delimited by ., ?, or !)
    • p = paragraph
    • "", '', or `` = quoted string
    • () = parenthesized block
    • {} = braced block
    • [] = bracketed block
    • t = tag block (HTML)
  • Modifiers:
    • i = inner (not including surrounding whitespace)
    • a = all (including surrounding whitespace)
  • Examples:
    • diw = delete inner word
    • dap = delete all paragraph
    • ci) = change inner parenthesized block
    • cas = change all sentence
    • vi{ = visually select inner braced block
    • vat = visually select all tag block
  • References:
    • :help text-objects
    • Vim Text Objects: The Definitive Guide • Carbon Five 📖

      The key is to try to always edit by text objects. Editing by motions e.g., by part of a line, to the next occurrence of a character, is tedious, clumsy, and slow.

    • kana/vim-textobj-user Wiki • Vim plugin for defining custom text objects (or installing plugins that do that) 🛠️

While Text objects exist in Vim, but not in Vi mode in the shell (which is based on Vi, not Vim).

Find and replace in file

  • :%s/before/after/gc
  • Reference vim help command for before/after syntax and flags
  • Add that great VimTips article on this
  • In selection:

Find and replace in project

Replace a symbol

  • When renaming a symbol, don’t rely on test pattern matching
  • Leverage LSP to intelligently rename all occurrences that symbol instead
  • TODO: define symbol

Replace text using Telescope -> Quickfix list

  1. Find occurrences you’re looking for using Telescope
  2. Send results to Quickfix list
  3. :cdo s/before/after/gc | update

Links:

Spectre

  • Omit this option?
  • Any benefits?

Inbox