LFCS 14 🐧 Editing Files with nano
A system administrator edits configuration files. The edit is often a single line, a value, a directive. The editor should be simple, available, and fast. The nano is the editor that meets the criteria. It is installed by default on most distributions, it displays the keyboard shortcuts on the screen, and it does not require the modal knowledge that vi and vim require. The LFCS exam expects the basic editing to be automatic, and the nano is the editor that the exam environment provides.
Key point: nano file opens the file for editing. The keyboard shortcuts are displayed at the bottom of the screen: ^ means Ctrl, and the letter follows. Ctrl+O saves, Ctrl+X exits, Ctrl+W searches, Ctrl+K cuts the line, Ctrl+U pastes the line, and Ctrl+G opens the help. The nano is the modeless editor: the typed characters are inserted at the cursor, and the shortcuts are the commands. There is no insert mode and no command mode.
Why nano matters
The simplicity problem. The vi and the vim are the powerful editors, but they are modal. The new user types the text and the editor interprets the characters as the commands. The nano is the modeless editor: the typed characters are the text, and the shortcuts are the commands. The simplicity is the reason the nano is the default editor on many distributions.
The availability problem. The nano is installed by default on the Debian, the Ubuntu, the RHEL, and the Fedora. The vi is also installed, but the nano is the one that the new administrator can use without the training. The LFCS exam environment provides the nano.
The shortcut display problem. The nano displays the shortcuts at the bottom of the screen. The user does not need to memorize them. The ^G opens the full help. The display is the reason the nano is the friendly editor.
The configuration problem. The nano is the editor for the /etc/ files, the systemd unit files, the crontab, and the many other configuration files. The edit is often the single line, and the nano is the tool for the quick change.
The exam problem. The LFCS tasks often require editing the configuration files. The nano is the editor that the exam expects. The alternative is the vi, and the tasks can be done with either. The nano is the safer choice for the candidates who are not fluent in the vi.
a. The basic editing and the shortcuts
The nano opens the file for editing.
$ nano file.txt
The screen shows the file’s content, the cursor, the status line, and the shortcuts at the bottom. The typed characters are inserted at the cursor.
The shortcuts at the bottom are the essential commands.
| Shortcut | Action |
|---|---|
Ctrl+O | Save (Write Out) |
Ctrl+X | Exit |
Ctrl+W | Search |
Ctrl+K | Cut the line |
Ctrl+U | Paste the line |
Ctrl+G | Help |
Ctrl+C | Cancel the current operation |
Ctrl+\ | Search and replace |
The ^ is the Ctrl key. The M- is the Alt key.
The save is the Ctrl+O. The nano asks for the file name and confirms with the Enter. The Ctrl+X exits, and if the file has the unsaved changes, the nano asks to save.
$ nano file.txt
# Edit the file
# Ctrl+O, Enter to save
# Ctrl+X to exit
The nano with the new file.
$ nano newfile.txt
The newfile.txt does not exist. The nano opens the empty buffer with the name. The save creates the file.
The nano with the read-only.
$ nano -v file.txt
The -v is the view mode. The file is opened read-only. The edit is disabled.
The nano with the line numbers.
$ nano -l file.txt
The -l shows the line numbers on the left. The -c shows the cursor position. The -i enables the auto-indent.
The nano with the specific line.
$ nano +10 file.txt
The file opens with the cursor at the line 10. The + is the line number. The +/pattern opens at the first match.
$ nano +/error file.txt
The file opens at the first line that contains the “error”. The pattern is the search.
b. The search and the replace
The Ctrl+W opens the search prompt.
$ nano file.txt
# Ctrl+W
# Type the search term, Enter
# The cursor moves to the first match
# Ctrl+W again, Enter to find the next
The search is the forward search. The Ctrl+W then the Enter finds the next match. The Alt+W is the alternative.
The search and the replace is the Ctrl+\.
$ nano file.txt
# Ctrl+\
# Type the search term, Enter
# Type the replacement, Enter
# The nano asks: Replace this instance? (Y/N/A)
# Y for yes, N for no, A for all
The A replaces all the instances. The Y replaces the one and moves to the next. The N skips the one. The Ctrl+C cancels the replace.
The search is case-sensitive by default. The Alt+C toggles the case sensitivity. The Alt+R toggles the regular expression search.
The Ctrl+W with the Alt+B searches backward.
The search with the nano‘s internal.
$ nano file.txt
# Ctrl+W, type "error", Enter
# The cursor moves to the first "error"
# Ctrl+W, Enter to find the next
The pattern is the tool for the configuration files where the specific directive should be found and the edited.
The nano with the grep for the line number.
$ grep -n "error" file.txt
10:error: something failed
The grep -n shows the line number. The nano +10 file.txt opens at the line. The combination is the tool for the large file where the search should be done with the grep and the edit with the nano.
c. The configuration and the alternatives
The nano is configured in the ~/.nanorc and the /etc/nanorc.
# ~/.nanorc
set autoindent
set linenumbers
set tabsize 4
set mouse
The set autoindent enables the auto-indent. The set linenumbers shows the line numbers. The set tabsize 4 sets the tab width. The set mouse enables the mouse support.
The nano with the syntax highlighting.
# ~/.nanorc
include "/usr/share/nano/*.nanorc"
The include loads the syntax definitions for the many file types. The nano highlights the syntax for the Python, the shell, the JSON, and the many others.
The nano with the EDITOR variable.
$ export EDITOR=nano
The EDITOR is the variable that the many commands use. The crontab -e opens the nano if the EDITOR is the nano. The visudo opens the vi by default, and the EDITOR may not change it.
The nano with the select-editor.
$ select-editor
The command is the Debian and the Ubuntu tool that sets the default editor for the user. The update-alternatives is the alternative.
The nano is the default editor on the many distributions. The vi is the alternative. The choice is the preference. The nano for the simplicity, the vi for the power.
The nano and the vi comparison.
| Aspect | nano | vi |
|---|---|---|
| Mode | Modeless | Modal |
| Learning curve | Easy | Steep |
| Shortcuts | On screen | Memorized |
| Power | Basic | Advanced |
| Availability | Default | Default |
| Use | Quick edits | Complex editing |
The nano with the -B option.
$ nano -B file.txt
The -B creates the backup of the file before the save. The backup is the file.txt~. The pattern is the safety for the configuration edits.
The nano with the -m option.
$ nano -m file.txt
The -m enables the mouse support. The click moves the cursor. The pattern is the convenience for the users who prefer the mouse.
The nano with the -S option.
$ nano -S file.txt
The -S is the smooth scroll. The screen scrolls one line at a time. The default scrolls the half-screen.
The nano with the -z option.
$ nano -z file.txt
The -z enables the suspend. The Ctrl+Z suspends the nano, and the fg resumes it. The option is for the cases where the shell should be used in the middle of the edit.
Complete Example Session
# ============================================
# PART 1: OPEN AND EDIT
# ============================================
nano file.txt
# Edit, Ctrl+O to save, Ctrl+X to exit
# ============================================
# PART 2: CREATE NEW FILE
# ============================================
nano newfile.txt
# Type the content, Ctrl+O, Enter, Ctrl+X
# ============================================
# PART 3: SEARCH
# ============================================
nano file.txt
# Ctrl+W, type "error", Enter
# Ctrl+W, Enter for the next
# ============================================
# PART 4: SEARCH AND REPLACE
# ============================================
nano file.txt
# Ctrl+\, type the search, Enter
# Type the replacement, Enter
# Y/N/A
# ============================================
# PART 5: LINE NUMBERS
# ============================================
nano -l file.txt
# ============================================
# PART 6: OPEN AT LINE
# ============================================
nano +10 file.txt
nano +/error file.txt
# ============================================
# PART 7: READ-ONLY
# ============================================
nano -v file.txt
# ============================================
# PART 8: BACKUP
# ============================================
nano -B /etc/config.conf
# Creates /etc/config.conf~
# ============================================
# PART 9: CONFIGURE
# ============================================
cat >> ~/.nanorc << EOF
set autoindent
set linenumbers
set tabsize 4
EOF
# ============================================
# PART 10: SET DEFAULT EDITOR
# ============================================
export EDITOR=nano
crontab -e
The ten parts covered the open and edit, the create new, the search, the search and replace, the line numbers, the open at line, the read-only, the backup, the configure, and the set the default editor.
Quick Reference
Essential Shortcuts
| Shortcut | Action |
|---|---|
Ctrl+O | Save |
Ctrl+X | Exit |
Ctrl+W | Search |
Ctrl+\ | Search and replace |
Ctrl+K | Cut the line |
Ctrl+U | Paste the line |
Ctrl+G | Help |
Ctrl+C | Cancel |
Navigation
| Shortcut | Action |
|---|---|
Ctrl+A | Beginning of line |
Ctrl+E | End of line |
Ctrl+Y | Page up |
Ctrl+V | Page down |
Alt+\ | Beginning of file |
Alt+/ | End of file |
Command-Line Options
| Option | Effect |
|---|---|
nano file | Open the file |
nano +N file | Open at the line N |
nano +/pattern file | Open at the first match |
nano -l | Line numbers |
nano -v | Read-only |
nano -B | Backup on save |
nano -m | Mouse support |
nano -S | Smooth scroll |
Configuration
| Setting | Effect |
|---|---|
set autoindent | Auto-indent |
set linenumbers | Line numbers |
set tabsize 4 | Tab width |
set mouse | Mouse support |
include | Syntax highlighting |
nano vs vi
| Aspect | nano | vi |
|---|---|---|
| Mode | Modeless | Modal |
| Learning | Easy | Steep |
| Shortcuts | On screen | Memorized |
| Use | Quick edits | Complex |
Best Practices
✅ Do This:
# Use nano for the quick config edits
nano /etc/hosts # ✅
# Save with Ctrl+O and exit with Ctrl+X
# (the nano asks if the file is modified) # ✅
# Use the line numbers for the navigation
nano -l file.txt # ✅
# Open at the line from the grep
grep -n "error" file.txt
nano +10 file.txt # ✅
# Use the backup for the important files
nano -B /etc/config.conf # ✅
# Set the default editor
export EDITOR=nano # ✅
# Enable the syntax highlighting
echo 'include "/usr/share/nano/*.nanorc"' >> ~/.nanorc # ✅
❌ Don’t Do This:
# Don't forget to save before the exit
# Ctrl+X without Ctrl+O discards the changes # ⚠️
# Don't use the nano for the massive files
nano /var/log/syslog # use less # ⚠️
# Don't forget the backup for the critical files
nano /etc/fstab # no backup # ⚠️
# Don't assume the nano is on every system
# The minimal containers may lack it # ⚠️
# Don't forget the .nanorc for the syntax
# The default has no highlighting # ⚠️
# Don't use the nano for the binary files
nano /bin/ls # garbage # ❌
# Don't ignore the status line
# The errors appear there # ⚠️
Common Pitfalls
| Pitfall | Why It Happens | Fix |
|---|---|---|
| Changes lost | Ctrl+X without save | Save with Ctrl+O |
| Wrong file | Wrong name | Check the status line |
| Search not found | Case-sensitive | Alt+C toggles |
| Permission denied | No write access | Use sudo nano |
| No syntax highlight | No .nanorc | Include the definitions |
| Backup missing | No -B | Use the option |
| Tab width wrong | The default 8 | set tabsize 4 |
Real-World Examples
1. Edit a Config
sudo nano /etc/hosts
2. Create a File
nano script.sh
3. Search
nano file.txt
# Ctrl+W, type the term, Enter
4. Search and Replace
nano file.txt
# Ctrl+\, search, Enter, replace, Enter
# A for all
5. Open at a Line
nano +50 file.txt
6. Read-Only
nano -v /etc/passwd
7. With the Backup
nano -B /etc/nginx/nginx.conf
8. Set the Default Editor
export EDITOR=nano
9. Edit the Crontab
EDITOR=nano crontab -e
10. Edit the Sudoers
sudo EDITOR=nano visudo
Visual
nano Screen
┌─────────────────────────────────────────────────────────────┐
│ GNU nano 7.2 file.txt │
│ │
│ line 1 content │
│ line 2 content │
│ line 3 content │
│ _ │
│ │
│ │
│ │
│ ^O Write Out ^W Where Is ^K Cut ^T Execute ^C Location │
│ ^X Exit ^R Read File ^U Paste ^J Justify ^/ Replace │
│ │
└─────────────────────────────────────────────────────────────┘
The Save and Exit Flow
┌─────────────────────────────────────────────────────────────┐
│ Edit the file │
│ │ │
│ ▼ │
│ Ctrl+O (Write Out) │
│ │ │
│ ▼ │
│ File Name to Write: file.txt │
│ │ │
│ ▼ │
│ Enter (confirm) │
│ │ │
│ ▼ │
│ [ Wrote 5 lines ] │
│ │ │
│ ▼ │
│ Ctrl+X (Exit) │
│ │ │
│ ▼ │
│ Back to the shell │
│ │
└─────────────────────────────────────────────────────────────┘
Search and Replace
┌─────────────────────────────────────────────────────────────┐
│ Ctrl+\ │
│ │ │
│ ▼ │
│ Search (to replace): error │
│ │ │
│ ▼ │
│ Replace with: warning │
│ │ │
│ ▼ │
│ Replace this instance? (Y/N/A) │
│ │ │
│ ├── Y ──▶ Replace and move to next │
│ │ │
│ ├── N ──▶ Skip and move to next │
│ │ │
│ └── A ──▶ Replace all and stop │
│ │
└─────────────────────────────────────────────────────────────┘
nano vs vi
┌─────────────────────────────────────────────────────────────┐
│ NANO │
│ │
│ Type the text, it appears. │
│ The shortcuts are on the screen. │
│ The save is Ctrl+O, the exit is Ctrl+X. │
│ │
├─────────────────────────────────────────────────────────────┤
│ │
│ VI │
│ │
│ Press i to insert, then type. │
│ Press Esc to leave the insert mode. │
│ Type :w to save, :q to quit. │
│ │
│ The modes are the power and the learning curve. │
│ │
└─────────────────────────────────────────────────────────────┘
Summary
| Item | Value |
|---|---|
| Open | nano file |
| Save | Ctrl+O, Enter |
| Exit | Ctrl+X |
| Search | Ctrl+W |
| Replace | Ctrl+\ |
| Cut the line | Ctrl+K |
| Paste the line | Ctrl+U |
| Help | Ctrl+G |
| Open at line | nano +N file |
| Read-only | nano -v |
| Backup | nano -B |
| Config | ~/.nanorc |
| Default editor | EDITOR=nano |
Key takeaways:
- The
nanois the modeless editor. The typed characters are the text, and the shortcuts are the commands. There is no insert mode and no command mode. The simplicity is the reason thenanois the default editor on many distributions. - The shortcuts are displayed at the bottom of the screen. The
^is theCtrlkey. TheCtrl+Osaves, theCtrl+Xexits, theCtrl+Wsearches, theCtrl+\replaces, theCtrl+Kcuts, theCtrl+Upastes, and theCtrl+Gopens the help. - The save is the
Ctrl+O, and the exit is theCtrl+X. TheCtrl+Xwith the unsaved changes asks to save. TheCtrl+Xwithout the save discards the changes. - **The search is the
Ctrl+Wand the replace is theCtrl+\.** The search is case-sensitive by default, and theAlt+Ctoggles the case. The replace offers the Y/N/A options. - The
nano +N fileopens at the line N. Thenano +/pattern fileopens at the first match. The combination with thegrep -nis the tool for the large file. - The
nano -Bcreates the backup on the save. The backup is thefile~. The option is the safety for the configuration edits. - The
~/.nanorcconfigures the nano. Theset linenumbers, theset autoindent, theset tabsize, and the syntax highlighting. The configuration is per-user and system-wide.
Remember: The nano is the editor for the quick configuration changes. The LFCS tasks often require editing the files, and the nano is the editor the exam environment provides. The shortcuts are on the screen, the save is the Ctrl+O, and the exit is the Ctrl+X. The search and the replace are the Ctrl+W and the Ctrl+\. The nano is the simple editor, and the simplicity is the reason it is the default on many distributions. The vi is the powerful alternative, and the choice is the preference and the task.
Stop using slow, ad-bloated tool sites! 🤮
🔎 Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
✅ Finance (Mortgage, Interest, Inflation)
✅ Tech (Base64, JSON, Dev Suite, IP)
✅ Health (BMI, BMR, TDEE)
✅ Productivity (Timer, Workspace, QR)
⚡️ Fast & Private
🔒 No data leaves your device
💎 100% Free
🔗 Use it now: https://tools.kandz.me
🔖 Bookmark it—you’ll need it later!