Vim Basic Commands Tutorial | Generated by AI
Vim (short for Vi IMproved) is a powerful, modal text editor that’s the default on many Linux systems like Red Hat. It’s keyboard-driven and efficient once you learn the basics. Vim has three main modes: Normal (for navigation and commands, default), Insert (for typing text), and Visual (for selecting text). Press Esc to return to Normal mode from anywhere.
The best way to learn hands-on is to run vimtutor in your terminal—it launches an interactive tutorial.
1. Opening and Closing Vim
- Open or create a file:
vim filename.txt(e.g.,vim notes.txt). - Quit without saving: In Normal mode, type
:q!and press Enter. - Exit Vim: See saving section below.
2. Modes
- Normal mode: Default—use for commands. Press
Escto enter. - Insert mode: Press
i(insert before cursor) ora(append after cursor) to enter. You’ll see-- INSERT --at the bottom. - Visual mode: Press
vto select text. - Command-line mode: Press
:to enter (for save, quit, search).
3. Navigation (in Normal Mode)
Use these instead of arrow keys for efficiency:
h: Left one characterj: Down one linek: Up one linel: Right one characterw: Forward to start of next wordb: Backward to start of previous word0: Start of line$: End of linegg: Top of fileG: Bottom of file:n: Jump to line n (e.g.,:5)- Prefix with numbers:
5j(down 5 lines)
Enable line numbers: :set number
4. Inserting and Editing Text
- Enter Insert mode:
i: Insert before cursorI: Insert at start of linea: Append after cursorA: Append at end of lineo: New line below (enters Insert mode)O: New line above (enters Insert mode)
- Exit Insert mode:
Esc - Replace single character:
r(then type the new char) - Undo:
u - Redo:
Ctrl + r - Repeat last command:
.
5. Deleting, Copying, and Pasting
- Delete character:
x - Delete line:
dd - Delete range:
:3,5d(lines 3 to 5) - Copy (yank) line:
yy - Copy selected text: In Visual mode (
v), highlight with arrows, theny - Paste after cursor:
p - Paste before cursor:
P - Select text:
v(character-wise), then move cursor;Escto exit Visual.
6. Saving and Quitting
- Save:
:w - Save and quit:
:wqor:xorZZ(capital Z twice) - Force save and quit:
:wq! - Quit without saving:
:q!orZQ
7. Searching
- Search forward:
/pattern(e.g.,/hello), then Enter;nfor next. - Search backward:
?pattern - Replace:
:%s/old/new/g(replace all “old” with “new”)
Quick Tips
- Help:
:helpor:h keyword - Split screen:
:split filename(horizontal) or:vsplit filename(vertical);Ctrl + w wto switch. - Practice in a test file to avoid mistakes.
- Syntax highlighting:
:syntax on
This covers the essentials to get you editing files quickly. For more, explore :help in Vim or the resources below.
Getting started with Vim: The basics
Basic Vim Commands
Vim Cheat Sheet