Vi/Vim Editor — Most Frequently Used Commands

Modes (understand this first — everything else follows from it)

Mode Purpose How to enter
Normal (Command) Mode Navigate, delete, copy — default mode on open Esc (from any other mode)
Insert Mode Actually type/edit text i, a, o, etc. (see below)
Visual Mode Select text (like highlighting with a mouse) v, V, Ctrl+v
Command-line Mode Run commands (save, quit, search/replace) :

Golden rule interviewers love: Vi always opens in Normal mode — you must explicitly enter Insert mode to type.


1. Opening & Saving Files

vi filename.txt          # open (or create) a file
vi +10 filename.txt      # open and jump to line 10
vi +/pattern filename    # open and jump to first match of "pattern"
Command Action
:w Save (write)
:w newname.txt Save as a new file
:q Quit (fails if unsaved changes)
:q! Quit without saving (force)
:wq or :x Save and quit
ZZ Save and quit (Normal mode shortcut, no :)
ZQ Quit without saving (Normal mode shortcut)

2. Switching to Insert Mode (7 ways — know at least 4)

Command Action
i Insert before cursor
a Insert after cursor (append)
I Insert at start of line
A Insert at end of line
o Open a new line below and insert
O Open a new line above and insert
Esc Exit Insert mode → back to Normal mode

3. Cursor Movement (Normal Mode)

Basic movement

Command Action
h j k l Left, Down, Up, Right (arrow key alternative)
w Jump to start of next word
b Jump to start of previous word
e Jump to end of current/next word
0 Go to start of line
^ Go to first non-blank character of line
$ Go to end of line

Larger jumps

Command Action
gg Go to first line of file
G Go to last line of file
:n or nG Go to line number n (e.g. :25 or 25G)
Ctrl+f Page forward (down)
Ctrl+b Page backward (up)
Ctrl+d Scroll half page down
Ctrl+u Scroll half page up
% Jump to matching bracket ( ) { } [ ]

4. Editing Commands

Command Action
x Delete character under cursor
X Delete character before cursor
dd Delete (cut) current line
ndd Delete n lines (e.g. 3dd deletes 3 lines)
dw Delete word from cursor
d$ or D Delete to end of line
d0 Delete to start of line
yy Yank (copy) current line
nyy Yank n lines
p Paste after cursor/below line
P Paste before cursor/above line
r Replace single character
R Enter Replace mode (overwrite as you type)
cc Change (delete + insert) entire line
cw Change word
u Undo last change
Ctrl+r Redo
. Repeat last change (extremely useful)
J Join current line with next line
~ Toggle case of character under cursor