CertNote/ Commute Study Notes
Roadmap
KOEN
CLF-C02 · FoundationalCloud Practitioner - Foundational
DVA-C02 · AssociateDeveloper - Associate
SAA-C03 · AssociateSolutions Architect - Associate
SOA-C02 · AssociateCloudOps Engineer - Associate
SAP-C02 · ProfessionalSolutions Architect - Professional
DOP-C02 · ProfessionalDevOps Engineer - Professional
Linux Master 2 · 2급KAIT Linux Master - Level 2
  • Week 1
    • 1.Operating Systems and the Kernel: What Linux Does
    • 2.The Birth of Linux: From Unix to Linus
    • 3.Free Software and Open Source: What a License Decides
    • 4.Distributions: The Unit That Bundles the Kernel and Tools for Sale
    • 5.Week 1 Review: The Identity Behind the Name Linux
  • Week 2
  • Week 3
  • Week 4
  • Week 5
  • Week 6
    • 1.The Three Modes of the vi Editor
    • 2.vi Search and Replace, and Other Editors
    • 3.Process Concepts and Types
    • 4.Process Commands and Job Scheduling
    • 5.Week 6 Comprehensive Review: Editors and Processes
  • Week 7
  • Week 8
  • Week 9
  • Week 10
  • Week 11
SCS-C03 · SpecialtySecurity - Specialty
Linux Master 1 · 1급KAIT Linux Master - Level 1
MLA-C01 · AssociateMachine Learning Engineer - Associate
AIF-C01 · FoundationalAI Practitioner - Foundational
DEA-C01 · AssociateData Engineer - Associate
MLS-C01 · SpecialtyMachine Learning - Specialty
SQLD · 개발자SQL Developer (SQLD)

© CertNote · 주간랩스

  • About
  • Changelog
  • Contact
  • Terms
  • Privacy
← Linux Master 2/Week 6/Day 1

Based on the Linux Master 2 exam syllabus·Report an error

Linux Master 2· AssociateWeek 6 · Day 1~21 min read

Day 1 - The Three Modes of the vi Editor

📌 Key Points

  • Editors are divided into line editors that work one line at a time (ed, ex) and screen editors that work on the whole screen (vi, emacs).
  • vi was created by Bill Joy. He is the same person who created the C shell (csh).
  • vi has three modes: command mode, insert mode, and ex mode. It starts in command mode.
  • You switch modes with three keys: i (to insert), ESC (to command), and : (to ex).
  • Save with :w, quit with :q, save and quit with :wq, and force quit with :q!.

Branches of Editors

CategoryUnit it works onExamples
Line editorOne line at a timeed, ex, sed
Screen editorThe whole screenvi, vim, emacs, nano, pico
EditorFeatures
viDeveloped by Bill Joy. Included by default on almost every Unix
vimVi IMproved — an improved version of vi. The current standard on Linux
emacsDeveloped by Richard Stallman. Extremely extensible
picoFor beginners. Simple
nanoAn improved version of pico (GNU). Shortcut keys are shown at the bottom of the screen
geditThe 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.

How They Came About

   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)
  • vi is ex with a "visual mode for editing while looking at the screen" attached. The name also comes from visual. The commands you call with : 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.
  • On modern Linux, running vi usually launches vim in reality. Depending on the distribution, a reduced-feature version may be installed.
Runtime environmentEditors
Console (terminal)vi, vim, emacs, nano, pico
Graphical (X Window)gedit (GNOME), kate and kwrite (KDE), emacs
  • Questions that ask you to pick the one of a different kind come in one of two criteria: line/screen or console/graphical.

The Three Modes

              ┌──────────────────┐
              │   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   │
      └──────────────┘   └──────────────┘
ModeWhat it doesHow to enter
Command modeMove, delete, copy, pasteStarting state, ESC
Insert modeActually typing texti, a, o, I, A, O
ex modeSave, quit, search and replace: (colon)
  • When you open vi, you are in command mode. If you start typing right away, the characters are not entered but interpreted as commands. This is where beginners get most flustered.
  • Whatever mode you are in, pressing ESC brings you back to command mode. When confused, press ESC first.
  • You can enter ex mode only from command mode. If you type : in insert mode, a plain colon character is entered.

Same Mode, Different Names

Textbooks and questions call the modes by different names. You only need to know they refer to the same thing.

Name used in this documentOther names
Command modecommand mode, normal mode
Insert modeinsert mode, edit mode
ex modelast line mode, colon mode, execute mode
  • There is no direct route between insert mode and ex mode. You must go through command mode with ESC. That is why there is no arrow between the two boxes in the figure.
  • A command in ex mode runs when you press Enter and automatically returns you to command mode. To cancel, press ESC.

Reading the Bottom Line of the Screen

   ~
   ~
   "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
  • The bottom line tells you the current state. In vim, if you see -- INSERT -- it is insert mode, and if there is nothing it is command mode.
  • The ~ at the start of a line is not file content. It marks the empty screen after the end of the file.
  • ex mode is called "last line mode" because the command is typed on the last line of the screen.

Keys That Enter Insert Mode

KeyPosition relative to the cursor
iInsert before the cursor (insert)
aInsert after the cursor (append)
IInsert at the start of the line
AInsert at the end of the line
oCreate a new line below and insert (open)
OCreate a new line above and insert
  • Lowercase and uppercase come in pairs. Lowercase works near the cursor, uppercase works relative to the whole line.
  • The difference between 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 line

There are also keys that enter insert mode while making changes.

KeyWhat it doesMode afterward
rReplaces just the one character at the cursor with another (replace)Stays in command mode
ROverwrites from the spot where pressedKeeps overwriting until you press ESC
sDeletes the one character at the cursor and enters inputInsert mode
cwDeletes from the cursor to the end of the word and enters input (change word)Insert mode
ccDeletes the whole line and enters inputInsert 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."

What You Do in Command Mode

Moving

KeyDirection
h j k lLeft, down, up, right
0The start of the line
$The end of the line
ggThe first line of the file
GThe last line of the file
numberGTo that line number
w / bNext / previous word
  • Questions ask for the order of h j k l. It follows the keyboard layout: left, down, up, right. The middle two are easy to mix up.
KeyDirection
^The first non-blank character of the line
eThe end of the word
H / M / LThe top / middle / bottom line of the screen
Ctrl+f / Ctrl+bOne screen down (forward) / up (backward)
Ctrl+d / Ctrl+uHalf a screen down (down) / up (up)
:numberTo that line number (:10)
:$To the last line of the file
  • Putting a number in front repeats it that many times. 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.
  • There are several routes to the same place — the first line is 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.

Delete, Copy, Paste

KeyWhat it does
xDelete one character at the cursor
ddDelete one line (cut)
numberddDelete that many lines — 3dd is three lines
dwDelete one word
yyCopy one line (yank)
numberyyCopy that many lines
pPaste below the cursor (paste)
PPaste above the cursor
uUndo
Ctrl+rRedo
  • 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.
KeyWhat it does
XDelete one character before the cursor
DDelete from the cursor to the end of the line (same as d$)
d0Delete from before the cursor to the start of the line
dGDelete from the current line to the end of the file
ywCopy one word
JJoin the next line onto the end of the current line (join)
UUndo all changes made on that line
.Repeat the previous command
  • There is one rule — action (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.
  • Where 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 doKey sequence
Move a line elsewheredd → go to the destination → p
Duplicate a lineyy → p
Copy five lines and paste them at the end of the file5yy → G → p
Undo the deletion you just madeu

ex Mode — Saving and Quitting

CommandWhat it does
:wSave (write)
:qQuit (quit)
:wqSave and quit
:q!Force quit without saving
:w!Force save (read-only files, etc.)
:w filenameSave under another name
:set nuShow line numbers
ZZSave 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

Warning Messages and the Exclamation Mark

   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
  • Both messages tell you "add ! to force it." In the first case it is :q! or :wq, and in the second it is :w!.
  • However, even :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.
CommandWhat it does
:xSave and quit — same as :wq, but saves only if something changed
:wq!Force save and then quit
:e filenameOpen another file
:e!Discard your edits and revert to the last saved state
:r filenameInsert that file's contents below the cursor (read)
:!commandRun a shell command without leaving vi — :!ls
:shStep out to a shell temporarily. exit returns you to vi
:1,5dDelete 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.
  • There are two ways to discard changes — :q! discards and exits, :e! discards and keeps editing.

Options Given at Launch

LaunchWhat it does
vi + fileOpen at the last line
vi +number fileOpen at that line number
vi +/pattern fileOpen at the first line where that pattern appears
vi -R fileOpen read-only (same as view file)
vi -r fileRecover 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.
  • While you edit, vim creates a hidden swap file such as .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.

📖 Glossary

  • Line editor : An editor that edits one line at a time. ed and ex belong here.
  • Screen editor : An editor that edits while viewing the whole screen. vi, emacs, and nano belong here.
  • Command mode : The mode you are in when you first launch vi. You move, delete, and copy.
  • Insert mode : The mode where you actually type text. Entered with i, a, o, etc.
  • ex mode : The mode entered with a colon. It performs saving, quitting, and substitution.
  • 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.

📝 Practice Questions

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?

PreviousWeek 5 Comprehensive Review: The Shell on One PageWeek 5 · Day 5Next vi Search and Replace, and Other EditorsWeek 6 · Day 2

Official references

  • Linux Master 2 official exam syllabus(opens in a new tab)

On this page

  • Key Points
  • Branches of Editors
  • How They Came About
  • The Three Modes
  • Same Mode, Different Names
  • Reading the Bottom Line of the Screen
  • Keys That Enter Insert Mode
  • What You Do in Command Mode
  • Moving
  • Delete, Copy, Paste
  • ex Mode — Saving and Quitting
  • Warning Messages and the Exclamation Mark
  • Options Given at Launch
  • Glossary
  • Practice Questions