Editing files in the terminal
A text editor never changes a file directly. It asks the kernel to read the file into memory, a copy called the buffer; you change the buffer with keys, and only saving writes it back to the disk. Follow one edit in nano, from nano notes.txt through Ctrl+O and Ctrl+X to cat, then meet vim's modes, :wq and :q!.
5 quick questions
Pick an answer to see why. Your score saves with your progress.
01You open notes.txt in nano and type a new line, but do not save. What is in the file on the disk?
nano works on a copy in memory, the buffer. The file on the disk changes only when you save with Ctrl+O (Write Out).
02In nano's help lines, what does ^X mean?
nano writes Ctrl as ^ (and Alt as M-). ^X is Ctrl and X together: Exit.
03You open a file in vim and press letters, but no text appears where you expect. Why?
vim starts in normal mode. Press i to enter insert mode (vim shows -- INSERT --), type, then press Esc to go back to normal mode.
04Which vim command writes the file and quits?
:wq means write, then quit. :q! quits without writing and throws your changes away; Ctrl+O is nano's save key; i enters insert mode.
05Which system call does an editor use to put the buffer back on the disk?
The editor opens the file (openat) and reads it (read) when it starts; saving hands the buffer to the kernel with write, and the kernel stores it on the disk.
Glossary
- Text editor
- A program for changing the text in a file. In a terminal you steer it with keys, not a mouse.
- nano
- A simple terminal editor that lists its main shortcuts at the bottom of the screen.
- vim
- A powerful terminal editor, based on the older vi, in which keys mean different things in different modes.
- Buffer
- The editor's working copy of a file, kept in memory. You change the buffer; saving copies it to the disk.
- Save (write)
- Copying the buffer back into the file on the disk: Ctrl+O in nano, :w or :wq in vim.
- Normal mode
- vim's starting mode, where keys are commands (i, :wq) rather than text.
- Insert mode
- vim's typing mode, entered with i and left with Esc; vim shows -- INSERT -- while you are in it.
Notes and sources
- While nano or vim runs, it draws the buffer over the whole terminal window. The figure keeps the terminal card and the buffer apart so you can see both.
- nano ends a saved file with a newline, so cat prints each of the three lines on its own line. Some nano versions show the help lines with slightly different wording.
- The C library turns open() into the openat system call on Linux. A program may read or write in more than one call; the figure shows one of each.
- Many programs (git, crontab -e) open the editor named in the EDITOR or VISUAL environment variable, for example export EDITOR=nano.
- In vim, :w writes without quitting, :q quits only when nothing is unsaved, and :q! quits and throws the changes away. Press Esc first if you are in insert mode.
- GNU nano manual
- GNU nano: nano(1) manual page
- GNU nano cheat sheet
- Ubuntu community help: Nano
- Debian Reference, chapter 1: GNU/Linux tutorials (text editors)
- Vim help: intro.txt, Vim's modes
- Vim help: insert.txt, Insert mode
- Vim help: editing.txt, writing and quitting (:wq, :q!)
- The Open Group Base Specifications, Issue 8: vi
- Linux man-pages: open(2) and openat
- Linux man-pages: read(2)
- Linux man-pages: write(2)
- Linux man-pages: cat(1)
- Linux man-pages: environ(7), EDITOR and VISUAL
- R. and A. Arpaci-Dusseau, Operating Systems: Three Easy Pieces, ch. 39 Files and Directories
Quiz Unlocks after the last step.