Learn › Linux Foundations › Editing and archives
Edit, save, create and discard text in the real nano editor.
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.
>>> The buffer is the working copy. Section 19.1.
>>> Nano uses ^ for Ctrl in its shortcut bar. Section 19.2.
>>> A new buffer becomes a file when saved. Section 19.3.
>>> Save to the intended file. Section 19.2.
>>> The file on disk is the result. Section 19.2.
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.
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.
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.
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.
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.
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.
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.
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.
| Key topic | What it is | Section |
|---|---|---|
| Buffer | Working copy in the editor | 19.1 |
| Save and exit | Write the confirmed file, then return to the shell | 19.2 |
| New file | Save a new buffer under its intended filename | 19.3 |
| Discard | Leave unsaved changes out of the file | 19.4 |
| Term | Definition |
|---|---|
| Buffer | Working copy in the editor |
| Save and exit | Write the confirmed file, then return to the shell |
| New file | Save a new buffer under its intended filename |
| Discard | Leave unsaved changes out of the file |
>>> Discard leaves the prior saved content. Section 19.4.
>>> The save action and filename determine what reaches disk. Section 19.2.
>>> Read the proposed filename before pressing Enter. Section 19.2.
>>> Remove accidental text before saving. Section 19.1.
>>> The previous save already changed disk content. Section 19.4.
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.