Skip to main content

✏️ 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
  • Everywherenano and vim are 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+OSave (Write Out) — press Enter to confirm filename
Ctrl+XExit — prompts to save if modified
Ctrl+KCut (delete) the current line
Ctrl+UPaste (uncut) the last cut line
Ctrl+WSearch for text
Ctrl+\Search and replace
Ctrl+GShow help
Ctrl+_Go to a specific line number
Alt+UUndo
Alt+ERedo

✅ 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.

graph TD A["NORMAL MODE
(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
iBefore the cursor
aAfter the cursor
IBeginning of the line
AEnd of the line
oNew line below
ONew line above

Navigation (Normal Mode)

Key Movement
h j k lLeft, Down, Up, Right (arrow keys also work)
wForward one word
bBack one word
0Beginning of line
$End of line
ggBeginning of file
GEnd of file
Ctrl+dHalf page down
Ctrl+uHalf page up

Editing (Normal Mode)

Key Action
xDelete character under cursor
ddDelete (cut) entire line
yyYank (copy) entire line
pPaste after cursor
uUndo
Ctrl+rRedo
.Repeat the last action

Saving and Quitting (Command Mode)

Command Action
:wSave (write)
:qQuit (fails if unsaved changes)
:wqSave and quit
:q!Quit without saving (force)
:wq!Save and quit (force)
:w newfile.txtSave as a new filename

💡 The vim "Language"

vim commands follow a verb + number + noun pattern:

  • d2w = delete 2 words
  • y3j = yank (copy) 3 lines down (j)
  • ci" = change inside "quotes
  • dt. = delete until the next .

Once you learn the vocabulary, you can combine verbs and nouns in ways you've never been explicitly taught.

Choosing an Editor

nano vim
Learning curveAlmost noneSteep at first
Speed (once learned)GoodExcellent
Best forQuick edits, config filesHeavy editing, coding
Philosophy"Just works" like Notepad"Speak to the editor" in commands
Available everywhereMost distrosEvery Unix/Linux system ever
ExtensibleMinimalMassively (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

  1. Create a file: nano practice.txt
  2. Type a few lines of text
  3. Save with Ctrl+O, Enter
  4. Cut a line with Ctrl+K
  5. Paste it elsewhere with Ctrl+U
  6. Search for a word with Ctrl+W
  7. 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:

  1. Open: vim practice_vim.txt
  2. Press i to enter Insert mode
  3. Type: "This is my first vim file."
  4. Press Esc to return to Normal mode
  5. Type :wq and press Enter to save and quit
  6. 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):

  1. j j — move down to "Line three"
  2. dd — delete "Line three"
  3. p — paste it below (now it's after "Line four")
  4. u — undo (line goes back)
  5. gg — jump to top
  6. o — open new line below, type "Inserted line", press Esc
  7. :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

ShortcutAction
Ctrl+OSave
Ctrl+XExit
Ctrl+KCut line
Ctrl+UPaste line
Ctrl+WSearch
Ctrl+\Search & replace
Alt+UUndo

vim Cheat Sheet

Key / CommandAction
iEnter Insert mode
EscReturn to Normal mode
:wSave
:qQuit
:wqSave and quit
:q!Quit without saving
ddDelete line
yyCopy line
pPaste
uUndo
/textSearch
:%s/old/new/gReplace all

Summary

🎉 Key Takeaways

  • nano is beginner-friendly — shortcuts are shown at the bottom of the screen
  • vim is modal — Normal mode for commands, Insert mode for typing
  • In vim: i to type, Esc to stop typing, :wq to save and quit
  • Both editors support search, replace, and syntax highlighting
  • vimtutor is the best way to learn vim — it's interactive and free
  • Set your preferred editor in EDITOR and VISUAL environment 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.