Quick Reference

Cheatsheets

Practical command references for Linux, networking, servers, containers, databases, and more.

Cheatsheet#vim-cheatsheet

Vim Cheatsheet

Vim is a highly configurable, terminal-based text editor built to enable efficient text editing.

Modes

Vim has several modes:

  • Normal Mode: Default mode for navigating and manipulating text. (Press Esc to enter)
  • Insert Mode: For typing text. (Press i to enter)
  • Visual Mode: For selecting text. (Press v to enter)
  • Command Mode: For running commands like save or quit. (Press : from Normal mode)

Exiting Vim

From Normal mode, type : followed by:

  • :w : Save (write)
  • :q : Quit (fails if there are unsaved changes)
  • :wq or :x : Save and quit
  • :q! : Quit without saving (discard changes)
  • h j k l : Left, Down, Up, Right (instead of arrow keys)
  • w : Jump forward to the start of a word
  • b : Jump backward to the start of a word
  • e : Jump forward to the end of a word
  • 0 : Jump to the start of the line
  • $ : Jump to the end of the line
  • gg : Go to the first line of the document
  • G : Go to the last line of the document
  • 50G : Go to line 50

Editing Text

From Normal mode:

  • i : Insert before the cursor
  • I : Insert at the beginning of the line
  • a : Append after the cursor
  • A : Append at the end of the line
  • o : Open a new line below the current line
  • O : Open a new line above the current line

Deleting and Copying (Normal Mode)

  • x : Delete character under the cursor
  • dd : Delete (cut) current line
  • 5dd : Delete 5 lines
  • yy : Yank (copy) current line
  • 5yy : Yank 5 lines
  • p : Paste after the cursor
  • P : Paste before the cursor
  • u : Undo
  • Ctrl + r : Redo

Searching and Replacing

  • /pattern : Search forward for "pattern"
  • ?pattern : Search backward for "pattern"
  • n : Next match
  • N : Previous match
  • :%s/old/new/g : Replace all occurrences of "old" with "new" in the whole file
  • :%s/old/new/gc : Same as above, but ask for confirmation

Configuration (~/.vimrc)

syntax on           " Enable syntax highlighting
set number          " Show line numbers
set relativenumber  " Show relative line numbers
set tabstop=4       " 1 tab = 4 spaces
set shiftwidth=4
set expandtab       " Convert tabs to spaces