Unix / Linux - The vi Editor
Unix/Linux - The vi/vim Editor Tutorial
Section titled “Unix/Linux - The vi/vim Editor Tutorial”The vi editor (or its enhanced successor, vim - Vi IMproved) is a powerful, screen-oriented text editor ubiquitous across Unix-like systems. While it has a reputation for a steep learning curve, mastering its basics is invaluable for system administration and development.
Why learn vi/vim?
- Availability: It’s almost always present, even on minimal system installs.
- Efficiency: Modal editing allows complex operations with few keystrokes once learned.
- Resourcefulness: It requires minimal system resources.
- Power:
vimoffers extensive features like syntax highlighting, multi-level undo, split windows, and vast plugin support.
This tutorial focuses on essential commands common to both vi and vim. We’ll use vim in examples, as it’s what most users encounter today. If you have vim installed, try running vimtutor for an excellent interactive tutorial.
Starting vim
Section titled “Starting vim”vim filename: Opensfilename. Creates it if it doesn’t exist.vim: Opens vim without a specific file.vim -R filenameorview filename: Opens in read-only mode.
$ vim my_document.txtWhen vim starts, you’ll see the file content (or an empty buffer) with status information at the bottom. Lines starting with ~ indicate they are not part of the file.
Understanding Modes
Section titled “Understanding Modes”Vim’s power (and initial confusion) stems from its modal nature. You are always in one of several modes:
- Normal Mode: The default mode. Used for navigation, commands, deleting, copying, pasting. Keystrokes are interpreted as commands, not text input. Press
Escto return here from other modes. - Insert Mode: Used for typing text directly into the file. Enter Insert Mode using commands like
i,a,o. PressEscto exit Insert Mode and return to Normal Mode. - Visual Mode: Used for selecting text (character-wise
v, line-wiseV, block-wiseCtrl+V). Commands in Normal Mode often operate on the visual selection. PressEscto exit. - Command-Line Mode: Entered by typing
:in Normal Mode. Allows executing extended commands like saving (:w), quitting (:q), searching/replacing (:s), setting options (:set). PressEnterto execute the command,Escto cancel.
Rule of thumb: If unsure what mode you’re in, press Esc once or twice to ensure you are in Normal Mode.
Exiting vim
Section titled “Exiting vim”From Normal Mode, use Command-Line Mode (:) commands:
:q- Quit (fails if there are unsaved changes).:q!- Quit without saving (discard changes). Use with caution!:w- Write (save) the current file.:w filename- Write (save) to a new file namedfilename.:wq- Write (save) and then quit.:x- Write (save) if changes were made, then quit (similar to:wq).
Shortcut from Normal Mode (no : needed):
ZZ- Save and quit (equivalent to:wqor:x).ZQ- Quit without saving (equivalent to:q!).
Basic Navigation (Normal Mode)
Section titled “Basic Navigation (Normal Mode)”Ensure you are in Normal Mode (Esc).
| Keystroke | Action |
|---|---|
h | Move cursor left |
j | Move cursor down |
k | Move cursor up |
l | Move cursor right |
w | Move forward to the start of the next word |
b | Move backward to the start of the previous word |
e | Move forward to the end of the current word |
0 (zero) | Move to the beginning of the current line (first character) |
^ | Move to the first non-whitespace character of the line |
$ | Move to the end of the current line |
gg | Go to the first line of the file |
G | Go to the last line of the file |
[N]G | Go to line number N (e.g., 10G goes to line 10) |
Ctrl+f | Scroll forward (down) one screen |
Ctrl+b | Scroll backward (up) one screen |
Ctrl+d | Scroll down half a screen |
Ctrl+u | Scroll up half a screen |
Many navigation commands can be prefixed with a count (e.g., 5j moves down 5 lines, 3w moves forward 3 words).
Editing Text
Section titled “Editing Text”Entering Insert Mode (from Normal Mode)
Section titled “Entering Insert Mode (from Normal Mode)”| Command | Action |
|---|---|
i | Insert text before the cursor |
I | Insert text at the beginning of the current line |
a | Append text after the cursor |
A | Append text at the end of the current line |
o | Open a new line below the current line and enter Insert Mode |
O | Open a new line above the current line and enter Insert Mode |
Remember to press Esc to return to Normal Mode after inserting text.
Deleting Text (Normal Mode)
Section titled “Deleting Text (Normal Mode)”| Command | Action |
|---|---|
x | Delete character under the cursor |
X | Delete character before the cursor |
dw | Delete from cursor to the beginning of the next word |
db | Delete from cursor backward to the beginning of the word |
dd | Delete (cut) the entire current line |
d$ or D | Delete from cursor to the end of the line |
d0 | Delete from cursor (exclusive) back to beginning of line |
d^ | Delete from cursor (exclusive) back to first non-whitespace char |
Combine d with motion commands (e.g., d2j deletes current and next line, df<char> deletes up to next occurrence of <char>). Prefix with a count (e.g., 3dd deletes 3 lines).
Changing Text (Normal Mode)
Section titled “Changing Text (Normal Mode)”Change commands often delete text and then enter Insert Mode.
| Command | Action |
|---|---|
r<char> | Replace the character under cursor with <char> (stays in Normal Mode) |
R | Enter Replace mode (overwrite characters, Esc to exit) |
cw | Change from cursor to beginning of next word (enters Insert Mode) |
cb | Change from cursor backward to beginning of word (enters Insert Mode) |
cc or S | Change (replace) the entire current line (enters Insert Mode) |
c$ or C | Change from cursor to end of line (enters Insert Mode) |
s | Substitute character under cursor (delete it, then enter Insert Mode) |
Combine c with motions (e.g., c2w change next two words). Prefix with count (e.g., 3cc change 3 lines).
Copy (Yank) and Paste (Put) (Normal Mode)
Section titled “Copy (Yank) and Paste (Put) (Normal Mode)”y stands for ‘yank’ (copy).
| Command | Action |
|---|---|
yw | Yank (copy) from cursor to beginning of next word |
yy or Y | Yank (copy) the entire current line |
y$ | Yank (copy) from cursor to end of line |
p | Put (paste) the yanked/deleted text after the cursor (or below current line if line was yanked) |
P | Put (paste) the yanked/deleted text before the cursor (or above current line if line was yanked) |
Combine y with motions. Use counts (e.g., 3yy yanks 3 lines). Note that d commands also place deleted text into the same buffer for pasting.
Using Visual Mode: Enter Visual Mode (v, V, Ctrl+V), select text using navigation keys, then press d to delete, y to yank, or c to change the selection.
Undo and Redo (Normal Mode)
Section titled “Undo and Redo (Normal Mode)”u- Undo the last change. Vim has multi-level undo; keep pressinguto go further back.Ctrl+r- Redo the last undone change.
Searching and Replacing
Section titled “Searching and Replacing”Searching (Normal Mode)
Section titled “Searching (Normal Mode)”/<pattern>- Search forward for<pattern>. PressEnter.?<pattern>- Search backward for<pattern>. PressEnter.n- Repeat the last search in the same direction.N- Repeat the last search in the opposite direction.
Useful settings (use :set command):
:set ic- Ignore case during search.:set noic- Case-sensitive search (default).:set hls- Highlight all search matches.:set is- Incremental search (show matches as you type).
To clear search highlighting: :noh (no highlight).
Substitution (Command-Line Mode)
Section titled “Substitution (Command-Line Mode)”Syntax: :[range]s/search/replace/[flags]
:s/old/new/- Replace first occurrence ofoldwithnewon the current line.:s/old/new/g- Replace all occurrences ofoldwithnewon the current line (g= global on line).:%s/old/new/g- Replace all occurrences in the entire file (%= range for all lines).:%s/old/new/gc- Replace all occurrences in the file, but confirm (c) each replacement.
Running Shell Commands
Section titled “Running Shell Commands”From Command-Line Mode:
:! command- Executecommandin the shell and display output. Press Enter to return to vim. (e.g.,:! ls -l,:! python script.py):r !command- Read the output ofcommandand insert it below the current line.:w !command- Write the file content as standard input tocommand.
Useful :set Options
Section titled “Useful :set Options”From Command-Line Mode:
:set numberor:set nu- Show line numbers.:set nonumberor:set nonu- Hide line numbers.:set autoindentor:set ai- Automatically indent new lines.:set expandtabor:set et- Use spaces instead of tab characters.:set tabstop=4or:set ts=4- Set tab width to 4 spaces.:set shiftwidth=4or:set sw=4- Set indentation width to 4 spaces.:syntax on- Enable syntax highlighting (usually automatic based on file type).:help option- Get help on a specific option (e.g.,:help tabstop).
To make settings permanent, add them to your vim configuration file, usually ~/.vimrc.
This covers the fundamentals. vim is incredibly deep; explore vimtutor, the built-in help (:help), and online resources to learn more advanced features.