Here’s a list of basic vi (or vim) commands to help you navigate and edit files in the vi text editor, a powerful but sometimes intimidating tool for beginners:
- Open a file:
vi filename
- Save and exit:
:wq # Write (save) and quit
- Quit without saving:
:q! # Force quit (discard changes)
- Save without exiting:
:w # Write (save) changes
-
Command Mode (default mode when you open
vi):- Navigate, delete, copy, paste, search, etc.
- Press
Escto return to Command Mode from other modes.
-
Insert Mode (to edit text):
- Press
ito start inserting text at the cursor. - Other insert commands:
a– Insert after the cursor.o– Insert a new line below the current line.O– Insert a new line above the current line.
- Press
- Move cursor:
h (left), j (down), k (up), l (right) # Or use arrow keys
- Jump to line:
:10 # Jump to line 10
- Move to start/end of line:
0 # Start of line $ # End of line
- Move by words:
w # Next word b # Previous word
- Scroll:
Ctrl + f # Page down Ctrl + b # Page up
- Delete:
x # Delete character under cursor dd # Delete entire line dw # Delete word
- Undo/Redo:
u # Undo last change Ctrl + r # Redo
- Copy/Paste:
yy # Copy (yank) a line p # Paste after the cursor P # Paste before the cursor
- Replace text:
r # Replace character under cursor R # Enter replace mode (overwrite text)
- Search forward:
/pattern # Search for "pattern" (press `n` for next match)
- Search backward:
?pattern # Search backward - Replace in entire file:
:%s/old/new/g # Replace all "old" with "new" globally
- Enter Visual Mode (to select text):
v # Character-wise selection V # Line-wise selection
- After selecting, use
yto yank (copy) ordto delete.
- Show line numbers:
:set number # Hide with `:set nonumber`
- Split screen:
:split # Horizontal split :vsplit # Vertical split
- Reload a file (discard changes):
:e! # Re-open the file
Practice Tip: Open a test file (e.g., vi test.txt) and experiment with these commands. Remember, vi mastery takes time—start with basic navigation and editing!