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
Escto enter) - Insert Mode: For typing text. (Press
ito enter) - Visual Mode: For selecting text. (Press
vto 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):wqor:x: Save and quit:q!: Quit without saving (discard changes)
Navigation (Normal Mode)
hjkl: Left, Down, Up, Right (instead of arrow keys)w: Jump forward to the start of a wordb: Jump backward to the start of a worde: Jump forward to the end of a word0: Jump to the start of the line$: Jump to the end of the linegg: Go to the first line of the documentG: Go to the last line of the document50G: Go to line 50
Editing Text
From Normal mode:
i: Insert before the cursorI: Insert at the beginning of the linea: Append after the cursorA: Append at the end of the lineo: Open a new line below the current lineO: Open a new line above the current line
Deleting and Copying (Normal Mode)
x: Delete character under the cursordd: Delete (cut) current line5dd: Delete 5 linesyy: Yank (copy) current line5yy: Yank 5 linesp: Paste after the cursorP: Paste before the cursoru: UndoCtrl + r: Redo
Searching and Replacing
/pattern: Search forward for "pattern"?pattern: Search backward for "pattern"n: Next matchN: 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