| |

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.

ShortcutAction
Ctrl+OSave (Write Out)
Ctrl+XExit
Ctrl+WSearch
Ctrl+KCut the line
Ctrl+UPaste the line
Ctrl+GHelp
Ctrl+CCancel 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.

Aspectnanovi
ModeModelessModal
Learning curveEasySteep
ShortcutsOn screenMemorized
PowerBasicAdvanced
AvailabilityDefaultDefault
UseQuick editsComplex 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

ShortcutAction
Ctrl+OSave
Ctrl+XExit
Ctrl+WSearch
Ctrl+\Search and replace
Ctrl+KCut the line
Ctrl+UPaste the line
Ctrl+GHelp
Ctrl+CCancel

Navigation

ShortcutAction
Ctrl+ABeginning of line
Ctrl+EEnd of line
Ctrl+YPage up
Ctrl+VPage down
Alt+\Beginning of file
Alt+/End of file

Command-Line Options

OptionEffect
nano fileOpen the file
nano +N fileOpen at the line N
nano +/pattern fileOpen at the first match
nano -lLine numbers
nano -vRead-only
nano -BBackup on save
nano -mMouse support
nano -SSmooth scroll

Configuration

SettingEffect
set autoindentAuto-indent
set linenumbersLine numbers
set tabsize 4Tab width
set mouseMouse support
includeSyntax highlighting

nano vs vi

Aspectnanovi
ModeModelessModal
LearningEasySteep
ShortcutsOn screenMemorized
UseQuick editsComplex

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

PitfallWhy It HappensFix
Changes lostCtrl+X without saveSave with Ctrl+O
Wrong fileWrong nameCheck the status line
Search not foundCase-sensitiveAlt+C toggles
Permission deniedNo write accessUse sudo nano
No syntax highlightNo .nanorcInclude the definitions
Backup missingNo -BUse the option
Tab width wrongThe default 8set 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

ItemValue
Opennano file
SaveCtrl+O, Enter
ExitCtrl+X
SearchCtrl+W
ReplaceCtrl+\
Cut the lineCtrl+K
Paste the lineCtrl+U
HelpCtrl+G
Open at linenano +N file
Read-onlynano -v
Backupnano -B
Config~/.nanorc
Default editorEDITOR=nano

Key takeaways:

  • The nano is 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 the nano is the default editor on many distributions.
  • The shortcuts are displayed at the bottom of the screen. The ^ is the Ctrl key. The Ctrl+O saves, the Ctrl+X exits, the Ctrl+W searches, the Ctrl+\ replaces, the Ctrl+K cuts, the Ctrl+U pastes, and the Ctrl+G opens the help.
  • The save is the Ctrl+O, and the exit is the Ctrl+X. The Ctrl+X with the unsaved changes asks to save. The Ctrl+X without the save discards the changes.
  • **The search is the Ctrl+W and the replace is the Ctrl+\.** The search is case-sensitive by default, and the Alt+C toggles the case. The replace offers the Y/N/A options.
  • The nano +N file opens at the line N. The nano +/pattern file opens at the first match. The combination with the grep -n is the tool for the large file.
  • The nano -B creates the backup on the save. The backup is the file~. The option is the safety for the configuration edits.
  • The ~/.nanorc configures the nano. The set linenumbers, the set autoindent, the set 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!