6.7 vi / vim Modes, plus nano, Emacs & EDITOR Awareness (103.8)
Key Takeaways
- `vi` functions as a modal text editor with three fundamental modes: Command Mode (Normal Mode, for navigation and manipulation), Insert Mode (for entering text), and Ex Mode / Command-Line Mode (for saving, quitting, and global substitution).
- Insert Mode is entered from Command Mode via `i` (insert before cursor), `I` (line start), `a` (append after cursor), `A` (line end), `o` (open line below), or `O` (open line above) and left with `<Esc>`; Command Mode navigation then uses `h`/`j`/`k`/`l` (left/down/up/right), `0`/`^`/`$` (line boundaries), `w`/`b`/`e` (word motions), and `gg`/`G`/`<N>G` (file jumps).
- Text manipulation uses operator-motion combinations: delete (`x`, `dw`, `dd`, `d$`), copy/yank (`yy`, `yw`, `y$`), paste (`p` below, `P` above), and undo (`u`, `Ctrl+R`).
- Ex Mode (`:`) manages file persistence and global modifications: write/save (`:w`, `:wq`, `:x`), exit (`:q`, `:q!`), save and exit via normal mode (`ZZ`), search (`/pattern`, `?pattern`), and global search-and-replace (`:%s/find/replace/g`).
- Objective 103.8 requires awareness of nano (Ctrl+O saves, Ctrl+X exits) and Emacs (Ctrl+x Ctrl+s saves, Ctrl+x Ctrl+c exits) alongside vi, and the default editor is selected by the VISUAL variable first, falling back to EDITOR.
6.7 vi / vim Editor Essentials & Modes
Quick Summary: The
vieditor (and its enhanced descendantvim) is the universal, POSIX-mandated visual text editor present by default across all Linux distributions.viis a modal editor operating in three distinct states: Command (Normal) Mode for navigation, copying, and deletion; Insert Mode for entering text; and Ex (Command-Line) Mode (prefixed with:) for file I/O, search, configuration, and regular expression substitution (:%s/find/replace/g).
1. The Three Operational Modes of vi
Unlike conventional modeless text editors (such as nano), keystrokes in vi perform radically different operations depending on the currently active mode:
vi Modal State Transitions:
┌──────────────────────────────┐
│ Command Mode (Normal Mode) │
│ (Default mode upon launch) │
└───────┬──────────────▲───────┘
Insert Keys: │ │ Exit Insert Mode:
`i`, `I`, `a`, `A`, │ │ `<Esc>` key
`o`, `O`, `s`, `S`, `C` ▼ │
┌──────────────────────────────┐
│ Insert Mode │
│ (Typing / Editing buffer) │
└──────────────────────────────┘
▲ │
Command completes │ │ Enter Ex Mode:
or `<Esc>` pressed │ │ `:` (Colon), `/`, `?`
┌───────┴──────────────▼───────┐
│ Ex Mode (Command-Line) │
│ (`:w`, `:q!`, `:%s///g`) │
└──────────────────────────────┘
Mode Definitions
- Command Mode (Normal Mode): The default landing mode when opening any file (
vi filename). Keystrokes are interpreted as navigation, text manipulation, deletion, yanking (copying), or mode-switching commands. - Insert Mode: Active text entry mode where typed characters are inserted directly into the text buffer. Indicated by
-- INSERT --at the bottom left invim. - Ex Mode (Command-Line Mode): Invoked from Command Mode by typing a colon (
:), forward slash (/), or question mark (?). Displays a command line prompt at the bottom of the screen to execute file operations, search patterns, buffer writes, or environment configuration.
2. Entering Insert Mode from Command Mode
Transitioning from Command Mode to Insert Mode is accomplished using specific single-letter keystrokes that simultaneously set the cursor position:
| Command | Insertion Behavior & Cursor Placement |
|---|---|
i | Insert: Enters insert mode immediately before the current cursor character. |
I | Insert at Line Start: Enters insert mode at the first non-whitespace character of the current line. |
a | Append: Enters insert mode immediately after the current cursor character. |
A | Append at Line End: Enters insert mode at the very end of the current line. |
o | Open Line Below: Opens a new blank line below the current line and enters insert mode. |
O | Open Line Above: Opens a new blank line above the current line and enters insert mode. |
s | Substitute Character: Deletes the single character under the cursor and enters insert mode. |
S | Substitute Line: Deletes the entire current line and enters insert mode. |
C | Change to End of Line: Deletes from cursor to the end of the line (c$) and enters insert mode. |
cw | Change Word: Deletes from cursor to the end of the current word and enters insert mode. |
Exam Rule: To return to Command Mode from Insert Mode or Ex Mode, press the
<Esc>key.
3. Navigation in Command Mode
Efficient navigation in vi eliminates the need for arrow keys or mouse movement:
Directional & Character Navigation
h: Move cursor one character to the left.j: Move cursor one line down.k: Move cursor one line up.l: Move cursor one character to the right.
In-Line Positioning
0(Zero): Jump to the absolute beginning of the current line (column 0).^: Jump to the first non-blank (whitespace) character on the current line.$: Jump to the end of the current line.
Word-Level Navigation
w: Jump forward to the start of the next word.W: Jump forward to the start of the next space-delimited WORD.b: Jump backward to the start of the previous word.B: Jump backward to the start of the previous space-delimited WORD.e: Jump forward to the end of the current or next word.
File-Wide Jumps & Screen Paging
G: Jump to the last line of the file.1Gorgg: Jump to the first line of the file.<N>G(e.g.,45G): Jump directly to line number<N>.Ctrl+F: Scroll forward one full screen page.Ctrl+B: Scroll backward one full screen page.Ctrl+D: Scroll down half a screen page.Ctrl+U: Scroll up half a screen page.H/M/L: Jump to High (top), Middle, or Low (bottom) line of current screen display.
4. Text Manipulation: Delete, Yank, Paste & Undo
In vi, deletion operations double as "cut" operations, placing deleted content into the default yank buffer.
Deletion Operators (Command Mode)
| Command | Deletion Action |
|---|---|
x | Delete single character under the cursor. |
X | Delete character immediately before the cursor. |
dw | Delete from cursor to the beginning of next word. |
d$ or D | Delete from cursor to the end of the current line. |
d0 | Delete from cursor to the beginning of the line. |
dd | Delete (cut) entire current line. |
<N>dd (e.g., 5dd) | Delete <N> lines starting from current line. |
dG | Delete from current line all the way to the end of the file. |
d1G or dgg | Delete from current line all the way to the first line of the file. |
Yanking (Copying) and Pasting
yy(orY): Yank (copy) the entire current line into the buffer.<N>yy(e.g.,3yy): Yank<N>lines starting from current line.yw: Yank word from cursor.y$: Yank from cursor to the end of the line.p(lowercase): Paste buffer contents below the current line (or after cursor).P(uppercase): Paste buffer contents above the current line (or before cursor).
Undo, Redo, and Repeat
u: Undo the most recent modification.Ctrl+R: Redo the previously undone change (vim).U: Undo all modifications made on the current line..(Period): Repeat the last text-modifying editing command.r<char>: Replace character under cursor with<char>without entering insert mode.R: Enter Replace mode (overwrites text continuously until<Esc>is pressed).J: Join the current line with the line immediately below it.
5. Ex Mode (:): File Management, Configuration & Search/Replace
Typing : in Command Mode places the cursor on the bottom status line to execute Ex commands.
Saving, Quitting & Exiting
| Command | Action Performed |
|---|---|
:w | Write (save) buffer changes to disk. |
:w <filename> | Write buffer to a new file path (Save As). |
:w! | Force write to write-protected file (if permissions allow). |
:q | Quit vi; fails if unsaved changes exist. |
:q! | Force quit, immediately discarding all unsaved changes. |
:wq or :x | Write and quit (save changes and exit). |
ZZ | Command Mode Shortcut: Save changes and exit (no : required). |
ZQ | Command Mode Shortcut: Quit without saving changes (equivalent to :q!). |
Editor Environment Settings (:set)
:set number # (or :set nu) Display line numbers
:set nonumber # (or :set nonu) Hide line numbers
:set ignorecase # (or :set ic) Case-insensitive pattern searching
:set noignorecase# (or :set noic) Case-sensitive pattern searching
:set hlsearch # Highlight all search pattern matches
:set nohlsearch # Disable search highlighting
:set autoindent # Enable automatic indentation on new lines
:set all # Display list of all editor settings
Pattern Searching
/pattern: Search forward through buffer forpattern.?pattern: Search backward through buffer forpattern.n: Repeat search for next match in the same direction.N: Repeat search for next match in the opposite direction.
Global Search and Replace (Substitution)
The Ex substitution command utilizes the general syntax:
# 1. Replace first occurrence of 'http' with 'https' on the current line:
:s/http/https/
# 2. Replace ALL occurrences of 'http' with 'https' on the current line:
:s/http/https/g
# 3. Replace all occurrences across lines 10 through 50:
:10,50s/old_param/new_param/g
# 4. Replace ALL occurrences across the ENTIRE file (% represents 1,$):
:%s/eth0/ens33/g
# 5. Replace across entire file with interactive confirmation prompt for each match:
:%s/debug=false/debug=true/gc
# 6. Using alternative delimiter (#) to avoid escaping directory slashes:
:%s#/var/log#/opt/logs#g
💡 LPIC-1 Exam Fill-in-the-Blank Alert: What single command-mode keystroke in
viundoes the most recent change? Answer:u
6. The Other Editors LPI Expects You to Recognise
Objective 103.8 requires you to navigate and edit in vi, but its Key Knowledge Areas also demand awareness of Emacs, nano and vim, plus the ability to configure the standard editor. You will not be asked to edit a file in Emacs — you will be asked to identify it, or to name the variable that selects a default editor.
| Editor | Character | What to recognise |
|---|---|---|
vi | The POSIX-mandated visual editor. Modal (Command / Insert / Ex). | Guaranteed present on every Unix-like system, including rescue shells and minimal containers. This is why LPI tests it and not the others. |
vim | "Vi IMproved" — a superset of vi with syntax highlighting, multi-level undo, split windows, and ~/.vimrc. | On most distributions vi is a symlink or alias to vim (or to vim.tiny). Everything you know from vi works unchanged. |
nano | Non-modal, beginner-friendly, based on Pico. Commands are Ctrl-key chords shown in a permanent on-screen footer. | Ctrl+O writes the file ("Output"), Ctrl+X exits, Ctrl+W searches ("Where is"), Ctrl+K cuts a line. Note it is not Ctrl+S/Ctrl+Q. |
emacs | Non-modal, extensible, Lisp-programmable environment that happens to edit text. | Chorded commands: Ctrl+x Ctrl+s saves, Ctrl+x Ctrl+c exits, Ctrl+g cancels the pending command. Configured in ~/.emacs or ~/.emacs.d/init.el. Not installed by default on most server distributions. |
Configuring the Standard Editor
Many programs — crontab -e, visudo, git commit, systemctl edit, less -v — need to launch "the user's editor." They resolve it from environment variables, checking VISUAL first and falling back to EDITOR:
# Set for the current shell only
$ export EDITOR=/usr/bin/vim
$ export VISUAL=/usr/bin/vim
# Make it permanent for one user
$ echo 'export EDITOR=/usr/bin/vim' >> ~/.bashrc
# Make it permanent system-wide
# echo 'export EDITOR=/usr/bin/nano' > /etc/profile.d/editor.sh
# Override for a single invocation
$ EDITOR=nano crontab -e
Debian and Ubuntu add a distribution-specific layer on top: the /usr/bin/editor symlink, managed by the alternatives system.
# Interactively choose the system-wide default editor on Debian/Ubuntu
# update-alternatives --config editor
# Show which binary /usr/bin/editor currently points at
$ readlink -f /usr/bin/editor
/usr/bin/vim.basic
Exam Rule:
VISUALtakes precedence overEDITORwhen both are set.VISUALhistorically meant "a full-screen editor is usable on this terminal" whileEDITORmeant "line editor safe on any terminal"; in practice both are set to the same value. If neither variable is set, most tools fall back to a compiled-in default — commonlyvion Red Hat family systems andnanoon Debian family systems.
Exam Rule: If a question describes an editor whose commands are
Ctrl+Oto save andCtrl+Xto exit with a two-line help footer, the answer is nano. If the commands areCtrl+x Ctrl+sandCtrl+x Ctrl+c, the answer is Emacs. If the editor has separate modes and:wqsaves and quits, the answer is vi/vim.
While in vi Command (Normal) Mode, which keystroke opens a new blank line directly ABOVE the current cursor line and immediately places the editor into Insert Mode?
Which vi Ex Mode command replaces every occurrence of the string 192.168.1.10 with 10.0.0.1 throughout the entire file, prompting the user for confirmation before each replacement?
An administrator working in vi Command (Normal) Mode wishes to save all modifications and exit the editor immediately without typing a colon (:) to enter Ex Mode. Which two-character keystroke sequence executes this?