Free Ebook cover Linux Command Line for Beginners: Navigate, Search, and Automate Simple Tasks

Linux Command Line for Beginners: Navigate, Search, and Automate Simple Tasks

New course

10 pages

Editing Basics in the Terminal: Nano Essentials and Quick Fixes

Capítulo 8

Estimated reading time: 7 minutes

+ Exercise

Why nano is a good first terminal editor

Many Linux tasks involve editing plain-text files: configuration files (often in /etc), small scripts, and notes. nano is a beginner-friendly terminal editor because it shows key shortcuts on-screen and uses simple keystrokes. This chapter gives you a practical workflow for making safe edits, finding text quickly, and avoiding common mistakes.

Starting nano

Open a file (or create it if it doesn’t exist):

nano filename

Open a file with line numbers (helpful for troubleshooting):

nano -l filename

If you need to edit a protected system file, you typically must use elevated privileges:

sudo nano /etc/someconfig.conf

Tip: If you only need to read a file, don’t open it in an editor. Use a viewer instead (you’ll do that in the mini-lab to verify changes).

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

Open / Save / Exit: the core workflow

Open

When nano opens, you’ll see the file content and a shortcut bar at the bottom. Nano writes shortcuts like ^O meaning “Ctrl + O”.

Save (Write Out)

To save your changes:

  • Press Ctrl + O (Write Out).
  • Nano shows the filename at the bottom. Press Enter to confirm.

To save to a different filename (a quick backup approach):

  • Press Ctrl + O.
  • Edit the filename (for example, add .bak).
  • Press Enter.

Exit

To exit nano:

  • Press Ctrl + X.
  • If you have unsaved changes, nano asks whether to save. Press Y for yes, N for no.
  • If you chose yes, press Enter to confirm the filename.

Navigating efficiently (lines, pages, start/end)

For small files you can use arrow keys, but config files can be long. These shortcuts help you move quickly:

  • Go to a specific line: Ctrl + _ (underscore), then type the line number, press Enter.
  • Page down / up: Ctrl + V (page down), Ctrl + Y (page up).
  • Start/end of line: Ctrl + A (start), Ctrl + E (end).
  • Search forward: Ctrl + W, type text, press Enter.
  • Repeat search: after a search, press Ctrl + W then Enter to find the next match (nano reuses the last search term).

Practical habit: when following instructions that reference “line 42”, open with nano -l and use Ctrl + _ to jump directly there.

Search and replace (quick fixes without retyping)

Search

To find text:

  • Press Ctrl + W.
  • Type the search term (for example, Listen or PATH=).
  • Press Enter.

Replace

To replace text:

  • Press Ctrl + \ (that’s Ctrl + backslash).
  • Type the text to find, press Enter.
  • Type the replacement text, press Enter.
  • Nano will ask for confirmation on each match. Use:
  • Y to replace this match
  • N to skip this match
  • A to replace all matches
  • Ctrl + C to cancel

Safe workflow tip: for configuration files, prefer replacing one-by-one (Y/N) unless you are very sure the term is unique.

Copy, cut, and paste (nano’s “mark” workflow)

Nano uses a selection system called “marking”. You mark a region, then cut or copy it, then paste it elsewhere.

Select (mark) text

  • Move the cursor to the start of what you want to select.
  • Press Ctrl + ^ (Ctrl + Shift + 6 on many keyboards).
  • Use arrow keys to expand the selection.

Cut and paste

  • Cut: Ctrl + K (cuts the selected region; if nothing is selected, it cuts the current line).
  • Paste: Ctrl + U.

Copy (without deleting)

On many nano versions, you can copy the marked text with:

  • Alt + 6 (sometimes shown as M-6) to copy the selection into nano’s buffer.
  • Then Ctrl + U to paste.

If Alt + 6 doesn’t work in your terminal, a simple alternative is to cut (Ctrl + K) and immediately paste (Ctrl + U) at the original location to restore it, then paste again where needed. This feels clumsy, but it works everywhere.

Avoiding common pitfalls

Pitfall: file permissions and “why won’t it save?”

If you open a file you cannot write, nano may let you edit but will fail when saving, or it will warn you that the file is read-only.

  • If it’s a system config file, reopen it with sudo nano /path/to/file.
  • If you don’t have permission to use sudo, save to a new filename in your home directory and ask an administrator to apply the change.

Practical safe approach for sensitive files: save a backup copy first by writing out to a new name (for example, Ctrl + O then filename.bak), then edit and save the original.

Pitfall: accidental format changes (tabs, spaces, and line endings)

Configuration files often care about exact formatting:

  • Indentation matters: some formats require spaces (or tabs) consistently. Don’t “pretty up” indentation unless you know the format rules.
  • Don’t wrap long lines: if a config line is long (for example, a long path or key), keep it on one line. If your terminal is narrow, it may look wrapped visually, but you should not insert real line breaks.
  • Be careful with copy/paste from websites: pasted text can include “smart quotes” or hidden characters. Prefer typing critical lines manually or pasting from trusted plain-text sources.

If you suspect hidden characters were introduced, retype the affected line in nano rather than trying to “fix” it by eye.

Pitfall: editing the wrong file

It’s easy to open a similarly named file in the wrong directory. Before saving, confirm the path shown at the top of nano matches what you intended. If you’re unsure, exit without saving (Ctrl + X, then N).

Pitfall: breaking config by deleting a comment marker

Many config files use # to mark comments. Removing a leading # enables a setting; adding it disables a setting. When making quick fixes, change only what you need and leave surrounding lines untouched.

Optional: setting your default editor with $EDITOR

Some commands open an editor automatically (for example, when editing commit messages or system configuration via helper tools). Linux uses environment variables like EDITOR to decide which editor to launch.

To set nano as your default editor for your current shell session:

export EDITOR=nano

To verify:

echo $EDITOR

Making it permanent depends on your shell startup files, but the key idea is: if a tool respects $EDITOR, setting it to nano makes nano the default.

Mini-lab: safe edits, notes file, and verification

Lab goal

You will (1) edit a “config-style” file safely using a backup, (2) create a small notes file, and (3) validate your changes by re-viewing the files.

Part 1: Edit a config file safely (practice file)

Instead of risking a real system file, create a practice config file in your home directory and treat it like a real one.

1) Open nano and create a file:

nano -l myapp.conf

2) Type the following lines (pay attention to # comments):

# myapp.conf - practice configuration file (not a real system file)  port=8080  mode=development  # log level can be: debug, info, warn, error  log_level=info

3) Save it (Ctrl + O, Enter), but also create a backup copy before making changes:

  • Press Ctrl + O
  • Change the filename to myapp.conf.bak
  • Press Enter

4) Now modify the original file content (still in nano):

  • Use Ctrl + W to search for log_level.
  • Use Ctrl + \ to replace log_level=info with log_level=debug (confirm with Y).
  • Change mode=development to mode=production by editing directly or using replace.

5) Save and exit:

  • Ctrl + O, Enter
  • Ctrl + X

Part 2: Create a small notes file (copy/paste practice)

1) Open a new file:

nano notes.txt

2) Type a short checklist like:

Editing checklist: - Make a backup before changing config - Search before you edit - Save, then re-view the file to confirm

3) Practice selecting and copying:

  • Mark the line Make a backup before changing config using Ctrl + ^ and arrow keys.
  • Copy it with Alt + 6 (if available), then paste it with Ctrl + U to duplicate it.
  • If Alt + 6 doesn’t work, cut with Ctrl + K, paste back with Ctrl + U, then paste again where you want the duplicate.

4) Save and exit (Ctrl + O, Enter, then Ctrl + X).

Part 3: Validate changes by re-viewing

Re-open the files in a viewer to confirm what is actually on disk.

View the updated config file:

less myapp.conf

Check that mode=production and log_level=debug are present, and that comments still start with #.

Compare with the backup:

less myapp.conf.bak

View your notes quickly:

cat notes.txt

Now answer the exercise about the content:

In nano, what is a safe way to handle a configuration file when you want to make changes without risking the original?

You are right! Congratulations, now go to the next page

You missed! Try again.

A safer workflow is to write out a backup copy first (e.g., save as filename.bak), then make changes and save the original. This reduces risk if an edit breaks formatting or settings.

Next chapter

Simple Automation with Shell Scripts

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.