Skip to content

Editing with Nano

Module
Reading and Editing Text
Difficulty
Easy
Updated
Tested on
Ubuntu 24.04.5 LTS (lab container), GNU Bash 5.2.21; Ubuntu 24.04.4 LTS (course VM), GNU nano 7.2
On this page

Your reading note says Status: draft, but you have completed the work. Replacing the whole file would also replace its title. You need to change the status and keep the rest.

By the end, you can open a file in Nano, move and search inside it, save an edit, and leave with a verified result.

Finish Following a changing file (B07.2). Keep your existing B04 practice directory and the earlier B07 exercises.

Use only ~/lab/b07/nano. Run this setup once; use Reset this lesson before repeating it.

Terminal window
mkdir -p ~/lab/b07/nano
cd ~/lab/b07/nano
printf 'Reading notes\nStatus: draft\n' > notes.txt

The setup writes a disposable two-line note. Its printf and > pattern is the same one used to prepare files in B04. It does not change your original notes in ~/lab/Documents.

The screenshots use GNU nano 7.2 on the course’s Ubuntu 24.04.4 Desktop VM. Their keyboard steps use the default configuration, without launch options. If your shortcuts differ, read the labels shown in your own editor.

Terminal window
nano notes.txt

Nano fills the terminal. While it is open, keys go to Nano rather than Bash. You do not need to start another shell to type file contents.

Nano shows notes.txt containing Reading notes and Status: draft, with its shortcut rows.
Nano 7.2 opens the original note. Ubuntu 24.04.4 LTS, course VM. Scroll inside the image on narrow screens.
  1. The title bar names notes.txt; check that it is the intended file.
  2. The shortcut rows show ^O Write Out, ^W Where Is, and ^X Exit. The caret means Ctrl.

The title bar identifies the file. The editing area contains its text. The bottom rows show shortcuts for the current context. In those shortcuts, ^ means Ctrl: ^O is Ctrl+O, the letter O rather than zero.

These are the keys used here:

Key What it does in this exercise
Arrow keys Move the cursor
Ctrl+A / Ctrl+E Move to the beginning / end of the current line
Backspace Remove the character before the cursor
Ctrl+W, text, Enter Search for text
Ctrl+O, Enter Write the file, accepting the displayed filename
Ctrl+X Exit, asking about unsaved changes when needed

You saw Ctrl+A and Ctrl+E in Bash’s Readline in B04.2. Nano also uses them for line movement, but these are Nano’s controls while it owns the terminal.

Move to the first line with the arrow keys if necessary. Press Ctrl+E to reach its end, then type for B07, including the leading space.

The first line is Reading notes for B07; an asterisk appears beside notes.txt.
Typing changes the note in Nano’s buffer. Ubuntu 24.04.4 LTS, course VM. Scroll inside the image on narrow screens.
  1. The added text is in Nano’s working copy.
  2. The asterisk marks unsaved changes.

Predict first: does the asterisk beside the filename mean the saved file already contains your edit?

Run it, then compare with your guess

No. You changed Nano’s buffer, its working copy of the text. The asterisk marks changes that have not been saved. The filename tells you the destination, not that a write has happened.

Do not type cat notes.txt into the editing area to check. Nano would insert those characters into the note. You will read the saved file from Bash after exiting.

Press Ctrl+W. Type Status, then Enter. The search moves the cursor to that word:

The block cursor rests on the S of Status: draft; the first line retains the added text.
Nano finds the status line. Ubuntu 24.04.4 LTS, course VM. Scroll inside the image on narrow screens.
  1. The cursor is at Status after Ctrl+W searches for that word.

Search is useful when a file is longer than the two lines used here. It finds the place to work; it does not replace the matched text.

On the status line, press Ctrl+E. Press Backspace five times to remove draft, leaving Status: . Type ready. The title remains Reading notes for B07; only the chosen value changes on the second line.

Press Ctrl+O. Nano’s label Write Out means save the buffer to a file. It asks for a filename:

The note says Status: ready; the bottom prompt reads File Name to Write: notes.txt.
Ctrl+O opens Nano’s filename-to-write prompt. Ubuntu 24.04.4 LTS, course VM. Scroll inside the image on narrow screens.
  1. File Name to Write asks where to save; notes.txt keeps the same destination.
  2. The intended status is ready before the save is confirmed.

Read that destination before accepting it. Here it is notes.txt, the disposable file you opened. Press Enter to write it. The prompt is part of Nano, not Bash; typing a shell command there would change the proposed filename.

Press Ctrl+X to exit. Because you saved the changes, Nano returns to Bash without asking you to save again. Check from the shell:

Terminal window
cat notes.txt
Captured output
Reading notes for B07
Status: ready
The terminal prints Reading notes for B07 and Status: ready, followed by a Bash prompt.
After saving and exiting, cat proves the contents. Ubuntu 24.04.4 LTS, course VM. Scroll inside the image on narrow screens.
  1. After Nano exits, cat reads both saved lines from notes.txt.
  2. The shell prompt confirms that input is back with Bash.

This output is captured from the course VM after the edit. The title and status survived reopening through cat: they are in the file, not only on an editor screen.

Reopen the same file:

Terminal window
nano notes.txt

Press Ctrl+A on its first line and type TEMPORARY , including the space. Then press Ctrl+X without saving. This is a disposable test change, not part of the note you want to keep.

Nano shows TEMPORARY at the start of the note and Save modified buffer? with Yes, No, and Cancel choices.
An unsaved change makes exit ask a question. Ubuntu 24.04.4 LTS, course VM. Scroll inside the image on narrow screens.
  1. Nano asks whether to save the modified buffer.
  2. N discards these unsaved edits; Ctrl+C cancels exit and returns to editing.

Read the choices. Y starts saving; N leaves without saving these edits; Ctrl+C cancels the exit so you can keep working. Here press N to discard only the temporary change.

Terminal window
cat notes.txt
Captured output
Reading notes for B07
Status: ready

The earlier saved version remains. Discarding a buffer change does not undo a save you already made. If you save unwanted text, leaving the editor afterward does not restore the old file.

A misspelled filename can open an empty buffer. Opening an unused name is a normal way to create a file. An empty Nano screen is therefore not proof that your existing note was empty. Check the title and intended path before typing or saving.

Confusing Ctrl+O with Ctrl+0. The shortcut uses the letter O. Read Write Out on the bottom row rather than guessing from the shape of the character.

Assuming exit always saves. The modified-buffer question exists because saving and exiting are separate actions. Choose deliberately; N discards unsaved edits, while Ctrl+C keeps the editor open.

Goal: Mark a separate handoff as checked while preserving the saved source note.

Terminal window
cp notes.txt handoff.txt
nano handoff.txt

Change only the handoff’s ready value to checked, using a search to find it. Save and exit, then verify the handoff from Bash. Keep notes.txt as it is.

Expected: The handoff keeps the title and says Status: checked; the source note still says Status: ready.

Hint

Search for ready, move to the end of its line, and remove its five letters. The filename-to-write prompt should name handoff.txt.

Solution
  1. Ctrl+W, type ready, Enter.
  2. Ctrl+E, Backspace five times, type checked.
  3. Ctrl+O, Enter, Ctrl+X.
Terminal window
cat handoff.txt
Captured output
Reading notes for B07
Status: checked
Terminal window
cat notes.txt
Captured output
Reading notes for B07
Status: ready

You edited an independent copy. Nothing in those editor steps changed the source path.

Exit Nano first, saving or discarding changes deliberately. Then inspect this lesson’s directory from its parent:

Terminal window
cd ~/lab/b07
ls -AR nano

After the challenge it contains notes.txt and handoff.txt.

Terminal window
rm -r ~/lab/b07/nano

Rerun Before you start to restore the draft note. If the directory is absent, skip removal. Restoring the fixture deliberately replaces this lesson’s saved edits, not your other notes.

  • Nano’s bottom rows show shortcuts; ^ means Ctrl.
  • Typing changes the buffer. An asterisk indicates unsaved changes.
  • Search locates text; it does not replace it.
  • Ctrl+O writes to the chosen filename; Ctrl+X exits. Read the unsaved-exit question before answering.
  • After exiting, cat verifies what was saved.

Next, Editing with Vim introduces an editor whose typing keys depend on its mode.

  • On your system: man nano and Nano’s Ctrl+G help screen.
  • GNU nano 7.2 manual, especially Editor Basics, Screen Layout, Commands, and Search and Replace.