Vi basics. Or how to quit vi

George Stefanis
George Stefanis

Lately I've been teaching myself a little bit of VI magic and migrating my writing to Spacemacs as an editor. I'll be writing more about Spacemacs in the future so bare with me. For now here's the list I have in front of me on my screen to navigate myself around VI. I will be adding more commands as I go.

The very basic stuff

  • :q - Quit vi
  • :w - Write. Aka Save
  • :wq - Save and exit.
  • u - Undo.
  • ctrl-r - Redo.
  • . - Repeat the previous command

Copy, Pasting and removing

  • v<object> - Select text. v and the arrows can help you select specific things. vw, select word. v$, select from the cursor to the end of the line.
  • y - Yank text. Aka copy your selection
  • d<object> - Deleting. E.g dw, deletes word. d$, delete from word all the way to the end of line
  • dd - Delete the entire line. That's my favourite, most convenient command.
  • p - Puts the deleted or yanked text. The equivalent of paste.
  • x - Delete the character under the cursor.

Moving and searching

  • G - Move to the end of the buffer
  • gg - Move to the beginning of the buffer
  • :<number> - Takes you to the selected line
  • /<word> - Search for a word. pressing n takes you to the next occurrence. N to the previous.
  • ?<word> - Same as above but it goes upwards instead of searching downwards.
  • % - Pressing that over one of the following characters {, [, ( will take you to its match.
  • h - Move left
  • j - Move down (looks like a down arrow)
  • k - Move up
  • l - Move right
  • # - Searches for the previous word under the cursor (press # again or n to go to the next)
  • * - Searches for the next word under the cursor (press * again or n to go to the next)
  • 0 - Moves you to the beginning of the line (think of it as place 0 of an array)
  • $ - Moves you to the end of the line
  • p - Go to previous find (while searching)
  • n - Go to next find (while searching)
  • w - Go to the next word.
  • b - Go to the previous word.

Changing words and things

  • r - replace character under the cursor. Super handy.
  • c<object> - Change characters. E.g cw, changes the word. c$, changes from the character you are on all the way to the end of the line.
  • :s/old/new/g - Substitute old for new in the entire line. Without the g it does only the first occurrence.
  • :%s/old/new/g - Substitute old for new in the entire buffer.
  • :%s/old/new/gc - Substitute old for new in the entire buffer and ask for confirmation as well.
  • o - Add line below where you currently are and go into edit mode.
  • O - Add line above where you currently are and go into edit mode.
  • a - Append after the cursor
  • A - Append at the end of the current line
  • i - Append before the cursor

General tips

  • Adding numbers in front of commands will do that for as many times as you asked. For example 5dd will delete the next 5 lines. d2w will delete the next 2 words etc.

Special thanks: Diomidis Spinellis, Panagiotis Vryonis, Isaac Kokkinidis