Editing files from the terminal

Editing a file at a terminal with vi: the modal idea that makes it confusing for the first hour, the movement and editing keys, and how to leave without losing work.

Lesson 8 of 8 in objective 103. GNU and Unix commands, part of LPIC-1 Exam 101-500.

How vi moves between its three modes, and the one jump you cannot make. A column of 3 states: Insert mode (What you type goes into the file); Command mode (Where vi opens; every key is a command, not text: moving, deleting, yanking); Ex, the last line (Commands that act on the file as a whole: w, q, wq, q!, %s). You get from Insert mode to Command mode by Esc, back from anywhere; from Command mode to Ex, the last line by :; from Command mode to Insert mode by i before the cursor, a after it, o opens a line below, O above; from Ex, the last line to Command mode by Enter, or Esc. One arrow is drawn crossed through, because that move does not exist: Insert mode to Ex, the last line — never: in insert mode a colon goes into the file, so press Esc first. Insert mode What you type goes into the file Esc, back from anywhere Command mode Where vi opens; every key is a command, not text: moving, deleting, yanking : Ex, the last line Commands that act on the file as a whole: w, q, wq, q!, %s i before the cursor, a after it, o opens a line below, O above never: in insert mode a colon goes into the file, so press Esc first Enter, or Esc
How vi moves between its three modes, and the one jump you cannot make.

Modes, and why nothing you type appears

vi starts in command mode, where every key is a command rather than text — which is why typing a sentence into a fresh vi produces chaos. i enters insert mode before the cursor, a after it, o opens a new line below and O above. Escape returns to command mode, and getting into the habit of pressing it before every command is the single most useful thing a beginner can do.

The third mode is the ex or last-line mode, entered with : and used for the commands that act on the file as a whole: :w writes, :q quits, :wq writes and quits, :q! quits discarding changes, and ZZ in command mode is the same as :wq. :w newfile writes a copy under another name.

Moving and editing

h, j, k and l move left, down, up and right, and the arrow keys usually work too. 0 goes to the start of a line, $ to the end, G to the end of the file and 1G or gg to the top. A search is a slash followed by the pattern and Enter: /daemon opens the prompt at the foot of the screen and jumps to the next occurrence of daemon below the cursor, ?daemon searches upwards instead, n repeats the search in its original direction and N reverses it. The pattern is a regular expression, so a dot matches any character until it is escaped.

x deletes one character, dd deletes a line, dw deletes a word, and a count multiplies any of them: 3dd deletes three lines. yy copies (yanks) a line and p pastes after the cursor, P before. u undoes the last change and Ctrl-R redoes it. Because a delete puts the text in the same buffer a yank uses, dd followed by p is how a line is moved.

The ex substitution is worth knowing because it is the same syntax as sed: :%s/old/new/g replaces every occurrence on every line, where % means the whole file and g means every match on each line.

The ex substitution, and what each position controls. :%s/old/new/g — part 1, :: enters ex mode; part 2, %: every line in the file; part 3, s: substitute; part 4, old: what to find; part 5, new: what replaces it; part 6, g: every match on a line. 1 : 2 % 3 s / 4 old / 5 new / 6 g 1 enters ex mode 2 every line in the file 3 substitute 4 what to find 5 what replaces it 6 every match on a line
The ex substitution, and what each position controls.

Which editor a program opens

Dropping into vi when you run crontab -e or visudo is not a coincidence and not a setting inside those programs: they look in the environment for the editor to start, and vi is what they fall back on when nothing tells them otherwise. VISUAL is consulted first and EDITOR second, which is the whole trap — setting EDITOR alone changes nothing on a machine where VISUAL is already set to something else, and that is the usual reason the change appears to be ignored. An alias cannot do this job either, however tempting alias vi=nano looks: an alias is expanded by an interactive shell reading a command line, and crontab is not going through your shell to start its editor.

Where the assignment goes decides how far it reaches. export EDITOR=/usr/bin/nano typed at a prompt applies to that shell and the programs it starts, and is gone when the shell exits. The same line in ~/.bashrc covers this user's later interactive shells, in ~/.bash_profile a login shell, and in /etc/profile or a file under /etc/profile.d/ everyone on the machine. A question that says "from now on" is asking for the startup file, not for the prompt.

Worth carrying in

i / a / o
Insert before, after, or open a line below. O opens above.
Esc
Back to command mode from anywhere.
:wq / ZZ
Write and quit. :q! quits discarding changes.
dd / yy / p
Delete a line, yank a line, paste after. A count prefix multiplies.
x / u
Delete a character; undo the last change.
/pattern / n
Search forward, repeat the search. ? searches backwards.
:%s/old/new/g
Substitute throughout the file — the sed syntax again.
G / 1G
End of file; top of file.
EDITOR / VISUAL
Which editor crontab -e and visudo start. VISUAL is checked first; set them in a startup file to persist.

What the exam does with this

Objective
103. GNU and Unix commands
Share of the exam
43.33% (the whole objective)
Questions in this lesson
15
Signed for by a person
0

Partly checked. None of the 15 questions here has been read against the cited source by a person. 15 questions have been checked against their cited clause by an automated pass — which is not the same thing, and is not a signature.

Only questions a person has signed for are used in mock exams here. That is the whole difference between the two kinds of checking above.

How these questions are written — where each question comes from, what the verification ledger records, and what happens when one is found wrong.

Drill this lesson

A lesson is one sitting: the trainer draws a short run from these questions alone and spaces the ones you get wrong.

Practise Editing files from the terminal

Questions in this lesson

Practise Editing files from the terminal

The rest of objective 103