Learn › Linux Foundations › Editing and archives

Chapter 19, Part 1. Editing text

Edit, save, create and discard text in the real nano editor.

Chapter 19, Part 1. Editing text

This chapter covers the following topics:

This is Part 1 of 3. It covers Sections 19.1 to 19.4.

What you already know: Chapters 2, 5 and 6 introduced paths, reading and copying files. Chapter 11 taught output redirection with >; Part 3 reuses it to keep a source file while writing compressed output to a named file. Every new option is explained before its command.

Objectives

These examples use a dedicated ~/editing-archives/practice tree. Card output is recorded; inspect your live terminal and files. Its prompt may look different. Type only the command, not a shown prompt. Tab can complete a path after you type its beginning. The assessment starts a separate fresh machine and checks its results automatically.

Pre-check 1 of 5

Pre-check 2 of 5

Pre-check 3 of 5

Pre-check 4 of 5

Pre-check 5 of 5

19.1 Read before editing

A text editor changes characters in a file. To work in the real editor, select Start machine in the adjacent workspace and wait for its Terminal. First read the prepared service file. You are in your home directory; this home-rooted path also works if you have moved elsewhere. cat displays the file without changing it.

cat ~/editing-archives/practice/edit/service.txt

prompt: admin@server1:~$ answer: cat ~/editing-archives/practice/edit/service.txt output: service=website status=draft hint: Section 19.1. Use the shown command form and the exact target ~/editing-archives/practice/edit/service.txt.

Identify the service name and the current status. The next exercise changes only the status.

19.1 Open the editor buffer

Run the command below to open the file in nano. The editor keeps a working copy called a buffer. Changes in the buffer are not yet saved to disk. Use the arrow keys to reach draft, remove that word, and type ready. Keep status= and the service line unchanged. Do not type shell commands into the editor.

nano ~/editing-archives/practice/edit/service.txt

Save with Ctrl+O: hold Ctrl and press the letter O, not zero. Read the proposed filename and press Enter to keep it. Press Ctrl+X to exit to the shell. Nano displays ^ for Ctrl in its shortcut bar. Confirm the filename before saving.

19.2 Verify the saved file

You are back at the shell. A saved change is a property of the file, not a completion message in the editor. Read the same path again. It should retain the service name and show the new status.

cat ~/editing-archives/practice/edit/service.txt

prompt: admin@server1:~$ answer: cat ~/editing-archives/practice/edit/service.txt output: service=website status=ready hint: Section 19.2. Use the shown command form and the exact target ~/editing-archives/practice/edit/service.txt.

Compare both lines with the intended change. If the old status remains, reopen nano, change the buffer, save to this same path, and return to the shell.

19.3 Create a new text file

Opening a new filename creates a new buffer. In nano, type two lines: Inspect the source. and Verify the restored copy. Press Enter between the lines. Leave the cursor after the last period; nano adds the final newline when saving in this configuration. Save with Ctrl+O, confirm this filename with Enter, then exit with Ctrl+X. The assigned file does not exist at the start of this exercise.

nano ~/editing-archives/practice/edit/new-note.txt

The file is created when you save it. Exiting without saving would leave no new file.

19.3 Check the new file

Read the new file from the shell. Verify both lines and their order. This checks the saved file, rather than relying on what remains visible in an editor buffer.

cat ~/editing-archives/practice/edit/new-note.txt

prompt: admin@server1:~$ answer: cat ~/editing-archives/practice/edit/new-note.txt output: Inspect the source. Verify the restored copy. hint: Section 19.3. Use the shown command form and the exact target ~/editing-archives/practice/edit/new-note.txt.

A missing-file result means the path was not saved as intended. Inspect the save filename rather than creating unrelated directories.

19.4 Leave an edit unsaved

Open the prepared discard file. Add UNSAVED to its line. Press Ctrl+X. When nano asks whether to save the modified buffer, choose No with N on this English-language configuration. Do not press Ctrl+O first: that would already save the change. This exercise deliberately discards only a change to this prepared file.

nano ~/editing-archives/practice/edit/discard.txt

The next read checks that the added text did not reach disk. Nano prompts and shortcut labels must match the installed version and locale.

19.4 Verify the unchanged saved content

Read the file after leaving nano. The original line must remain and UNSAVED must be absent. Saving and discarding have different effects even though both return you to the shell.

cat ~/editing-archives/practice/edit/discard.txt

prompt: admin@server1:~$ answer: cat ~/editing-archives/practice/edit/discard.txt output: Keep this saved line. hint: Section 19.4. Use the shown command form and the exact target ~/editing-archives/practice/edit/discard.txt.

The file keeps its previous saved content. Discard does not undo a change you saved earlier.

Summary

Nano keeps a buffer separate from the saved file. Ctrl+O writes to the confirmed filename. Ctrl+X exits. Choosing not to save at exit discards unsaved changes. Read the saved file from the shell to verify your result.

Review all key topics

Key topicWhat it isSection
BufferWorking copy in the editor19.1
Save and exitWrite the confirmed file, then return to the shell19.2
New fileSave a new buffer under its intended filename19.3
DiscardLeave unsaved changes out of the file19.4

Define key terms

TermDefinition
BufferWorking copy in the editor
Save and exitWrite the confirmed file, then return to the shell
New fileSave a new buffer under its intended filename
DiscardLeave unsaved changes out of the file

Review question 1 of 5

Review question 2 of 5

Review question 3 of 5

Review question 4 of 5

Review question 5 of 5

Next part

Continue to Part 2: Tar archives and recovery. Keep the practice files; the next part uses the same dedicated tree.

Practice Chapter 19, Part 1. Editing text in a real Linux terminal at The Linux Camp. Progress is verified automatically as you type commands on the machine.

More lessons in Editing and archives