Editors and buffers
A text editor is a program for changing the words in a file. In a terminal there is no mouse menu, so you steer it with keys. nano is the friendliest one; vim is powerful but has modes.
Editing with nano
Type nano notes.txt and press Enter. The shell starts the nano program and gives it the file's name. nano now fills the whole terminal window until you leave it.
nano asks the kernel to open notes.txt and read it, with the system calls openat and read. The kernel fetches the two lines from the disk, and nano keeps a copy in memory: the buffer.
What you see is the buffer, not the file itself. The bottom lines list shortcuts: ^O Write Out, ^X Exit. The ^ means hold Ctrl, so ^O is Ctrl and O together.
Use the arrow keys to move to the end, press Enter and type milk. The buffer now has three lines and nano shows Modified at the top. The file on the disk still has two.
Press Ctrl+O. nano asks File Name to Write: notes.txt, and Enter says yes. nano hands the buffer to the kernel with the write system call, the file on the disk gets line 3, and nano reports [ Wrote 3 lines ].
Press Ctrl+X to exit. nano ends, its buffer is freed and the shell's prompt comes back. Had you not saved, nano would first ask Save modified buffer? Y saves; N throws the changes away.
Check it: cat notes.txt asks the kernel to read the file and prints it. Three lines come back: eggs, bread, milk. The change really is on the disk now.
vim and its modes
vim notes.txt opens the same file in vim. vim starts in normal mode, where keys are commands. Press i for insert mode (it shows -- INSERT --) and type; Esc goes back. Then :wq and Enter write and quit.
Made a mess? In vim, :q! quits without writing, like N in nano. Careful: the changes in the buffer are thrown away for good. Here the typo brd is lost, and the disk still says bread.
What happens
;; step 1/10 · editor
text editor a program that changes the text in a file
in a terminal no menus to click: you steer with keys
nano simple: shortcuts listed on screen
vim powerful: keys mean different things
in different modes
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. 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.