vim: command cheatsheet

2021-09-10

 | 

~2 min read

 | 

345 words

A place to store vim commands that aren’t muscle memory yet.

Undo, Repeat, and Redo

Doing a little consolidation as I’ve previously had to learn to undo and redo in vim.

u is Undo . will repeat the last command (e.g. if you just pasted in a word and then use ., it will paste it again) ctrl-r is Redo - the inverse of the u command.

Replacing a block of text

Method One: visual-block Mode

  1. ctrl-v - enter visual block mode
  2. 4j - highlight the same position in the next four rows
  3. Esc to insert before the cursor or Esc to insert after the cursor. In this case, I’m not replacing text, but inserting. In theory, you could also use c2w to replace the first two words.

Note An important thing to know for first time users of the visual block editing is that when in Insert mode to make the change, vim will only render the changes on the first line. The changes will be applied to all lines in the visual block once Insert mode has been escaped (<Esc>).

Method Two: visual-block mode and :normal

  1. v to enter visual-block mode
  2. Expand selection for all lines that are targeted (j or k)
  3. While still in visual-block mode, type : to enter a command - you should see :'<,'>. It is automatically added by Vim and means “act on the selected area”
  4. norm[al] enables executing normal mode commands on a range of lines
  5. Enter the command, e.g., norm ^w2 i"<C-v

    What's actually happening in this command?
  6. ^ puts the cursor on the first char of the line.
  7. w moves to the next word.
  8. i" inserts a " before the cursor.
  9. <Esc is Vim’s way to input a control character in this context, here it’s <Esc> used to exit insert mode.
  10. ee moves to the end of the next word.
  11. a" appends a ” after the cursor.
  12. <CR> executes the command.


Hi there and thanks for reading! My name's Stephen. I live in Chicago with my wife, Kate, and dog, Finn. Want more? See about and get in touch!