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.
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.
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
sedsyntax again. - G / 1G
- End of file; top of file.
- EDITOR / VISUAL
- Which editor
crontab -eand visudo start.VISUALis checked first; set them in a startup file to persist.
What the exam does with this
- :q! is the escape hatch when a question says the changes must be discarded; :wq is the one that saves.
dddeletes into the same buffer p pastes from, so delete-then-paste is a move.- A number before a command repeats it: 5dd, 3x, 10j.
export EDITOR=/usr/bin/nanoin a startup file is howcrontab -estops opening vi. An alias cannot do it, andVISUALwins if it is also set.
- 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
- You open a file with vi. You immediately type `dd`. What have you done, and what mode were you in? machine-checked
- In vi you have made changes you do not want. Which command leaves the editor and discards them? machine-checked
- You are repairing a minimal rescue system with almost no packages installed. Why is vi the editor an LPIC-1 candidate is expected to know? machine-checked
- Starting from command mode in vi, which THREE keys switch the editor into insert mode? machine-checked
- You are in vi command mode with unsaved changes. Type the command that writes the file and then exits the editor. machine-checked
- You are reordering entries in a configuration file with vi. In command mode you put the cursor on the line you want to move and press `dd`, then move the cursor down two lines. Which single keystroke re-inserts the removed line below the cursor? machine-checked
- You have opened a long log file in vi and jumped to the end of it. You now need to find the nearest earlier occurrence of the word `error`, that is, above the cursor. Which command starts that search? machine-checked
- You are editing a file in vi over a serial console whose arrow keys produce escape sequences vi does not interpret. In command mode, which key moves the cursor down one line? machine-checked
- Running `crontab -e` on a server drops you into vi, but you would rather it opened nano for your account from now on. Which line, added to your shell startup file, is the intended way to arrange that? machine-checked
- You opened a file with `vi -R /etc/motd`, so the editor's `readonly` option is set. You then decide the file does need changing, make your edits, and `:w` is refused with a message that the readonly option is set. Your account does have permission to write the file. Which command writes it anyway? machine-checked
- In vi you are editing the kernel command line inside /etc/default/grub. The cursor sits on the first character of an option in the middle of the line, and you want to remove that character and everything after it on the same line, keeping the text before the cursor and the line itself. Which command-mode command does it in one step? machine-checked
- A colleague edits a file with nano and asks what the entries at the bottom of the screen, such as `^O Write Out` and `^X Exit`, are telling them to press. machine-checked
- Which THREE of the following vi command-mode commands remove text from the buffer? (Choose three.) machine-checked
- You are in vi's command mode at the top of /etc/services and want to jump forward to the next occurrence of the string `daemon`. Type exactly what you enter to begin that search, including the leading punctuation character and the search string, but without the Enter key. machine-checked
- In vi's command mode you want to copy the current line into the unnamed buffer without removing it from the file. Type the two-keystroke command that does it. machine-checked
Practise Editing files from the terminal
The rest of objective 103
- Working on the command line
- Filtering text streams
- Basic file management
- Streams, pipes and redirection
- Creating, monitoring and killing processes
- Process execution priorities
- Searching text with regular expressions
- Editing files from the terminal — you are here