Based on the Linux Master 2 exam syllabusReport an error
i (to insert), ESC (to command), and : (to ex).:w, quit with :q, save and quit with :wq, and force quit with :q!.| Category | Unit it works on | Examples |
|---|---|---|
| Line editor | One line at a time | ed, ex, sed |
| Screen editor | The whole screen | vi, vim, emacs, nano, pico |
| Editor | Features |
|---|---|
| vi | Developed by Bill Joy. Included by default on almost every Unix |
| vim | Vi IMproved — an improved version of vi. The current standard on Linux |
| emacs | Developed by Richard Stallman. Extremely extensible |
| pico | For beginners. Simple |
| nano | An improved version of pico (GNU). Shortcut keys are shown at the bottom of the screen |
| gedit | The graphical editor of the GNOME desktop |
You may be asked directly who created each one. vi is Bill Joy, emacs is Richard Stallman. It also helps to remember that Bill Joy created the C shell (csh) too.
ed (line editor) → ex (line editor, Bill Joy) → vi (ex's screen mode, 1976) → vim (Bram Moolenaar, 1991)
pico (editor of the mail program Pine) → nano (GNU): inside vi are precisely ex commands, which is why the third mode is called ex mode.sed is a non-interactive stream editor. It does not open a file and edit it; it processes edits specified by commands as the data flows through. It works line by line, so it is classified with the line editors.vi usually launches vim in reality. Depending on the distribution, a reduced-feature version may be installed.| Runtime environment | Editors |
|---|---|
| Console (terminal) | vi, vim, emacs, nano, pico |
| Graphical (X Window) | gedit (GNOME), kate and kwrite (KDE), emacs |
┌──────────────────┐
│ Command mode │ ← you start here
│ (command mode) │ move · delete · copy
└──────────────────┘
i a o │ ▲ │ :
▼ ESC │ ▼
┌──────────────┐ ┌──────────────┐
│ Insert mode │ │ ex mode │
│ (insert mode)│ │ (last line) │
│ type text │ │ save · quit │
└──────────────┘ └──────────────┘| Mode | What it does | How to enter |
|---|---|---|
| Command mode | Move, delete, copy, paste | Starting state, ESC |
| Insert mode | Actually typing text | i, a, o, I, A, O |
| ex mode | Save, quit, search and replace | : (colon) |
ESC brings you back to command mode. When confused, press ESC first.: in insert mode, a plain colon character is entered.Textbooks and questions call the modes by different names. You only need to know they refer to the same thing.
| Name used in this document | Other names |
|---|---|
| Command mode | command mode, normal mode |
| Insert mode | insert mode, edit mode |
| ex mode | last line mode, colon mode, execute mode |
ESC. That is why there is no arrow between the two boxes in the figure.ESC. ~
~
"test.txt" [New File] ← when you opened a nonexistent file
-- INSERT -- ← after pressing i (vim)
:wq ← after ESC, pressing : makes input appear here
"test.txt" 3L, 42C written ← save result — 3 lines, 42 characters-- INSERT -- it is insert mode, and if there is nothing it is command mode.~ at the start of a line is not file content. It marks the empty screen after the end of the file.| Key | Position relative to the cursor |
|---|---|
i | Insert before the cursor (insert) |
a | Insert after the cursor (append) |
I | Insert at the start of the line |
A | Insert at the end of the line |
o | Create a new line below and insert (open) |
O | Create a new line above and insert |
i and a — before or after is the fork. Original line Linux (cursor is on the n)
i then X → LiXnux before the cursor
a then X → LinXux after the cursor
I then X → XLinux start of the line
A then X → LinuxX end of the lineThere are also keys that enter insert mode while making changes.
| Key | What it does | Mode afterward |
|---|---|---|
r | Replaces just the one character at the cursor with another (replace) | Stays in command mode |
R | Overwrites from the spot where pressed | Keeps overwriting until you press ESC |
s | Deletes the one character at the cursor and enters input | Insert mode |
cw | Deletes from the cursor to the end of the word and enters input (change word) | Insert mode |
cc | Deletes the whole line and enters input | Insert mode |
r does not go to insert mode. It changes one character and stays in command mode. r or x appear in the answer choices as "keys that do not switch to insert mode."| Key | Direction |
|---|---|
h j k l | Left, down, up, right |
0 | The start of the line |
$ | The end of the line |
gg | The first line of the file |
G | The last line of the file |
numberG | To that line number |
w / b | Next / previous word |
h j k l. It follows the keyboard layout: left, down, up, right. The middle two are easy to mix up.| Key | Direction |
|---|---|
^ | The first non-blank character of the line |
e | The end of the word |
H / M / L | The top / middle / bottom line of the screen |
Ctrl+f / Ctrl+b | One screen down (forward) / up (backward) |
Ctrl+d / Ctrl+u | Half a screen down (down) / up (up) |
:number | To that line number (:10) |
:$ | To the last line of the file |
5j is five lines down, and 3w is three words ahead.$ means different things depending on where it is used. $ in command mode is the end of the line, while :$ in ex mode is the last line of the file.gg, 1G, or :1, and the last line is G or :$.G is uppercase, gg is lowercase twice. You can also move with the arrow keys, but the exam asks about h j k l.| Key | What it does |
|---|---|
x | Delete one character at the cursor |
dd | Delete one line (cut) |
numberdd | Delete that many lines — 3dd is three lines |
dw | Delete one word |
yy | Copy one line (yank) |
numberyy | Copy that many lines |
p | Paste below the cursor (paste) |
P | Paste above the cursor |
u | Undo |
Ctrl+r | Redo |
dd is both delete and cut. The deleted content stays in the buffer and can be pasted with p.yy (copy), dd (cut), and p (paste) are a set.| Key | What it does |
|---|---|
X | Delete one character before the cursor |
D | Delete from the cursor to the end of the line (same as d$) |
d0 | Delete from before the cursor to the start of the line |
dG | Delete from the current line to the end of the file |
yw | Copy one word |
J | Join the next line onto the end of the current line (join) |
U | Undo all changes made on that line |
. | Repeat the previous command |
d delete, y copy, c change) + range (w word, $ end of line, G end of file). Typing the same letter twice (dd, yy, cc) targets the whole line.3dd is three lines downward starting from the line where the cursor is. The lines above are untouched.p pastes depends on what is in the buffer. If it is line-based (dd, yy), it goes on the next line down, and if it is character-based (x, dw), it goes after the cursor.| What you want to do | Key sequence |
|---|---|
| Move a line elsewhere | dd → go to the destination → p |
| Duplicate a line | yy → p |
| Copy five lines and paste them at the end of the file | 5yy → G → p |
| Undo the deletion you just made | u |
| Command | What it does |
|---|---|
:w | Save (write) |
:q | Quit (quit) |
:wq | Save and quit |
:q! | Force quit without saving |
:w! | Force save (read-only files, etc.) |
:w filename | Save under another name |
:set nu | Show line numbers |
ZZ | Save and quit (same as :wq, in command mode) |
!means force. If you edit and then do:q, a warning "not saved" appears. To discard changes and leave, use:q!. For questions asking about this command, you need to distinguish it from the wrong answer choices.
vi test.txt # open a file (creates it if it does not exist)
vi +10 test.txt # open at line 10
vi -R test.txt # open read-only E37: No write since last change (add ! to override) ← when you did :q after editing
E45: 'readonly' option is set (add ! to override) ← when you did :w in a file opened read-only! to force it." In the first case it is :q! or :wq, and in the second it is :w!.:w! cannot get past permissions. If a regular user opened a file they have no write permission for, such as /etc/passwd, :w! will not save it either. In that case, leave with :q! and reopen with root privileges.| Command | What it does |
|---|---|
:x | Save and quit — same as :wq, but saves only if something changed |
:wq! | Force save and then quit |
:e filename | Open another file |
:e! | Discard your edits and revert to the last saved state |
:r filename | Insert that file's contents below the cursor (read) |
:!command | Run a shell command without leaving vi — :!ls |
:sh | Step out to a shell temporarily. exit returns you to vi |
:1,5d | Delete lines 1 through 5 |
:w is write, :r is read in. The directions are opposite.ZZ has no colon. You press uppercase Z twice in command mode. It is not an ex command.:q! discards and exits, :e! discards and keeps editing.| Launch | What it does |
|---|---|
vi + file | Open at the last line |
vi +number file | Open at that line number |
vi +/pattern file | Open at the first line where that pattern appears |
vi -R file | Open read-only (same as view file) |
vi -r file | Recover from the swap file the edits from an abnormal exit |
-R (uppercase) is read-only, -r (lowercase) is recovery. One letter's case changes the meaning..test.txt.swp. If the terminal was cut off before you could save, you use this file to bring the work back.Tomorrow we cover vi's search and replace, settings, and other editors.
i, a, o, etc.dd : The command that cuts one line. It stays in the buffer and can be pasted with p.yy : The command that copies one line (yank).:q! : The command that force-quits without saving changes.Click a choice to reveal the answer and explanation.
Question 1
Which of the following is the mode you are in when you first launch the vi editor?
Question 2
Which of the following commands force-quits the vi editor without saving changes?
Question 3
Which of the following commands deletes the entire line the cursor is on in the vi editor?
Question 4
Who developed the vi editor?
Question 5
Which of the following keys creates a new line below the cursor and switches to insert mode in the vi editor?