Red Hat System Administration I · RH124

Chapter 8

Editing Text Files
vim modes · Basic editing · Search & replace · nano
CIS126RH — Mesa Community College

Chapter Objective

Create and edit text files at the command line using vim, including moving around a file, making edits, saving changes, and performing search-and-replace operations.

Key Commands

  • vim
  • nano
  • :w
  • :q
  • :wq
  • dd
  • yy
  • p

Why a Text-Mode Editor?

Configuration files, scripts, and unit files all live as plain text, and administrators frequently need to edit them directly at the command line — often over an SSH session with no graphical interface available at all.

RHEL ships two editors you should be comfortable with at different levels: vim, a powerful modal editor descended from the classic Unix vi, and nano, a simpler editor with on-screen keybinding hints.

Why It Mattersvim is installed on essentially every Linux system you will ever touch professionally. Even administrators who prefer a friendlier editor day to day need enough vim to survive a minimal recovery environment where nothing else is installed.

vim's Three Modes

Unlike most editors, vim is modal: the same key does different things depending on which mode you're in. Nearly all beginner confusion with vim comes from not tracking which mode is currently active.

ModePurposeEnter With
Normal (Command) modeNavigate, delete, copy, and issue commands — the default mode on openEsc from any other mode
Insert modeType text directly into the file, like a typical editori, a, o (from Normal mode)
Command-line modeRun :-prefixed commands like save, quit, or search-and-replace: (from Normal mode)
Warning — Press Esc First — If vim seems to be "ignoring" your commands or inserting strange characters into the file, you're almost certainly still in Insert mode. Press Esc to return to Normal mode before trying a command again.

Opening, Saving, and Quitting

# Open a file for editing (creates it if it doesn't exist)
vim notes.txt
CommandAction
:wSave (write) the file
:w filenameSave a copy under a new name
:qQuit — fails if there are unsaved changes
:q!Quit and discard unsaved changes
:wq or ZZSave and quit
:xSave and quit, but only writes if changes were made
Tip — ZZ Is Faster Than It Looks — Typing ZZ (capital Z twice) from Normal mode saves and quits in two keystrokes — no colon, no Enter needed.

Moving Around a File

KeyMoves
h j k lLeft, down, up, right (arrow keys also work)
w / bForward / backward one word
0Start of the current line
$End of the current line
ggFirst line of the file
GLast line of the file
:nGo to line number n
Ctrl+F / Ctrl+BScroll forward / backward one screen
Note — Keeping Hands on the Home Rowh j k l feel awkward at first, but they let you navigate without leaving the keyboard's home row — the entire reason vim uses them instead of arrow keys.

Basic Editing — Entering Insert Mode

KeyEffect
iInsert before the cursor
aInsert (append) after the cursor
oOpen a new line below and insert
OOpen a new line above and insert

Deleting, Copying, and Pasting (Normal Mode)

KeyEffect
xDelete the character under the cursor
ddDelete (cut) the current line
3ddDelete 3 lines starting at the cursor
yyYank (copy) the current line
pPaste after the cursor / current line
uUndo the last change
Ctrl+RRedo
Tip — Numbers Are Multipliers — Most Normal-mode commands accept a number prefix that repeats them: 5dd deletes 5 lines, 3yy yanks 3 lines. This pattern shows up throughout vim.

Search and Replace

# Search forward for a pattern
/hostname

# Search backward for a pattern
?hostname

# Repeat the last search (forward)
n

# Repeat the last search in the opposite direction
N

# Replace the first "old" with "new" on the current line
:s/old/new/

# Replace ALL occurrences of "old" with "new" on the current line
:s/old/new/g

# Replace all occurrences on EVERY line in the file
:%s/old/new/g

# Same, but ask for confirmation before each replacement
:%s/old/new/gc
Warning — % Means the Whole File — Forgetting the % in :%s/old/new/g silently limits the substitution to just the current line — a very common source of "why didn't that replace everything" confusion.

A Simpler Option: nano

nano is a non-modal editor — what you type is what appears, no mode-switching required — and it displays its most common keybindings at the bottom of the screen at all times.

# Open a file in nano
nano notes.txt
ShortcutAction
Ctrl+OWrite out (save) the file
Ctrl+XExit nano
Ctrl+KCut the current line
Ctrl+UPaste (uncut) the last cut text
Ctrl+WSearch for text
Tip — Know Both — nano is friendlier for quick edits, but not every system has it installed — vim is guaranteed to be present. Be comfortable enough with vim's basics that you're never stuck without an editor.

Key Terms for Chapter 8

vim
A modal, keyboard-driven text editor descended from the classic Unix vi, installed on nearly every Linux system
modal editing
An editing model where the same keys perform different actions depending on the active mode
Normal mode
vim's default mode, used for navigation and issuing commands
Insert mode
vim mode where typed characters are entered directly into the file
Command-line mode
vim mode for :-prefixed commands like saving, quitting, and substitution
yank
vim's term for copying text
nano
A non-modal, beginner-friendly text editor with on-screen keybinding hints

Review Questions

  1. What are vim's three modes, and what is each one used for?
  2. You're stuck in vim and characters keep appearing in the file instead of your commands working. What's the first thing to try?
  3. What is the difference between :q and :q!?
  4. Write the vim Normal-mode command that deletes the current line.
  5. Write the vim command-line command that replaces every occurrence of "dev" with "prod" across the entire file.
  6. What key opens a new line below the cursor and switches to Insert mode in one step?
  7. Why is it worth learning vim even if you prefer using nano day to day?
1 / 12