✏️ Lesson 4.2: Text Editors: nano and vim
Two editors, two philosophies — pick the one that fits your brain (or learn both).
🎯 Learning Objectives
- Open, edit, save, and exit files in
nano - Understand vim's modal editing concept
- Navigate, insert, save, and quit in
vim - Perform basic search and replace in both editors
- Know when to use which editor
Estimated Time: 45 minutes
📑 In This Lesson
Why Edit in the Terminal?
You might wonder why you'd edit text in a terminal when GUI editors like VS Code exist. Here's why terminal editors matter:
- Remote servers — when you SSH into a server, the terminal is all you have
- System configuration — many config files need to be edited as root with
sudo - Quick edits — sometimes it's faster than opening a GUI application
- Everywhere —
nanoandvimare installed on virtually every Linux system
nano — The Friendly Editor
nano works like any text editor you've used: type text, use keyboard shortcuts to save and quit. No modes, no surprises.
# Open a file (creates it if it doesn't exist)
nano myfile.txt
# Open a file at a specific line number
nano +15 myfile.txt
# Open a file as root (for system configs)
sudo nano /etc/hosts
What nano Looks Like
GNU nano 7.2 myfile.txt
Hello, this is my file.
I can just type here like normal.
│ ← cursor
^G Help ^O Write Out ^W Where Is ^K Cut
^X Exit ^R Read File ^\ Replace ^U Paste
The bottom two lines are your shortcut hints. ^ means Ctrl.
Essential nano Shortcuts
| Shortcut | Action |
|---|---|
| Ctrl+O | Save (Write Out) — press Enter to confirm filename |
| Ctrl+X | Exit — prompts to save if modified |
| Ctrl+K | Cut (delete) the current line |
| Ctrl+U | Paste (uncut) the last cut line |
| Ctrl+W | Search for text |
| Ctrl+\ | Search and replace |
| Ctrl+G | Show help |
| Ctrl+_ | Go to a specific line number |
| Alt+U | Undo |
| Alt+E | Redo |
✅ The Save-and-Quit Combo
The quickest way to save and exit: Ctrl+O, Enter, Ctrl+X. Or just Ctrl+X and answer Y when it asks if you want to save.
nano Beyond the Basics
Selecting and Copying Text
# Start a selection (set mark)
Alt+A (then move cursor to highlight)
# Copy the selection
Alt+6
# Cut the selection
Ctrl+K
# Paste
Ctrl+U
Useful Startup Options
# Open with line numbers displayed
nano -l myfile.txt
# Open with soft line wrapping
nano -$ myfile.txt
# Open in read-only mode
nano -v myfile.txt
# Open with syntax highlighting (auto-detected by extension)
nano script.sh # automatically highlights bash syntax
Customizing nano with .nanorc
# Create or edit your nano config
nano ~/.nanorc
# Add these useful settings:
set linenumbers # Always show line numbers
set tabsize 4 # 4-space tabs
set autoindent # Auto-indent new lines
set mouse # Enable mouse support
set softwrap # Wrap long lines visually
💡 Syntax Highlighting
Modern versions of nano include syntax highlighting for many languages (Python, Bash, HTML, CSS, JavaScript, etc.). It's usually enabled by default. If not, add include "/usr/share/nano/*.nanorc" to your ~/.nanorc.
vim — The Powerful Editor
vim (Vi IMproved) is a modal editor — it has different modes for different tasks. This feels alien at first, but once it clicks, many developers find it faster than any other editor.
# Open a file
vim myfile.txt
# Open at a specific line
vim +15 myfile.txt
# Open as root
sudo vim /etc/hosts
# If vim isn't installed, install it:
sudo apt install vim
🔴 The Most Important vim Command
If you ever get stuck in vim and just want to get out:
Press Esc, then type :q! and press Enter
This exits without saving. Memorize it before doing anything else.
🐧 vi vs vim
vi is the original editor from the 1970s. vim is the improved version with syntax highlighting, undo, plugins, and much more. On Ubuntu, the vi command usually launches a minimal vim. Install the full version with sudo apt install vim for the best experience.
vim Modes
vim's key concept: different modes for different actions. This is what makes it confusing at first — and powerful once you understand it.
(default)
Navigate, delete, copy"] -->|"i, a, o"| B["INSERT MODE
Type text normally"] B -->|"Esc"| A A -->|":"| C["COMMAND MODE
Save, quit, search/replace"] C -->|"Enter or Esc"| A A -->|"v, V, Ctrl+v"| D["VISUAL MODE
Select text"] D -->|"Esc"| A style A fill:#3b82f6,stroke:#2563eb,color:#fff style B fill:#22c55e,stroke:#166534,color:#fff style C fill:#f59e0b,stroke:#b45309,color:#fff style D fill:#6366f1,stroke:#4338ca,color:#fff
| Mode | How to Enter | What You Do | How to Leave |
|---|---|---|---|
| Normal | Start here (or press Esc) | Navigate, delete, copy, paste | — |
| Insert | i, a, o | Type text like a normal editor | Esc |
| Command | : | Save, quit, search/replace, settings | Enter or Esc |
| Visual | v or V | Select text | Esc |
⚠️ The Golden Rule
When in doubt, press Esc. This always returns you to Normal mode, where it's safe to figure out what to do next. If you're typing and nothing appears (or strange things happen), you're probably in Normal mode instead of Insert mode — press i to start inserting.
vim Essentials
Entering Insert Mode
| Key | Inserts Where |
|---|---|
| i | Before the cursor |
| a | After the cursor |
| I | Beginning of the line |
| A | End of the line |
| o | New line below |
| O | New line above |
Navigation (Normal Mode)
| Key | Movement |
|---|---|
| h j k l | Left, Down, Up, Right (arrow keys also work) |
| w | Forward one word |
| b | Back one word |
| 0 | Beginning of line |
| $ | End of line |
| gg | Beginning of file |
| G | End of file |
| Ctrl+d | Half page down |
| Ctrl+u | Half page up |
Editing (Normal Mode)
| Key | Action |
|---|---|
| x | Delete character under cursor |
| dd | Delete (cut) entire line |
| yy | Yank (copy) entire line |
| p | Paste after cursor |
| u | Undo |
| Ctrl+r | Redo |
| . | Repeat the last action |
Saving and Quitting (Command Mode)
| Command | Action |
|---|---|
:w | Save (write) |
:q | Quit (fails if unsaved changes) |
:wq | Save and quit |
:q! | Quit without saving (force) |
:wq! | Save and quit (force) |
:w newfile.txt | Save as a new filename |
💡 The vim "Language"
vim commands follow a verb + number + noun pattern:
d2w= delete 2 wordsy3j= yank (copy) 3 lines down (j)ci"= change inside "quotesdt.= delete until the next .
Once you learn the vocabulary, you can combine verbs and nouns in ways you've never been explicitly taught.
Search and Replace in vim
Searching
# Search forward (from Normal mode)
/search_term # then press Enter
# Search backward
?search_term
# Next match
n
# Previous match
N
# Turn off search highlighting
:noh
Search and Replace
# Replace first occurrence on current line
:s/old/new/
# Replace ALL occurrences on current line
:s/old/new/g
# Replace all occurrences in the ENTIRE file
:%s/old/new/g
# Replace with confirmation (y/n for each)
:%s/old/new/gc
# Case-insensitive replace
:%s/old/new/gi
💡 Remembering the Pattern
:%s/old/new/g breaks down as: % = entire file, s = substitute, /old/ = search for this, /new/ = replace with this, g = globally (all occurrences per line).
Choosing an Editor
| nano | vim | |
|---|---|---|
| Learning curve | Almost none | Steep at first |
| Speed (once learned) | Good | Excellent |
| Best for | Quick edits, config files | Heavy editing, coding |
| Philosophy | "Just works" like Notepad | "Speak to the editor" in commands |
| Available everywhere | Most distros | Every Unix/Linux system ever |
| Extensible | Minimal | Massively (plugins, themes, etc.) |
✅ Recommendation
Start with nano for everyday edits — it's simple and gets the job done. Learn enough vim to open, edit, save, and quit (this lesson covers that). Over time, if you find yourself editing files constantly, investing in vim proficiency pays off. Many developers use VS Code or another GUI editor day-to-day but keep vim skills for server work.
Setting Your Default Editor
# Set nano as the default editor for git, crontab, etc.
export EDITOR=nano
export VISUAL=nano
# Make it permanent — add those lines to:
nano ~/.bashrc
# Or set vim:
export EDITOR=vim
export VISUAL=vim
💡 vimtutor — Built-in vim Training
vim ships with an interactive tutorial. Just type vimtutor in your terminal and follow along. It takes about 30 minutes and covers everything in this lesson (and more) with hands-on practice.
Exercises
🏋️ Exercise 1: nano Basics
- Create a file:
nano practice.txt - Type a few lines of text
- Save with Ctrl+O, Enter
- Cut a line with Ctrl+K
- Paste it elsewhere with Ctrl+U
- Search for a word with Ctrl+W
- Exit with Ctrl+X
🏋️ Exercise 2: nano Search and Replace
# Create a test file
echo -e "Hello World\nHello Linux\nHello Terminal" > hello.txt
# Open in nano
nano hello.txt
# Press Ctrl+\ to search and replace
# Search: Hello
# Replace: Goodbye
# Press A to replace all
# Save and exit: Ctrl+O, Enter, Ctrl+X
🏋️ Exercise 3: vim Survival
Practice the essential vim workflow:
- Open:
vim practice_vim.txt - Press i to enter Insert mode
- Type: "This is my first vim file."
- Press Esc to return to Normal mode
- Type
:wqand press Enter to save and quit - Verify:
cat practice_vim.txt
🏋️ Exercise 4: vim Navigation and Editing
# Create a test file and open it
echo -e "Line one\nLine two\nLine three\nLine four\nLine five" > vimtest.txt
vim vimtest.txt
Inside vim (all in Normal mode unless noted):
- j j — move down to "Line three"
- dd — delete "Line three"
- p — paste it below (now it's after "Line four")
- u — undo (line goes back)
- gg — jump to top
- o — open new line below, type "Inserted line", press Esc
:wq— save and quit
🏋️ Exercise 5: vim Search and Replace
vim hello.txt
# In Normal mode, type:
# :%s/Goodbye/Hey/g
# Press Enter
# All "Goodbye" replaced with "Hey"
# :wq to save and quit
# Clean up
rm practice.txt hello.txt practice_vim.txt vimtest.txt
Knowledge Check
❓ Question 1
How do you save and exit nano?
❓ Question 2
In vim, what does pressing i do?
❓ Question 3
You're stuck in vim and want to quit without saving. What do you type?
❓ Question 4
In vim, what does dd do?
Quick Reference
nano Cheat Sheet
| Shortcut | Action |
|---|---|
| Ctrl+O | Save |
| Ctrl+X | Exit |
| Ctrl+K | Cut line |
| Ctrl+U | Paste line |
| Ctrl+W | Search |
| Ctrl+\ | Search & replace |
| Alt+U | Undo |
vim Cheat Sheet
| Key / Command | Action |
|---|---|
| i | Enter Insert mode |
| Esc | Return to Normal mode |
:w | Save |
:q | Quit |
:wq | Save and quit |
:q! | Quit without saving |
| dd | Delete line |
| yy | Copy line |
| p | Paste |
| u | Undo |
/text | Search |
:%s/old/new/g | Replace all |
Summary
🎉 Key Takeaways
nanois beginner-friendly — shortcuts are shown at the bottom of the screenvimis modal — Normal mode for commands, Insert mode for typing- In vim: i to type, Esc to stop typing,
:wqto save and quit - Both editors support search, replace, and syntax highlighting
vimtutoris the best way to learn vim — it's interactive and free- Set your preferred editor in
EDITORandVISUALenvironment variables
🍎 On macOS
Both nano and vim come pre-installed on macOS and work identically. vimtutor is also available. The EDITOR and VISUAL variables work the same way — set them in your ~/.zshrc file (Zsh) instead of ~/.bashrc (Bash) if you're using the Mac default shell.
🎉 Module 4 Complete!
You can now view, search, and edit files entirely from the command line. Combined with your file management and permissions skills, you have a solid toolkit for working on any Linux system.
🚀 What's Next?
Module 5 dives into package management — installing, updating, and removing software with apt, Snap, Flatpak, and more.