LFCS 15 ๐ง Editing Files with vim โ Basics
The vim is the editor that is installed on every Linux system. It is the editor that the visudo, the crontab -e, and the many other tools open by default. It is the editor that the system administrator must know, because the alternative is the editor that may not be installed. The vim is modal: the keys are not always the text, and the mode determines the meaning. The learning curve is the steepest of the editors, and the payoff is the speed and the ubiquity. The LFCS exam expects the basic editing to be automatic, and the vim is the editor that the exam environment may open.
Key point: The vim has the modes. The Normal mode is the default: the keys are the commands. The Insert mode is for the typing: press i to enter, and Esc to leave. The Command-line mode is for the commands: press : in the Normal mode, type the command, and press Enter. The visual mode is for the selection: press v in the Normal mode. The save is :w, the quit is :q, and the save-and-quit is :wq or ZZ. The quit without the save is :q!.
Why vim matters
The availability problem. The vi is the POSIX standard editor, and the vim is the improved version. The vim is installed on the nearly every Linux system, and the vi is the subset. The nano is the friendly editor, but it may not be installed on the minimal systems. The vim is the editor that the administrator can rely on.
The modal problem. The vim is modal: the keys are the commands in the Normal mode and the text in the Insert mode. The new user types the text and the editor interprets the keys as the commands. The confusion is the reason the vim has the learning curve. The understanding of the modes is the key to the fluency.
The ubiquity problem. The visudo opens the vi by default. The crontab -e opens the editor in the EDITOR variable, and the vi is the fallback. The git commit opens the vi if no editor is configured. The vim is the editor that the tools assume.
The power problem. The vim is the powerful editor: the navigation, the search, the replace, the macros, the multiple buffers, the split windows, and the many commands. The nano is the simple editor, and the vim is the one that the experienced administrator uses for the complex editing.
The exam problem. The LFCS tasks often require editing the configuration files. The vim is the editor that the exam environment may open, and the candidate must be able to save, quit, and edit. The nano is the alternative, and the knowledge of both is the safe position.
a. The modes and the basic editing
The vim opens the file in the Normal mode.
$ vim file.txt
The Normal mode is the default. The keys are the commands, not the text. The i enters the Insert mode.
# In the Normal mode:
i
# Now the Insert mode: the typed characters are the text
The i inserts at the cursor. The a appends after the cursor. The o opens the new line below. The O opens the new line above. The I inserts at the beginning of the line. The A appends at the end of the line.
The Esc returns to the Normal mode.
# In the Insert mode:
Esc
# Now the Normal mode: the keys are the commands
The Esc is the key that the user presses the most. The Esc returns to the Normal mode, and the Normal mode is the home.
The save is the :w in the Command-line mode.
# In the Normal mode:
:w
# The file is saved
The : enters the Command-line mode. The w is the write. The Enter executes the command.
The quit is the :q.
# In the Normal mode:
:q
# The editor quits
The :q quits if the file has no unsaved changes. The :q! quits and discards the changes.
The save and the quit is the :wq or the ZZ.
# In the Normal mode:
:wq
# Or:
ZZ
# The file is saved and the editor quits
The ZZ is the two Z keys in the Normal mode. The :wq is the Command-line mode. The both save and quit.
The summary of the essential commands:
| Command | Mode | Action |
|---|---|---|
i | Normal | Insert before cursor |
a | Normal | Append after cursor |
o | Normal | New line below |
O | Normal | New line above |
Esc | Insert | Return to Normal |
:w | Normal | Save |
:q | Normal | Quit |
:wq | Normal | Save and quit |
:q! | Normal | Quit without saving |
ZZ | Normal | Save and quit |
b. The navigation and the search
The vim navigation is the Normal-mode keys.
| Key | Action |
|---|---|
h | Left |
j | Down |
k | Up |
l | Right |
w | Next word |
b | Previous word |
0 | Beginning of line |
$ | End of line |
gg | Beginning of file |
G | End of file |
:N | Go to line N |
Ctrl+f | Page down |
Ctrl+b | Page up |
The h, the j, the k, and the l are the arrow keys. The keys are the home row, and the fingers do not leave the keyboard.
The gg goes to the beginning of the file. The G goes to the end. The :N goes to the line N.
# In the Normal mode:
:10
# The cursor moves to the line 10
The search is the /pattern.
# In the Normal mode:
/error
# The cursor moves to the first "error"
n
# The next match
N
# The previous match
The / searches forward. The ? searches backward. The n goes to the next match. The N goes to the previous match. The Esc cancels the search.
The search and the replace is the :s.
# In the Normal mode:
:s/old/new/
# Replace the first "old" on the current line
:s/old/new/g
# Replace all on the current line
:%s/old/new/g
# Replace all in the file
:%s/old/new/gc
# Replace all with the confirmation
The s is the substitute. The % is the whole file. The g is the global (all on the line). The c is the confirmation.
The : enters the Command-line mode. The %s is the whole file substitute. The g is the global. The c is the confirm.
The :set number shows the line numbers.
# In the Normal mode:
:set number
# The line numbers appear
:set nonumber
# The line numbers disappear
The :set number is the shorthand for the :set nu. The :set nonumber is the :set nonu.
The :set paste disables the auto-indent and the other automatic behaviors. The option is for the pasting of the code from the outside.
# In the Normal mode:
:set paste
# Paste the text
:set nopaste
The :set paste is the tool for the pasting. The :set nopaste restores the automatic behaviors.
c. The saving, the quitting, and the recovery
The save is the :w. The :w filename saves to the different file.
# In the Normal mode:
:w /tmp/backup.txt
# Save to /tmp/backup.txt
The :w with the ! forces the write.
# In the Normal mode:
:w!
# Force the write (e.g., the read-only file)
The quit is the :q. The :q! discards the changes.
# In the Normal mode:
:q!
# Quit without saving
The save and the quit is the :wq or the :x or the ZZ. The :x writes only if the file is changed. The :wq writes always. The ZZ is the :x.
The vim with the multiple files.
$ vim file1.txt file2.txt
The :n goes to the next file. The :prev goes to the previous. The :ls lists the buffers. The :b N goes to the buffer N.
The vim with the recovery.
$ vim -r file.txt
The -r recovers the file from the swap. The swap is the .file.txt.swp in the same directory. The vim creates the swap on the open and deletes it on the clean exit. The crash leaves the swap, and the -r recovers.
The vim with the swap file.
$ ls -la .file.txt.swp
# The swap file
The swap file is the hidden file. The vim detects the existing swap on the open and offers the recovery options.
The vim with the :earlier and the :later.
# In the Normal mode:
:earlier 5m
# The file state 5 minutes ago
:later 5m
# The state 5 minutes later
The :earlier and the :later are the undo on the time. The options are for the recovery of the accidental changes.
The vim with the undo.
| Command | Action |
|---|---|
u | Undo |
Ctrl+r | Redo |
. | Repeat the last change |
:u | Undo the last line |
The u is the undo in the Normal mode. The Ctrl+r is the redo. The . repeats the last change.
The vim with the :e!.
# In the Normal mode:
:e!
# Reload the file from the disk, discarding the changes
The :e! is the reset. The file is reloaded from the disk. The changes are discarded.
Complete Example Session
# ============================================
# PART 1: OPEN AND EDIT
# ============================================
vim file.txt
# i to insert
# Type the text
# Esc to return
# :w to save
# :q to quit
# ============================================
# PART 2: SAVE AND QUIT
# ============================================
vim file.txt
# Edit
# :wq
# ============================================
# PART 3: QUIT WITHOUT SAVE
# ============================================
vim file.txt
# Edit
# :q!
# ============================================
# PART 4: SEARCH
# ============================================
vim file.txt
# /error
# n for the next
# N for the previous
# ============================================
# PART 5: SEARCH AND REPLACE
# ============================================
vim file.txt
# :%s/old/new/g
# :%s/old/new/gc (with the confirmation)
# ============================================
# PART 6: NAVIGATION
# ============================================
vim file.txt
# gg (beginning)
# G (end)
# :10 (line 10)
# ============================================
# PART 7: LINE NUMBERS
# ============================================
vim file.txt
# :set number
# :set nonumber
# ============================================
# PART 8: UNDO
# ============================================
vim file.txt
# Edit
# u (undo)
# Ctrl+r (redo)
# ============================================
# PART 9: MULTIPLE FILES
# ============================================
vim file1.txt file2.txt
# :n (next)
# :prev (previous)
# :ls (list)
# ============================================
# PART 10: RECOVERY
# ============================================
vim -r file.txt
The ten parts covered the open and edit, the save and quit, the quit without save, the search, the search and replace, the navigation, the line numbers, the undo, the multiple files, and the recovery.
Quick Reference
Modes
| Mode | Enter | Purpose |
|---|---|---|
| Normal | Esc | Commands |
| Insert | i, a, o | Text |
| Command-line | : | Ex commands |
| Visual | v | Selection |
Essential Commands
| Command | Action |
|---|---|
i | Insert before |
a | Append after |
o | New line below |
O | New line above |
Esc | Normal mode |
:w | Save |
:q | Quit |
:wq | Save and quit |
:q! | Quit without save |
ZZ | Save and quit |
Navigation
| Key | Action |
|---|---|
h j k l | Arrows |
w b | Words |
0 $ | Line ends |
gg G | File ends |
:N | Line N |
Ctrl+f Ctrl+b | Pages |
Search and Replace
| Command | Action |
|---|---|
/pattern | Search forward |
?pattern | Search backward |
n N | Next/previous |
:s/old/new/ | Replace on the line |
:%s/old/new/g | Replace in the file |
:%s/old/new/gc | Replace with the confirm |
Save and Quit
| Command | Action |
|---|---|
:w | Save |
:w file | Save as |
:wq | Save and quit |
:x | Save and quit (if changed) |
:q | Quit |
:q! | Quit without save |
:e! | Reload |
Best Practices
โ Do This:
# Press Esc often to return to the Normal mode
# The Normal mode is the home # โ
# Use :wq to save and quit
:wq # โ
# Use :q! to discard the changes
:q! # โ
# Use the search for the navigation
/error # โ
# Use the :%s for the replace
:%s/old/new/g # โ
# Use the :set number for the line numbers
:set number # โ
# Use the u for the undo
u # โ
# Use the :earlier for the accidental changes
:earlier 5m # โ
โ Don’t Do This:
# Don't type the text in the Normal mode
# The keys are the commands # โ ๏ธ
# Don't forget the Esc before the command
# The : in the Insert mode inserts the colon # โ ๏ธ
# Don't use the :q with the unsaved changes
:q # the vim refuses # โ ๏ธ
# Don't use the :wq when the :x fits
:wq # writes always # โ
# Don't use the arrow keys in the Normal mode
# The h j k l are the home row # โ ๏ธ
# Don't forget the swap file on the crash
# Use vim -r to recover # โ ๏ธ
# Don't use the vim for the simple edits if the nano fits
# The choice is the task and the comfort # โ ๏ธ
Common Pitfalls
| Pitfall | Why It Happens | Fix |
|---|---|---|
| Text not typed | Normal mode | Press i first |
| Commands not working | Insert mode | Press Esc |
| Quit fails | Unsaved changes | Use :q! or :wq |
| Wrong mode | Lost track | The status line shows |
| Search not found | Case-sensitive | :set ignorecase |
| No line numbers | The default off | :set number |
| Swap file warning | The crash | Use vim -r |
Real-World Examples
1. Edit a Config
sudo vim /etc/hosts
2. Save and Quit
:wq
3. Quit Without Save
:q!
4. Search
/error
n
5. Replace in the File
:%s/old/new/g
6. Go to Line
:50
7. Show the Line Numbers
:set number
8. Undo
u
9. Multiple Files
vim file1 file2
:n
10. Recovery
vim -r file.txt
Visual
The Modes
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ NORMAL MODE (default) โ
โ The keys are the commands. โ
โ i โ Insert, : โ Command-line, v โ Visual โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ INSERT MODE โ
โ The keys are the text. โ
โ Esc โ Normal โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ COMMAND-LINE MODE โ
โ The : commands. โ
โ Enter โ execute, Esc โ Normal โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ VISUAL MODE โ
โ The selection. โ
โ Esc โ Normal โ
โ โ
โ The mode determines the meaning. โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
The Edit Cycle
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ vim file.txt โ
โ โ โ
โ โผ โ
โ NORMAL MODE โ
โ โ โ
โ โโโ i โโโถ INSERT MODE โโโถ type โโโถ Esc โโโถ NORMAL โ
โ โ โ
โ โโโ /pattern โโโถ search โ
โ โ โ
โ โโโ :w โโโถ save โ
โ โ โ
โ โโโ :q! โโโถ quit without save โ
โ โ
โ The Normal mode is the home. โ
โ The Esc is the key to the home. โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Search and Replace
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ :%s/old/new/g โ
โ โ
โ : โโโถ Command-line mode โ
โ % โโโถ The whole file โ
โ s โโโถ Substitute โ
โ /old/new/ โโโถ The pattern and the replacement โ
โ g โโโถ Global (all on the line) โ
โ โ
โ :%s/old/new/gc โ
โ c โโโถ Confirm each replacement โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Navigation
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ gg โโโถ Beginning of file โ
โ G โโโถ End of file โ
โ :N โโโถ Line N โ
โ โ
โ h โ k โ l โ j โ โ
โ โ
โ w โโโถ Next word โ
โ b โโโถ Previous word โ
โ 0 โโโถ Beginning of line โ
โ $ โโโถ End of line โ
โ โ
โ Ctrl+f โโโถ Page down โ
โ Ctrl+b โโโถ Page up โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Summary
| Item | Value |
|---|---|
| Open | vim file |
| Normal mode | Default |
| Insert mode | i, a, o |
| Command mode | : |
| Save | :w |
| Quit | :q |
| Save and quit | :wq, ZZ |
| Quit without save | :q! |
| Search | /pattern |
| Replace | :%s/old/new/g |
| Undo | u |
| Redo | Ctrl+r |
| Line numbers | :set number |
| Recovery | vim -r file |
Key takeaways:
- The
vimis modal. The Normal mode is the default, and the keys are the commands. The Insert mode is for the text. Theienters the Insert mode, and theEscreturns to the Normal mode. - The save is the
:w, and the quit is the:q. The:wqsaves and quits. The:q!quits without the save. TheZZis the two keys in the Normal mode. - The search is the
/pattern, and the replace is the:%s. Thenand theNnavigate the matches. The%s/old/new/greplaces in the whole file. Thegis the global, and thecis the confirm. - The navigation is the
h, thej, thek, thel. The keys are the home row. Theggand theGgo to the file ends. The:Ngoes to the line N. - The undo is the
u, and the redo is theCtrl+r. The.repeats the last change. The:earlierand the:laterare the time-based undo. - The
vim -rrecovers the file from the swap. The swap is the.file.swpcreated on the open. The crash leaves the swap, and the-rrecovers the changes. - The
vimis the editor that the tools assume. Thevisudo, thecrontab -e, and thegit commitopen thevior thevim. The knowledge of the basic commands is the requirement for the system administration.
Remember: The vim is the editor that the system administrator must know. The modes are the learning curve, and the Normal mode is the home. The i for the insert, the Esc for the return, the :w for the save, and the :q for the quit. The search and the replace are the / and the :%s. The vim is the powerful editor, and the power is the reason it is the default on the nearly every Linux system. The nano is the simple alternative, and the choice is the task and the comfort. The LFCS tasks often require the editing, and the vim is the editor that the exam environment may open. The basic commands are the requirement, and the fluency is the goal.
Stop using slow, ad-bloated tool sites! ๐คฎ
๐ Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
โ
Finance (Mortgage, Interest, Inflation)
โ
Tech (Base64, JSON, Dev Suite, IP)
โ
Health (BMI, BMR, TDEE)
โ
Productivity (Timer, Workspace, QR)
โก๏ธ Fast & Private
๐ No data leaves your device
๐ 100% Free
๐ Use it now: https://tools.kandz.me
๐ Bookmark itโyouโll need it later!