๐ Linux/Unix: vi/vim Editor Basics โ Modes & Navigation Explained
๐งฒ Introduction โ Why Learn vi/vim?
The vi and vim editors are powerful, terminal-based text editors that are pre-installed on nearly every Unix-like system. Whether editing config files, scripts, or code, knowing vim is an essential skill for system admins and developers.
๐ฏ In this guide, youโll learn:
- How to switch between modes in
vi/vim - How to navigate files using keyboard shortcuts
- Practical commands for cursor movement, file editing, and quitting
โจ What is vi and vim?
vi: Original UNIX text editor.vim: “Vi IMproved” โ adds features like syntax highlighting, multi-level undo, plugins, and more.
Launch vim with:
vim filename
๐ฎ Modes in vi/vim
vim works in different modes, each with a specific purpose:
| Mode | Purpose | How to Access |
|---|---|---|
| Normal Mode | Default mode โ used for navigation | Press Esc |
| Insert Mode | Used to insert or edit text | Press i, a, o |
| Visual Mode | Used to select and manipulate text | Press v or V |
| Command Mode | Used for saving, exiting, searching | Type : in Normal |
๐ Entering Modes โ Quick Keys
| Key | Action |
|---|---|
i | Insert before cursor |
I | Insert at beginning of line |
a | Append after cursor |
A | Append at end of line |
o | Open new line below |
O | Open new line above |
Esc | Return to Normal mode |
๐งญ Navigation Shortcuts
In Normal Mode, use these keys to navigate:
| Key | Action |
|---|---|
h | Move left |
l | Move right |
j | Move down |
k | Move up |
0 | Move to beginning of line |
^ | Move to first non-blank char |
$ | Move to end of line |
gg | Move to start of file |
G | Move to end of file |
:n | Go to line n |
Ctrl+d | Scroll down half page |
Ctrl+u | Scroll up half page |
๐งช Basic Workflow Example
vim test.txt
- Press
iโ Enter Insert Mode - Type:
Hello, Linux World! - Press
Escโ Back to Normal Mode - Type
:wqโ Save and Quit
โ Saving and Quitting
| Command | Description |
|---|---|
:w | Save (write) |
:q | Quit |
:wq | Save and quit |
:q! | Quit without saving |
:x | Save (if modified) + quit |
๐ Search and Replace (Command Mode)
| Command | Description |
|---|---|
/word | Search forward for “word” |
?word | Search backward |
n / N | Repeat search (next/prev) |
:%s/old/new/g | Replace all “old” with “new” |
:%s/old/new/gc | Replace with confirmation |
๐ง Bonus: Visual Mode Tips
- Press
vto select by character - Press
Vto select by line - Use
d,y, orpafter selection:dโ deleteyโ yank (copy)pโ paste
๐ Summary โ Recap & Next Steps
Mastering vi/vim starts with understanding its modes and movement commands. With just a few keystrokes, you can navigate, edit, search, and save filesโwithout ever leaving your terminal.
๐ Key Takeaways:
Escalways returns to Normal Mode.- Use
i,a,oto enter Insert Mode. - Use
:wq,:q!, and/searchin Command Mode. - Efficient text navigation makes editing lightning-fast.
โ FAQs
โ Whatโs the difference between vi and vim?
โ
vim is an improved version of vi, offering syntax highlighting, undo levels, plugins, and more.
โ How do I copy and paste in vim?
โ
Use yy to yank a line, p to paste below the cursor.
โ How can I delete a line?
โ
Press dd in Normal Mode.
โ Can I open multiple files in vim?
โ
Yes:
vim file1 file2
Use :n and :prev to switch files.
โ How do I enable line numbers?
โ
In Command Mode:
:set number
Share Now :
