Linux Text Editors: nano and vi/vim Essentials
I built reliable terminal editing skills in nano for quick edits and vi/vim for power editing on any server. Knowing vim's modal model guarantees I can edit a config on a minimal host where vi is the only editor present.
Objective & Context
Every Linux server ships vi; many minimal images ship nothing else. This lab builds confidence in both editors so a remote config edit never becomes a blocker, the practical complement to the scripting labs.
Learning Objectives
- Open, edit, save, and exit files in nano using its on-screen shortcuts.
- Navigate vim's modes (normal, insert, command-line) without getting stuck.
- Save and quit in vim with :w, :q, :wq, and recover from :q! situations.
- Search and substitute text across a file with / and :%s.
- Choose the right editor for quick edits versus structured editing sessions.
Environment & Prerequisites
Prerequisites
- Any Linux system; nano and vi are preinstalled virtually everywhere, vim on most.
- A scratch config file to edit safely (copy /etc/hosts to ~/lab/hosts).
- A terminal that passes through Ctrl and Esc keys cleanly.
- No root needed if you practice on copies in your home directory.
Step-by-Step Execution
1. Quick edit with nano
mkdir -p ~/lab && cp /etc/hosts ~/lab/hosts && nano ~/lab/hosts # Ctrl+O save, Ctrl+X exitExpected output:
(nano opens full-screen; bottom bar shows ^O Write Out, ^X Exit)
# after Ctrl+O, Enter, Ctrl+X: file saved, back at shell
What this teaches: nano is modeless: what you type is what gets inserted, and ^ means Ctrl, so ^O writes the file and ^X exits. Practicing on the ~/lab/hosts copy keeps this safe; editing /etc/hosts directly would need sudo. It is the safe default when you just need a quick change.
2. Edit and save in vim
cp /etc/ssh/sshd_config ~/lab/sshd_config && vim ~/lab/sshd_config # i to insert, Esc, :wq to saveExpected output:
(vim opens; press i, edit, Esc, then :wq)
"sshd_config" 132L, 4821C written
What this teaches: vim separates navigation (normal mode) from typing (insert mode via i); Esc returns to normal, and :wq writes and quits. Knowing the mode model is what unsticks beginners. The copy is world-readable so no sudo is needed; the real file would need it.
3. Search and replace in vim
:%s/^#PermitRootLogin.*/PermitRootLogin no/Expected output:
"sshd_config" 132L written
$ grep "^PermitRootLogin" ~/lab/sshd_config
PermitRootLogin no
What this teaches: :%s ranges over the whole file (%), s substitutes, and the ^# anchor matches the commented default line (typically #PermitRootLogin prohibit-password) so the replacement works on a stock config; add the g flag when a line can hold more than one match. Type this as an ex command inside vim on the ~/lab/sshd_config copy, then verify with grep. This turns repetitive config changes into one command.
How It Works
Terminal editors exist because servers often have no graphical environment and no network path for a GUI editor. nano optimizes for discoverability: every action is listed at the bottom of the screen, so a first-time user can save and exit without a manual. It trades power for zero learning curve.
vim optimizes for the opposite: modal editing separates moving through text from changing it, which lets single keystrokes become a composable language (d2w deletes two words, ci" changes inside quotes). The learning curve pays off when you edit config files daily, and vi (not vim) is guaranteed present on minimal systems, which is why the basics transfer.
Search and substitute are where vim earns its keep for sysadmins: / finds interactively with n/N to repeat, and :%s/old/new/g rewrites every occurrence in one pass. Combined with :set number and syntax highlighting, these turn a 132-line sshd_config from intimidating to navigable.
Validation & Testing
Make a change in each editor and confirm it persisted with cat; perform a global substitution in vim and verify every occurrence changed. Pass criteria: edits save correctly and the vim substitution is complete and accurate.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Stuck and cannot type in vim | You are in normal mode, where keys are commands | Press i to enter insert mode; press Esc to return to normal |
| :q refuses to quit | The buffer has unsaved changes | Use :wq to save first, or :q! to discard deliberately |
| Terminal shows garbled output after vim | The alternate screen was not restored after a crash | Run reset or stty sane to restore the terminal |
| nano shortcuts do nothing | ^ means the Ctrl key, not the caret character | Hold Ctrl while pressing the letter (Ctrl+O, Ctrl+X) |
| Search in vim finds nothing | Case sensitivity or a magic character in the pattern | Try :set ignorecase, or escape regex metacharacters with backslash |
| Swap file warning on open | A previous session crashed and left .file.swp behind | Recover with vim -r, then delete the swap file once verified |
Advanced: Troubleshooting
- Stuck in vim: press Esc then
:q!to quit without saving. - Read-only file: reopen with sudo or use
:w !sudo tee %. - Mangled paste: use
:set pastebefore pasting blocks into vim.
Key Results
- Edited configs reliably on minimal hosts with vi only.
- Performed global search/replace across a file in one command.
- Recovered from the common "stuck in vim" trap confidently.
- Chose nano vs vim by task to minimize edit time.
Key Takeaways
- nano: type to edit, Ctrl+O saves, Ctrl+X exits; the safe quick-edit choice.
- vim is modal: i inserts, Esc returns to normal, :wq saves and quits.
- :%s/old/new/g substitutes across the whole file in one command.
- vi basics work on every Unix; vim extras are a bonus.
- Practice on copies first; editors make changes the moment you save.
Practice Challenges
Challenge 1: Blind config edit
Goal: without looking at a cheat sheet, open a hosts copy in vim, add two entries, save, and quit.
Hint: the whole flow is i, type, Esc, :wq.
Challenge 2: Bulk port change
Goal: in an sshd_config copy, use one :%s command to change every commented #Port 22 line to Port 2222, then verify with grep.
Hint: anchor with ^ and handle the # in the pattern.