| |

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:

CommandModeAction
iNormalInsert before cursor
aNormalAppend after cursor
oNormalNew line below
ONormalNew line above
EscInsertReturn to Normal
:wNormalSave
:qNormalQuit
:wqNormalSave and quit
:q!NormalQuit without saving
ZZNormalSave and quit

b. The navigation and the search

The vim navigation is the Normal-mode keys.

KeyAction
hLeft
jDown
kUp
lRight
wNext word
bPrevious word
0Beginning of line
$End of line
ggBeginning of file
GEnd of file
:NGo to line N
Ctrl+fPage down
Ctrl+bPage 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.

CommandAction
uUndo
Ctrl+rRedo
.Repeat the last change
:uUndo 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

ModeEnterPurpose
NormalEscCommands
Inserti, a, oText
Command-line:Ex commands
VisualvSelection

Essential Commands

CommandAction
iInsert before
aAppend after
oNew line below
ONew line above
EscNormal mode
:wSave
:qQuit
:wqSave and quit
:q!Quit without save
ZZSave and quit

Navigation

KeyAction
h j k lArrows
w bWords
0 $Line ends
gg GFile ends
:NLine N
Ctrl+f Ctrl+bPages

Search and Replace

CommandAction
/patternSearch forward
?patternSearch backward
n NNext/previous
:s/old/new/Replace on the line
:%s/old/new/gReplace in the file
:%s/old/new/gcReplace with the confirm

Save and Quit

CommandAction
:wSave
:w fileSave as
:wqSave and quit
:xSave and quit (if changed)
:qQuit
: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

PitfallWhy It HappensFix
Text not typedNormal modePress i first
Commands not workingInsert modePress Esc
Quit failsUnsaved changesUse :q! or :wq
Wrong modeLost trackThe status line shows
Search not foundCase-sensitive:set ignorecase
No line numbersThe default off:set number
Swap file warningThe crashUse 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

ItemValue
Openvim file
Normal modeDefault
Insert modei, a, o
Command mode:
Save:w
Quit:q
Save and quit:wq, ZZ
Quit without save:q!
Search/pattern
Replace:%s/old/new/g
Undou
RedoCtrl+r
Line numbers:set number
Recoveryvim -r file

Key takeaways:

  • The vim is modal. The Normal mode is the default, and the keys are the commands. The Insert mode is for the text. The i enters the Insert mode, and the Esc returns to the Normal mode.
  • The save is the :w, and the quit is the :q. The :wq saves and quits. The :q! quits without the save. The ZZ is the two keys in the Normal mode.
  • The search is the /pattern, and the replace is the :%s. The n and the N navigate the matches. The %s/old/new/g replaces in the whole file. The g is the global, and the c is the confirm.
  • The navigation is the h, the j, the k, the l. The keys are the home row. The gg and the G go to the file ends. The :N goes to the line N.
  • The undo is the u, and the redo is the Ctrl+r. The . repeats the last change. The :earlier and the :later are the time-based undo.
  • The vim -r recovers the file from the swap. The swap is the .file.swp created on the open. The crash leaves the swap, and the -r recovers the changes.
  • The vim is the editor that the tools assume. The visudo, the crontab -e, and the git commit open the vi or the vim. 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!