Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

Vim Keybindings Guide for Beginners

Learn the Vim modes and the most useful keys to move, edit, copy, search and save.

How-To Guides2 min read2 steps5 screenshots

Vim modes

Vim has modes. The same key does different things in each mode.

  • Normal mode is for moving and running commands. Press Esc to return to it. Vim starts here.
  • Insert mode is for typing text. Enter it with i.
  • Visual mode selects text. Enter it with v (characters), V (lines) or Ctrl+v (blocks).
  • Command-line mode runs commands such as save, quit and search. Enter it with :.

Install Vim

sudo apt install vim
Installing Vim
vim --version
The Vim version

Start Vim

vim filename.txt
Opening Vim

Press i to enter insert mode and type your text.

Vim insert mode

Press Esc to go back to normal mode.

Vim normal mode

Move around (normal mode)

  • h left, j down, k up, l right.
  • w to the start of the next word, b to the start of the previous word, e to the end of the word.
  • 0 to the start of the line, ^ to the first non-blank character, $ to the end of the line.
  • gg to the first line, G to the last line, 42G to line 42.
  • Ctrl+f page down, Ctrl+b page up.

Insert text

  • i insert before the cursor, a after the cursor.
  • I at the start of the line, A at the end.
  • o open a new line below, O above.

Edit

  • x delete the character under the cursor.
  • dd delete (cut) the line, dw delete a word, d$ delete to the end of the line.
  • yy copy (yank) the line, yw copy a word.
  • p paste after the cursor, P before.
  • u undo, Ctrl+r redo.
  • r replace one character, cw change a word.
  • . repeat the last change.
  • A number before a command repeats it: 3dd deletes 3 lines.

Visual mode

  1. Press v and move to select text.
  2. Press y to copy, d to cut, or > to indent.

Search and replace

  • /word search forward, ?word search backward.
  • n next match, N previous match.
  • Replace in the whole file: :%s/old/new/g
  • Confirm each change: :%s/old/new/gc

Save and quit (command-line mode)

  • :w save.
  • :q quit.
  • :wq or ZZ save and quit.
  • :q! quit without saving.
  • :w newname save as a new file.

Tips

  • If you are stuck, press Esc twice, then type :q! to leave.
  • Type vimtutor in the terminal for a free interactive lesson.
  • Change settings in ~/.vimrc, for example set number shows line numbers.

Quick recap

  • i to type, Esc to stop, :wq to save and quit.