Course / 03 Command line / Lesson 14 of 19

An editor that works under pressure

Estimated time: 15 min reading · 15 min activity

You type a note in an editor, then close it. The text you saw is not enough to establish what the file contains: a save must write the edited text to storage. Reading the file back connects the visible draft with the saved result.

An editor is a program for changing text. Its buffer is the working text held in memory. Saving writes that buffer to a chosen file. This lesson uses Nano, a terminal editor, and a new note in your owned lab directory.

Before you begin

Estimated time
15 min reading, 15 min activity. Setup and optional practice may take longer.
Where you work
Use Bash in your owned Ubuntu practice environment as an ordinary user. The supplied text files use UTF-8, a text encoding; where shown, LC_ALL=C requests consistent text ordering. Work only under your own new ~/yanosec-lab directory, never a host share or personal/system file. Refuse an existing exercise destination rather than overwriting it. No account, paid platform, or remote target is required. Optional extensions take about 10–15 minutes and are not part of the time above. A virtual machine is optional. You can also finish the reasoning from the printed evidence.
Without a lab
The page supplies the files, source and text records needed for the activity. Without a shell, follow the numbered steps on paper, label each result as predicted, and use the separate evidence supplied by the question. Running commands can wait until your lab is available. Reading works without JavaScript. Answer checking needs JavaScript and a secure context, such as HTTPS; saving a correct result also needs usable browser storage. Missing tools, managed or mobile hosts, or a deferred guest setup never require buying a service. Keep owned lab files for review; do not use broad deletion.

After this lesson you can:

  • Edit, save and exit a new owned text file.
  • Interpret a control-key shortcut without typing its display notation.
  • Compare the saved file with the intended editing buffer.

01Example

Read this conceptual Nano session as actions rather than one block to paste. Save writes the working buffer to the chosen filename. After exiting, cat reads that saved file. The result below is a prediction for the worked example, not an observed session.

Shell command:
nano demo.txt

Editor actions:
Type: first draft then Enter
Ctrl+O -> confirm demo.txt with Enter -> Ctrl+X

Shell command after leaving the editor:
cat demo.txt

Predicted first line:
first draft

Newline behavior can depend on editor state; inspect the saved file.

02Read the save destination

Open a named file with nano followed by its path. For a new file, the name chooses a future save location; it does not prove a file has already been written. Keep the destination inside your fresh exercise directory.

Nano displays keyboard shortcuts near the bottom. The notation ^O means hold Ctrl while pressing O; do not type a caret and O into the text. With default bindings, Ctrl+O opens Write Out, which asks where to save. Confirm the intended filename with Enter. Ctrl+X exits.

If you exit after unsaved changes, read any save decision carefully. Choosing not to save discards those buffer changes; cancelling returns to review. This lesson assumes no automatic save or recovery. After a confirmed write, cat lets you inspect the saved content independently of the editor display.

Trace buffer edits, named save destination and later disk readback; a typed buffer is not automatically a saved file.Model. A teaching model drawn by hand. It shows the idea, not real tool output.

Read the full explanation below.

Playback is optional. Step through at your own pace.

Instruction being read

nano demo.txt

Editor working buffer

editing buffer before typing: unspecified

unknown

Saved demo.txt

prior bytes unknown

unknown

Predicted cat stdout

not yet read

unobserved

Open the editing session; existing saved bytes are unspecified.

Follow the explanation

  1. Open the editing session; existing saved bytes are unspecified.
  2. Typing changes the buffer; it does not establish a save.
  3. ^O means Ctrl plus O; confirm demo.txt as the save destination.
  4. After leaving nano, cat reads the saved file: predicted first line first draft.
Read every state and value

1. Open the editing session; existing saved bytes are unspecified.

Current command
open
Data in each location
Pane: Editor working buffer; Text: editing buffer before typing: unspecified; Status: unknown · Pane: Saved demo.txt; Text: prior bytes unknown; Status: unknown · Pane: Predicted cat stdout; Text: not yet read; Status: unobserved
Links
None

2. Typing changes the buffer; it does not establish a save.

Current command
type
Data in each location
Pane: Editor working buffer; Text: first draft, then Enter; Status: edited · Pane: Saved demo.txt; Text: prior bytes unknown; no save yet; Status: unknown · Pane: Predicted cat stdout; Text: not yet read; Status: unobserved
Links
From: type; To: Editor working buffer; Label: edit working data

3. ^O means Ctrl plus O; confirm demo.txt as the save destination.

Current command
save
Data in each location
Pane: Editor working buffer; Text: first draft, then Enter; Status: edited · Pane: Saved demo.txt; Text: first line: first draft; remaining newline details unspecified; Status: saved · Pane: Predicted cat stdout; Text: not yet read; Status: unobserved
Links
From: Editor working buffer; To: Saved demo.txt; Label: save to demo.txt

4. After leaving nano, cat reads the saved file: predicted first line first draft.

Current command
read
Data in each location
Pane: Editor working buffer; Text: editor closed; buffer no longer active; Status: closed · Pane: Saved demo.txt; Text: first line: first draft; remaining newline details unspecified; Status: saved · Pane: Predicted cat stdout; Text: first line: first draft; Status: predicted
Links
From: Saved demo.txt; To: Predicted cat stdout; Label: cat reads saved content
Trace buffer edits, named save destination and later disk readback; a typed buffer is not automatically a saved file.

03Use a fresh owned workspace

The paths lesson introduced ~/yanosec-lab: ~ expands to your home directory. Before a file activity, return with cd ~/yanosec-lab. Use pwd to check your location and ls -a to inspect names. Continue only if this is your recognized owned lab. Then create the lesson's new child directory.

If mkdir reports that a destination already exists, preserve that work. Choose an unused name and use it consistently, or follow the paper route. Enter one command at a time and continue only after the preceding step succeeds. Record an unexpected result before doing anything else.

Unless the lesson says otherwise, leave the small activity folder for review and return with cd ~/yanosec-lab when finished. Closing the terminal does not delete files you saved. These activities need no sudo or system-file changes.

04Try a saved note

The worked file uses first draft. The activity uses meeting at ten, so you can tell the two cases apart. Both are harmless text; neither requires a privileged editor or a system-file change.

The worksheet retains an earlier Nano 7.2 observation: pressing Enter before Write Out saved the sentence with a blank second line. This is a recorded result from the earlier exercise, not a claim about every editor setting. In the paper route, trace open, edit, write, exit and read-back using that record. Running Nano can wait if it is unavailable.

Trace editing and saving separately

A buffer is the text being edited in memory. Write Out saves it to a
named file. After exiting, reading that file checks stored bytes.
The Nano 7.2 observation below is retained from the earlier course
exercise record; it is not a newly performed session or a guarantee
about every configuration.

Nano 7.2 in a disposable directory. This card is the keystroke record, not a screenshot.
nano note.txt
Type exactly: meeting at ten, then press Enter once.
Ctrl+O: write out; confirm note.txt with Enter; Ctrl+X: exit.
Observed with Nano 7.2: the first line is meeting at ten, and a blank second line is also present.
^O on screen means hold Ctrl while pressing O, not type caret and O.
If a prompt offers to save unexpected changes, read filename and decision;
cancel and preserve work if uncertain. Do not edit host/system files.
Question: buffer changed; exit without saving; no autosave/recovery assumed.
Does disk necessarily hold the new buffer content? yes/no

Download the activity's keystrokes and earlier recorded result

  1. At the lab root create mkdir editor, then cd editor after success. Check that note.txt does not already exist before opening nano note.txt.
  2. Type meeting at ten and press Enter once. Locate the displayed Write Out and Exit shortcuts; do not paste the prompt labels as file content.
  3. Press Ctrl+O, confirm note.txt with Enter, then Ctrl+X. If any filename or save decision differs, cancel the decision and inspect it rather than agreeing blindly.
  4. Read cat note.txt outside Nano. Expected with Nano 7.2: the first line is meeting at ten, and pressing Enter before Write Out also leaves a blank second line. Record actual content and path; a screenshot of a buffer alone would not prove the save.
  5. On paper trace open, buffer change, write and read-back using the same text. Label live editing deferred. Return to the lab root and retain the file; no system edits or cleanup are needed.

05Optional challenge

Allow about ten extra minutes. Start from a saved first draft, change the buffer on paper, and choose exit without saving. Track the buffer and disk file separately. Explain why reopening cannot be assumed to restore an unsaved change through an unspecified recovery feature.

06Fresh cases for the checks

Use these fictional cases for the checks below. They have different inputs from the worked examples. Read them here or make notes; no installation or external service is needed.

Editor card A: separate buffer and save destinationsOwned fixture. An original file made for this course. Follow the activity's instructions in your own lab.

Starting disk file plan.txt contains: gate closes at six
No file copy.txt exists initially.
1. Open plan.txt and change the buffer to: gate closes at eight
2. Write Out that buffer to copy.txt and confirm that write.
3. Change the buffer again to: gate closes at nine
4. Exit and choose not to save this last change.
No other write, automatic save or recovery occurs. Read only the first-line text.

Editor card B: a shortcut mistakeOwned fixture. An original file made for this course. Follow the activity's instructions in your own lab.

A learner wants to save an owned note in Nano with its default bindings.
Nano displays ^O for Write Out.
The learner types the two characters ^O into the buffer instead.
No Write Out prompt appears. No save has occurred.

Editor card C: an independent read-back discrepancyOwned fixture. An original file made for this course. Follow the activity's instructions in your own lab.

Intended first line in the file ./travel/packing.txt: bring spare socks
The editor buffer shows: bring spare socks
A confirmed Write Out saved to ./travel/packing-copy.txt.
After exit, cat ./travel/packing.txt displays: bring socks
Both paths belong to this exercise. No automatic save or synchronization is present.

Editor card D: a changed write sequenceOwned fixture. An original file made for this course. Follow the activity's instructions in your own lab.

Starting first line on disk in reminder.txt: return before noon
Open reminder.txt; change its buffer to: return before dusk
Confirm Write Out to reminder.txt, then exit.
No later changes occur. Predict only the first-line text, excluding its newline.

07Explain what proves a save

Optional self-check: explain how you would preserve an edited note and check the saved result. Include the buffer, the Write Out filename, the shortcut and a read-back after leaving the editor.

Review your explanation: interpret ^O as a Ctrl key combination; inspect the destination before confirming; distinguish a buffer edit from a disk write; and compare the intended content with the same saved path. Name any editing work you have only traced on paper. This explanation is self-reviewed.

08Common mistakes

If ^O appears in the note, you typed the shortcut notation. Use the actual Ctrl key combination; inspect the saved text before claiming success.

If cat shows older content, inspect the save filename and whether a write was confirmed. Do not overwrite unrelated paths while guessing where the buffer went.

If Nano is unavailable or the host intercepts a shortcut, use the complete trace and record the environment issue. The saved activity file began with meeting at ten.

References

  • Ubuntu 24.04 packaged manual: nano(1)

    Use the named command and option sections. This manual describes Ubuntu 24.04 packages; your installed version can differ. The lesson supplies the facts needed for its activity.

  • Ubuntu 24.04 packaged manual: cat(1)

    Use the named command and option sections. This manual describes Ubuntu 24.04 packages; your installed version can differ. The lesson supplies the facts needed for its activity.