VIM Basics
VIM (Vi IMproved) is a powerful text editor commonly used for programming and system administration. This guide covers essential VIM commands and concepts to help you get started efficiently.
📌 Getting Started with VIM
VIM operates in multiple modes:
Mode Overview
- Normal Mode: Used for navigation and text manipulation (default mode).
- Insert Mode: Used for typing and inserting text (
iorato enter). - Visual Mode: Used for selecting text (
vfor character selection,Vfor line selection). - Command Mode: Used for executing commands (
:to enter command mode).
To start VIM, use:
vim filename
If the file does not exist, VIM creates a new file.
🔄 Switching Between Modes
Quick Mode Switching
- Normal Mode → Insert Mode: Press
i(insert before cursor) ora(append after cursor). - Insert Mode → Normal Mode: Press
Esc. - Normal Mode → Visual Mode: Press
v(character selection) orV(line selection). - Normal Mode → Command Mode: Press
:.
📜 Basic Navigation
Move the cursor without using arrow keys:
| Command | Action |
|---|---|
h | Move left |
l | Move right |
j | Move down |
k | Move up |
gg | Jump to the beginning of the file |
G | Jump to the end of the file |
0 | Move to the start of a line |
^ | Move to the first non-blank character |
$ | Move to the end of a line |
✏️ Editing Text
Inserting Text
Insert Commands
i→ Insert before cursorI→ Insert at the beginning of the linea→ Append after cursorA→ Append at the end of the lineo→ Open a new line belowO→ Open a new line above
Deleting Text
| Command | Action |
|---|---|
x | Delete character under cursor |
X | Delete character before cursor |
dd | Delete the current line |
D | Delete from cursor to end of line |
d$ | Delete from cursor to end of line |
d0 | Delete from cursor to start of line |
Copy and Paste
| Command | Action |
|---|---|
yy | Copy (yank) the current line |
y$ | Copy from cursor to end of line |
y0 | Copy from cursor to start of line |
p | Paste after the cursor |
P | Paste before the cursor |
🔍 Searching and Replacing
Search for Text
Use / to search forward and ? to search backward:
/SearchTerm " Searches forward
?SearchTerm " Searches backward
Press n to go to the next occurrence and N to go to the previous occurrence.
Find and Replace
:%s/old/new/g " Replace all occurrences of 'old' with 'new'
%→ Entire fileg→ Global (replace all occurrences in a line)c→ Confirm before replacing
🗂️ Working with Multiple Files
Open Multiple Files
vim file1 file2
- Switch between files:
:n(next) or:prev(previous) - Open file explorer:
:e .
Split Windows
| Command | Action |
|---|---|
:split filename | Horizontal split |
:vsplit filename | Vertical split |
Ctrl + w | Switch between splits |
💾 Saving and Exiting
| Command | Action |
|---|---|
:w | Save changes |
:wq or ZZ | Save and exit |
:q! | Quit without saving |
:x | Save and close (only if changes were made) |
🔧 Customization and Configuration
VIM can be customized using a .vimrc file.
Example .vimrc:
set number " Show line numbers
syntax on " Enable syntax highlighting
set autoindent " Enable auto-indentation
set tabstop=4 " Set tab width to 4 spaces
🚀 Advanced Tips
Undo and Redo
| Command | Action |
|---|---|
u | Undo last action |
Ctrl + r | Redo last undone action |
Macros (Recording & Replaying Commands)
| Command | Action |
|---|---|
q<letter> | Start recording macro in register <letter> |
q | Stop recording |
@<letter> | Run the recorded macro |
🎯 Conclusion
Mastering VIM requires practice, but once you get familiar with the commands and shortcuts, it becomes an incredibly powerful tool for efficient text editing.
For more details, check out the VIM documentation:
:help