vim: command cheatsheet

2021-09-10

 | 

~3 min read

 | 

595 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 text with yanked text

Imagine you yanked some text. Now, you want to replace other text with that. How might you do it?

One way is to learn the commands and just use those.

For example, if you want to replace text with what you’ve last yanked, you can do "_d"0P.

Or, if you want to do last yanked or deleted text: "_dP.

These are variations of a technique referred to as “stamping”.

This is nice to know, but wouldn’t it be nice if we remapped our bindings so that it was more ergonomic?

".vimrc

" Replace the visually selected text with previously yanked text
vnoremap p pgvy

There are also other strategies, laid out here.

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?
  • ^ puts the cursor on the first char of the line.
  • w moves to the next word.
  • i" inserts a " before the cursor.
  • <Esc is Vim’s way to input a control character in this context, here it’s <Esc> used to exit insert mode.
  • ee moves to the end of the next word.
  • a" appends a ” after the cursor.
  • <CR> executes the command.

Cut, Copy, and Paste

Cutting is done with the letter d in NORMAL mode. Copying (yanking) is done with y. Pasting is done with p (or P which pastes before the cursor).

There is a lot of navigation involved in vim since that’s one of the ways that you can move so quickly around a file.

Two that I always struggle to remember are:

  1. gg will take you to the top of the file,
  2. shift+g will take you to the last line of the file. shift+g is also how you navigate to a specific line number by preceding the command with the desired row. E.g., If you want to go to the 11th line in the file, the command would be ESC 11 Shift+G.

Related Posts
  • Zsh: Keyboard Shortcuts


  • 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!