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
vimnano:w:q:wqddyyp
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.
vimnano:w:q:wqddyypConfiguration 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.
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.
| Mode | Purpose | Enter With |
|---|---|---|
| Normal (Command) mode | Navigate, delete, copy, and issue commands — the default mode on open | Esc from any other mode |
| Insert mode | Type text directly into the file, like a typical editor | i, a, o (from Normal mode) |
| Command-line mode | Run :-prefixed commands like save, quit, or search-and-replace | : (from Normal mode) |
Esc to return to Normal mode before trying a command again.
# Open a file for editing (creates it if it doesn't exist)
vim notes.txt
| Command | Action |
|---|---|
:w | Save (write) the file |
:w filename | Save a copy under a new name |
:q | Quit — fails if there are unsaved changes |
:q! | Quit and discard unsaved changes |
:wq or ZZ | Save and quit |
:x | Save and quit, but only writes if changes were made |
ZZ (capital Z twice) from Normal mode saves and quits in two keystrokes — no colon, no Enter needed.
| Key | Moves |
|---|---|
h j k l | Left, down, up, right (arrow keys also work) |
w / b | Forward / backward one word |
0 | Start of the current line |
$ | End of the current line |
gg | First line of the file |
G | Last line of the file |
:n | Go to line number n |
Ctrl+F / Ctrl+B | Scroll forward / backward one screen |
h 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.
| Key | Effect |
|---|---|
i | Insert before the cursor |
a | Insert (append) after the cursor |
o | Open a new line below and insert |
O | Open a new line above and insert |
| Key | Effect |
|---|---|
x | Delete the character under the cursor |
dd | Delete (cut) the current line |
3dd | Delete 3 lines starting at the cursor |
yy | Yank (copy) the current line |
p | Paste after the cursor / current line |
u | Undo the last change |
Ctrl+R | Redo |
5dd deletes 5 lines, 3yy yanks 3 lines. This pattern shows up throughout vim.
# 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
% 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.
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
| Shortcut | Action |
|---|---|
Ctrl+O | Write out (save) the file |
Ctrl+X | Exit nano |
Ctrl+K | Cut the current line |
Ctrl+U | Paste (uncut) the last cut text |
Ctrl+W | Search for text |
vi, installed on nearly every Linux system:-prefixed commands like saving, quitting, and substitution:q and :q!?