Text Editors: Nano and Vim
Summary
This chapter teaches you to edit files directly in the terminal using two popular editors. Nano provides a beginner-friendly interface with on-screen shortcuts, while Vim offers powerful modal editing for advanced users. You'll learn Vim's modes (insert, command, visual), navigation, search and replace, and configuration through vimrc.
Concepts Covered
This chapter covers the following 15 concepts from the learning graph:
- Text Editors
- Nano Editor
- Nano Commands
- Nano Shortcuts
- Vi Editor
- Vim Editor
- Vim Modes
- Vim Insert Mode
- Vim Command Mode
- Vim Visual Mode
- Vim Navigation
- Vim Save and Quit
- Vim Search Replace
- Vimrc Configuration
- Editor Selection
Prerequisites
This chapter builds on concepts from:
Why Edit in the Terminal?
"Wait, can't I just use VS Code or Notepad?" Great question! You absolutely can—but here's the thing: sometimes you're logged into a remote server with no graphical interface. Sometimes you need to quickly edit a config file while troubleshooting. Sometimes you just want to feel like a hacker from the movies (no judgment!).
Text editors that run in the terminal are essential tools for any Linux user. They work everywhere, even on minimal systems, and once you master them, you'll find yourself editing files faster than you ever thought possible.
We'll cover two editors:
- Nano: The friendly one. Perfect for beginners.
- Vim: The powerful one. Legendary among developers.
By the end of this chapter, you'll never be stuck unable to edit a file again!
The Nano Editor: Your Gentle Introduction
The Nano editor is like the training wheels of terminal text editors—and I mean that in the best way! It shows you exactly what keys to press right on the screen, so you're never lost.
Opening Files with Nano
1 2 3 4 5 6 7 8 9 10 11 | |
When nano opens, you'll see:
- Your file content in the main area
- A title bar at the top (filename, modified status)
- A help bar at the bottom with Nano shortcuts
Nano Shortcuts: The Bottom Bar Is Your Friend
See those ^G Get Help and ^X Exit at the bottom? The ^ symbol means Ctrl. So ^X means press Ctrl+X.
Here are the essential Nano commands:
| Shortcut | Action |
|---|---|
Ctrl+X |
Exit nano |
Ctrl+O |
Write Out (save file) |
Ctrl+S |
Save (same as Write Out) |
Ctrl+G |
Get Help |
Ctrl+K |
Cut current line |
Ctrl+U |
Uncut (paste) |
Ctrl+W |
Where Is (search) |
Ctrl+\ |
Search and Replace |
Ctrl+C |
Show cursor position |
Ctrl+_ |
Go to line number |
Basic Nano Workflow
1 2 3 4 5 6 7 8 9 10 | |
If you try to exit without saving, nano politely asks: "Save modified buffer?" Press Y for yes, N for no, or Ctrl+C to cancel.
Nano Navigation
| Shortcut | Action |
|---|---|
Ctrl+A |
Go to beginning of line |
Ctrl+E |
Go to end of line |
Ctrl+Y |
Page up |
Ctrl+V |
Page down |
Ctrl+_ |
Go to specific line |
Alt+\ |
Go to beginning of file |
Alt+/ |
Go to end of file |
Nano Search and Replace
1 2 3 4 5 6 7 8 | |
Nano Config Files
Nano can be customized through ~/.nanorc. You can enable syntax highlighting, line numbers, auto-indentation, and more:
1 2 3 4 5 | |
When to Use Nano
Nano is perfect for:
- Quick edits to config files
- Beginners learning terminal editing
- Situations where you need something "just works"
- When you don't want to remember complex commands
But once you get comfortable with the terminal, you might want something more powerful...
The Vi Editor: The Grandfather
The Vi editor is the ancestor of Vim. Created in 1976, it's been around longer than most programming languages! Vi (pronounced "vee-eye") is available on virtually every Unix and Linux system.
1 2 | |
Vi is minimal and can feel strange at first because it's a modal editor—more on that soon. Most modern systems install Vim instead of pure vi, but it's good to know vi exists as a fallback.
The Vim Editor: Vi Improved
The Vim editor (Vi IMproved) takes vi and supercharges it. Vim has been called "the programmer's editor" and for good reason—once mastered, it makes you incredibly productive.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
The Vim Learning Curve
Here's the honest truth: Vim has a reputation for being hard to learn. There's even a famous joke:
"How do you generate a random string? Put a first-year CS student in front of Vim and tell them to exit."
But here's the secret: Vim isn't hard, it's just different. Once you understand Vim modes, everything clicks.
Diagram: The Vim Learning Curve
Why Vim's Learning Curve is Worth It
Type: diagram
Bloom Taxonomy: Understand Learning Objective: Visualize how the initial time investment in learning Vim pays off with dramatically increased productivity.
Layout: Graph showing productivity over time
Visual elements: - X-axis: Time (days/weeks) - Y-axis: Productivity - Line 1 (blue): "Simple editor" - starts high, stays flat - Line 2 (green): "Vim" - starts low, dips lower, then climbs dramatically upward - Intersection point: "The breakthrough" - Area between lines after intersection: "Your productivity gains"
Key annotations: - Day 1: "Why can't I exit?!" - Week 1: "Okay, this is starting to make sense" - Week 2: "Wait, I can do THAT?" - Month 1: "I never want to go back" - Month 3: "I edit faster than I think"
Color scheme: - Simple editor line: Blue - Vim learning curve: Green - Frustration zone: Red shaded area - Productivity zone: Green shaded area
Implementation: p5.js
Vim Modes: The Key to Understanding Vim
Unlike most editors where you just start typing, Vim uses Vim modes. Think of modes like different "states" the editor can be in. Each mode has a different purpose.
The Three Main Modes
- Normal Mode (Command Mode): For navigation and commands
- Insert Mode: For typing text
- Visual Mode: For selecting text
When you open Vim, you're in Normal Mode (also called Vim Command Mode). This is where beginners get confused—they try to type and weird things happen!
| Mode | Purpose | How to Enter | How to Exit |
|---|---|---|---|
| Normal | Navigate, commands | Esc from any mode |
(default mode) |
| Insert | Type text | i, a, o, etc. |
Esc |
| Visual | Select text | v, V, Ctrl+v |
Esc |
The Golden Rule
When in doubt, press Esc. It always returns you to Normal mode!
Vim Insert Mode: Time to Type
Vim Insert Mode is where you actually type text, like a normal editor. There are several ways to enter Insert mode:
| Key | Action |
|---|---|
i |
Insert before cursor |
I |
Insert at beginning of line |
a |
Append after cursor |
A |
Append at end of line |
o |
Open new line below |
O |
Open new line above |
s |
Substitute character (delete char, enter insert) |
S |
Substitute line (delete line, enter insert) |
When you're in Insert mode, you'll see -- INSERT -- at the bottom of the screen.
1 2 3 4 5 6 | |
Vim Command Mode: The Control Center
Vim Command Mode (Normal Mode) is where you navigate and execute commands. Here's why it's powerful: you can edit text without moving your hands from the home row!
Vim Navigation: Moving Like a Pro
Forget the arrow keys! Vim navigation uses letter keys:
| Key | Movement |
|---|---|
h |
Left |
j |
Down |
k |
Up |
l |
Right |
"But why h, j, k, l?" On old terminals, these keys literally had arrows on them. Now it's a tradition—and it keeps your hands on the home row.
More navigation commands:
| Key | Movement |
|---|---|
w |
Next word |
b |
Back a word |
e |
End of word |
0 |
Beginning of line |
$ |
End of line |
^ |
First non-blank character |
gg |
Go to first line |
G |
Go to last line |
5G |
Go to line 5 |
Ctrl+f |
Page forward |
Ctrl+b |
Page backward |
% |
Jump to matching bracket |
Diagram: Vim Navigation Keys
Understanding hjkl Navigation
Type: diagram
Bloom Taxonomy: Remember, Apply Learning Objective: Visualize the hjkl key positions and their movement directions to build muscle memory.
Layout: Keyboard-style display with arrow indicators
Visual elements: - Four keys arranged like on keyboard: h j k l - Arrows showing direction of movement: - h: Left arrow - j: Down arrow - k: Up arrow - l: Right arrow - Hand position overlay showing fingers on home row - Animation: cursor moving on sample text as keys are "pressed"
Key teaching points: - j looks like a down arrow (hook goes down) - k is for "kick up" - h is leftmost, l is rightmost - Fingers stay on home row
Interactive features: - Click/press keys to see cursor move - Sample text area showing movement - Speed challenge mode
Color scheme: - Keys: Gray with white letters - Active key: Blue highlight - Direction arrows: Green - Cursor: Orange
Implementation: p5.js
Vim Visual Mode: Selecting Text
Vim Visual Mode lets you select text for copying, deleting, or changing:
| Key | Mode |
|---|---|
v |
Character-wise visual |
V |
Line-wise visual |
Ctrl+v |
Block visual (columns!) |
Once in visual mode:
- Use navigation keys to extend selection
yto yank (copy)dto deletecto change (delete and enter Insert)>to indent<to unindent
1 2 3 4 | |
Block visual mode (Ctrl+v) is magical—you can select and edit columns of text!
Essential Editing Commands
In Normal mode, you have powerful editing commands:
| Command | Action |
|---|---|
x |
Delete character under cursor |
dd |
Delete entire line |
dw |
Delete word |
d$ |
Delete to end of line |
yy |
Yank (copy) line |
yw |
Yank word |
p |
Paste after cursor |
P |
Paste before cursor |
u |
Undo |
Ctrl+r |
Redo |
. |
Repeat last command |
The commands are composable! You can combine numbers and motions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
The Vim Grammar
Vim commands follow a grammar: [count][operator][motion]
d= delete,y= yank,c= changew= word,$= end of line,gg= file start3dw= delete 3 wordsd5j= delete 5 lines down
Vim Save and Quit: The Famous Commands
Vim save and quit commands all start with : (colon), which opens the command line at the bottom:
| Command | Action |
|---|---|
:w |
Write (save) file |
:q |
Quit (fails if unsaved changes) |
:wq |
Write and quit |
:x |
Write and quit (same as :wq) |
ZZ |
Write and quit (Normal mode) |
:q! |
Quit without saving (force) |
:wq! |
Write and quit (force) |
:w filename |
Save as new filename |
:e filename |
Edit a different file |
So when people joke about not being able to exit Vim:
1 2 3 4 | |
Vim Search Replace: Find and Transform
Vim search replace is powerful and uses regular expressions!
Searching
| Command | Action |
|---|---|
/pattern |
Search forward |
?pattern |
Search backward |
n |
Next match |
N |
Previous match |
* |
Search word under cursor (forward) |
# |
Search word under cursor (backward) |
1 2 3 | |
Search and Replace
The substitute command syntax: :[range]s/old/new/[flags]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
The % Symbol
In Vim, % means "the entire file." So :%s/old/new/g means "substitute in the entire file."
Vimrc Configuration: Make Vim Your Own
The vimrc configuration file (~/.vimrc) lets you customize Vim to your liking. Here's a sensible starter configuration:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | |
To use this configuration:
1 2 3 4 5 6 7 | |
Vimrc Tips
1 2 3 4 5 6 7 8 9 10 11 12 | |
Learn Vimtutor First
Vim comes with a built-in tutorial! Just type vimtutor in your terminal. It takes about 30 minutes and teaches all the basics interactively.
The Great Editor War: Vi vs Emacs
Ah, the Editor War. If you haven't heard of it, buckle up—this is one of the longest-running, nerdiest feuds in computing history. It makes Marvel vs DC look like a polite disagreement about tea preferences.
The Combatants
In the red corner: Vi (and later Vim), the modal editor we just learned about. Lightweight, fast, and available on every Unix system since the dawn of time.
In the blue corner: Emacs, created by Richard Stallman (yes, THE Richard Stallman who started the Free Software movement). Emacs is... well, it's not just an editor. It's been described as:
- "A great operating system, lacking only a decent editor"
- "An editor that happens to contain a Lisp interpreter, mail client, news reader, web browser, calendar, psychiatrist (really!), and several games"
- "Eight Megabytes And Constantly Swapping" (a joke from when 8MB was a LOT of memory)
The Arguments (All In Good Fun)
Vi users said:
- "My editor loads before you can blink!"
- "I can edit files over a 300 baud modem connection!"
- "Real programmers don't need a kitchen sink in their editor!"
- "Emacs users have Emacs pinky from all those Ctrl combinations!"
Emacs users said:
- "I never have to leave my editor—ever. For anything."
- "Vi users are afraid of Lisp!"
- "Modal editing is like having to shift gears in a car. In 2024."
- "My editor can read email, browse the web, AND make coffee!" (That last one might be an exaggeration. Might.)
The Flame Wars
The Editor War produced some legendary internet flame wars in the 1980s and 1990s. Newsgroups burned with passionate debates. Friendships were tested. Marriages... were probably fine, actually, but the internet drama was REAL.
There was even a mock religious war, with vi users forming the "Cult of vi" and Emacs users practicing the "Church of Emacs" (complete with St. IGNUcius, Stallman's alter ego, blessing computers).
The Verdict: Vi Won (Sort Of)
Here's the thing: Vi won the war, but not by defeating Emacs—by outlasting the debate. Here's why:
-
Vi/Vim is everywhere: Every Linux system, every Mac, every BSD, every server you'll ever SSH into has vi. It's the cockroach of text editors (and I mean that lovingly).
-
The keybindings spread: Vim keybindings are now available in:
- VS Code (Vim extension)
- JetBrains IDEs (IdeaVim)
- Sublime Text
- Atom (RIP)
- Web browsers (Vimium)
-
Even Emacs! (Evil mode—yes, really)
-
Modern AI tools use Vim: Tools like Claude Code let you jump straight into Vim with a single
Ctrl+Gcommand. When AI assistants default to Vim, you know which side history chose. -
Muscle memory: Once you learn hjkl navigation, you want it everywhere. It's like learning to touch type—you can't go back.
But Emacs Is Still Amazing
Let's be fair: Emacs is still beloved by many developers, especially in the Lisp, Clojure, and functional programming communities. Org-mode (Emacs's organizational system) is genuinely incredible. And Emacs users are some of the most passionate, dedicated programmers you'll ever meet.
The real winner of the Editor War? Everyone who had fun arguing about it.
The True Lesson
The Editor War taught us something important: programmers are REALLY passionate about their tools. And that's okay! Use what makes you productive. Just be prepared to defend your choice at parties. (Kidding. Mostly.)
Modern Peace Treaty
These days, most developers just... use both? Or neither! VS Code has largely taken over as the "default" editor for many new programmers. But here's the secret: VS Code's Vim extension is one of its most popular extensions.
So whether you're a vi purist, an Emacs evangelist, or a VS Code convert with Vim keybindings, we're all just trying to edit text efficiently. And now you know the history of why your senior developer gets a twinkle in their eye when someone mentions "the one true editor."
1 2 3 4 5 6 | |
Editor Selection: Choosing Your Weapon
So, which editor should YOU use? Here's the honest editor selection guide:
Choose Nano If...
- You're just starting with Linux
- You need to make quick edits occasionally
- You want something that "just works"
- You don't edit files in the terminal frequently
Choose Vim If...
- You edit files in the terminal frequently
- You write code or edit config files daily
- You want to become extremely productive
- You enjoy mastering powerful tools
- You work on remote servers often
The Middle Path: Vim with Training Wheels
You don't have to go all-in immediately! Many editors support "Vim mode":
- VS Code has a Vim extension
- JetBrains IDEs have IdeaVim
- Even web browsers have Vim-like navigation (Vimium)
This lets you learn Vim keybindings while keeping your familiar editor.
Comparison Table
| Feature | Nano | Vim |
|---|---|---|
| Learning curve | Gentle | Steep (then rewarding) |
| On-screen help | Yes | No (but :help is excellent) |
| Modes | No | Yes |
| Extensibility | Limited | Extensive |
| Speed once learned | Good | Excellent |
| Available everywhere | Usually | Almost always |
| Best for | Quick edits | Daily use |
MicroSim: Interactive Vim Mode Practice
Practice Vim Modes
Type: microsim
Bloom Taxonomy: Apply, Analyze Learning Objective: Allow students to practice switching between Vim modes and see how input is interpreted differently in each mode.
Canvas layout (responsive, ~750px max width): - Top section (60px): Current mode indicator (NORMAL, INSERT, VISUAL) - Middle section (300px): Text editing area with cursor - Bottom section (120px): Key input display and mode explanation
Visual elements: - Mode indicator: Large, color-coded badge - Normal: Blue background - Insert: Green background - Visual: Purple background - Text area: Monospace font, with visible cursor - Keystroke display: Shows what keys were pressed - Action log: Shows what each keystroke did
Interactive controls: - Full keyboard input capture - Text area responds to Vim commands - Mode switches with i, v, Esc
Behavior: - Start in Normal mode - Press i: Enter Insert mode, text input works - Press Esc: Return to Normal mode - Press v: Enter Visual mode, movement selects - hjkl: Move cursor in Normal/Visual - dd: Delete line in Normal - yy: Yank line in Normal - p: Paste in Normal
Sample starting text:
1 2 3 4 5 | |
Challenge modes: 1. "Type your name" - switch to Insert, type, switch back 2. "Delete a line" - use dd 3. "Copy and paste" - use yy and p 4. "Select and delete" - use V, j, d
Color scheme: - Normal mode: Blue (#4a9eff) - Insert mode: Green (#4ade80) - Visual mode: Purple (#a78bfa) - Text: White on dark background - Cursor: Orange blink
Implementation: p5.js
Vim Commands Cheat Sheet
Mode Switching
1 2 3 4 5 6 7 | |
Navigation
1 2 3 4 5 | |
Editing
1 2 3 4 5 6 7 8 | |
Save/Quit
1 2 3 4 5 | |
Search
1 2 3 4 | |
Key Takeaways
You've learned the essential terminal text editors!
- Nano: Beginner-friendly with on-screen shortcuts
- Vim: Modal editor that's incredibly powerful once mastered
- Vim Modes: Normal (commands), Insert (typing), Visual (selecting)
- Navigation: hjkl keys keep hands on home row
- Commands: Composable grammar (count + operator + motion)
- Configuration: ~/.vimrc customizes your Vim experience
You're an Editor Wizard Now!
Whether you stick with Nano for simple edits or dive deep into Vim mastery, you now have the skills to edit any file on any Linux system. No more being stuck unable to edit a config file on a remote server!
What's Next?
Now that you can edit files like a pro, it's time to start writing your own programs—shell scripts! The next chapter introduces Bash scripting, where you'll automate tasks and combine everything you've learned.
Quick Quiz: Nano and Vim
- What key do you press to save and exit in Nano?
- What does pressing
Escdo in Vim? - How do you enter Insert mode in Vim?
- What command searches and replaces all occurrences in a file?
- What file contains your Vim configuration?
- What does
dddo in Vim's Normal mode?
Quiz Answers
Ctrl+X(then Y to confirm save)- Returns you to Normal mode from any other mode
- Press
i(ora,o,I,A,O) :%s/old/new/g~/.vimrc- Deletes the entire current line
References
- Vim Official Documentation - Complete official reference for Vim including user manual, reference guide, and FAQ.
- Vimtutor - Interactive Tutorial - Guide to using the built-in vimtutor command for hands-on Vim learning.
- OpenVim Interactive Tutorial - Browser-based interactive Vim tutorial for practicing commands without installation.
- Nano Editor Guide - Official Nano documentation covering all commands and configuration options.
- Vim Adventures - Learn Vim Through Gaming - Fun game-based approach to learning Vim navigation and commands.
- Vi and Vim Editors: Power and Agility - O'Reilly book covering vi and Vim from basics to advanced topics.
- A Vim Guide for Beginners - Comprehensive beginner-friendly guide explaining Vim's philosophy and core concepts.
- Vim Cheat Sheet - Visual cheat sheet with all essential Vim commands organized by category.
- Mastering the Vim Language - Tutorial explaining Vim's grammar of operators, motions, and text objects.
- Nano Tutorial for Beginners - Linuxize guide to Nano basics, shortcuts, and configuration.
- Practical Vim: Edit Text at the Speed of Thought - Book focusing on efficient Vim workflows and productivity tips.
- Vim for Programmers - Article explaining why Vim's modal editing is powerful for coding.
- Customizing Nano with Syntax Highlighting - Guide to configuring Nano's appearance and enabling syntax highlighting.
- Vim Regex Tutorial - Learn Vim guide to search and replace with regular expressions.
- The Vi/Vim Editor History - Wikipedia article covering the history and evolution from vi to Vim.
- Vim Plugins for Beginners - Introduction to extending Vim with plugins using vim-plug or Vundle.
- Nano vs Vim vs Emacs - It's FOSS comparison of popular Linux text editors with pros and cons.
- Vim Golf - Code in Fewest Keystrokes - Challenge site for practicing efficient Vim commands through puzzles.
- Vim for VS Code Users - Guide to using the Vim extension in Visual Studio Code.
- Learn Vim Progressively - Step-by-step approach to learning Vim gradually without overwhelming beginners.