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 :
