Skip to content

Vi Text Editor

Introduction to the Vi Editor

  • Before vi, UNIX used a line editor that allowed editing one line at a time.
  • vi (Visual Editor) was created by Bill Joy in 1976.
  • It is the default editor in Linux/UNIX systems and is case-sensitive.
  • vim (Vi IMproved) is an advanced version of vi.
  • Used for creating, editing, or viewing text files.
  • Not a text formatter (unlike MS Word); no margins or formatting controls.

Characteristics of Vi

  • Powerful and universally available on UNIX systems.
  • Harder to learn for new users, especially Windows users.
  • Allows movement and modification anywhere in a file.
  • Case-sensitive commands.

Starting Vi

bash
vi filename
  • Creates filename if it doesn't exist, otherwise opens it.
  • Opens in Command Mode by default.

Modes of Operation

Vi has three modes:

  1. Command Mode – Default mode for navigation and commands.
  2. Insert Mode – Used to type and edit text.
  3. Last Line Mode (Escape Mode) – Used to save, quit, or execute commands.

Command Mode

  • Activated by pressing Esc.
  • Keystrokes are interpreted as commands (not shown on screen).
  • Used for navigation, deletion, copying, and pasting text.
  • Pressing Esc again makes vi beep or flash if already in command mode.

Insert Mode

  • Entered from Command Mode by pressing i.
  • Allows text input and editing.
  • Exit by pressing Esc to return to Command Mode.

Example

bash
vi vifile
i
This file is being created using the vi editor.
<Esc>

Last Line Mode (Escape Mode)

  • Invoked by typing a colon : in Command Mode.
  • Cursor moves to the bottom for entering commands.

Common Commands

CommandAction
:qQuit
:q!Quit without saving
:wSave file
:w!Force save file
:w filenameSave file as filename
:wq or :xSave and quit
ZZSave and quit

Input Mode Shortcuts

CommandAction
iInsert before cursor
IInsert at start of line
aAppend after cursor
AAppend at end of line
oOpen new line below
OOpen new line above
rReplace one character
ROverwrite text

Basic Movement

CommandAction
hMove left
jMove down
kMove up
lMove right

Word Navigation

CommandAction
wMove to next word
bMove to previous word
eMove to end of word
n wMove forward n words
dwDelete one word
ywCopy one word
n dwDelete n words

Line Navigation

CommandAction
0, |, or ^Move to start of line
$Move to end of line
30 |Move to column 30

Scrolling

CommandAction
Ctrl+fScroll one page forward
Ctrl+bScroll one page backward
Ctrl+dScroll half page forward
Ctrl+uScroll half page backward
10 Ctrl+fScroll 10 pages forward

Absolute Movement

CommandAction
Ctrl+gShow current line number
GGo to end of file
1G or ggGo to first line
nGGo to line n (e.g., 40G)

Cut, Copy, and Paste

Commands

CommandAction
ddCut (delete) current line
yyCopy (yank) current line
pPaste below cursor
PPaste above cursor
n ddDelete n lines
n yyCopy n lines

Deleting Text

CommandAction
xDelete current character
XDelete previous character
xpSwap two characters
dwDelete to end of word
d$ or DDelete to end of line
d0Delete to start of line
ddDelete whole line
:20,40dDelete lines 20 to 40
dGDelete to end of file

Editing and Joining Lines

CommandAction
JJoin current and next line
4JJoin 4 lines
yypDuplicate current line
ddpSwap two lines

Undo and Repeat

CommandAction
uUndo last change
ctrl+rRedo last change
UUndo all changes on current line
.Repeat last command

Searching for a Pattern

Search Commands

CommandAction
/stringSearch forward
?stringSearch backward
nGo to next match (same direction)
NGo to previous match (opposite direction)
/^string/Match a line starting with string
/string$/Match a line ending with string
/\<word\>Match exact word
/pl[abc]ceMatch patterns like place, plbce, plcce

Replace Text

CommandAction
:1,$ s/old/new/Replace first occurrence in the entire file
:3,6 s/old/new/gReplace all matches in lines 3–6
g (modifier)Makes the substitution global on each line

Text Buffers

CommandAction
"addDelete line to buffer a
"apPaste from buffer a

Set Commands

CommandDescription
:set icIgnore case when searching
:set aiEnable auto-indent
:set noaiDisable auto-indent
:set nuShow line numbers
:set sw=4Set shift width to 4 spaces
:set wsEnable wrapscan (search wraps to top)
:set wm=2Enable word wrap with margin of 2
:set roSet file as read-only
:set termDisplay terminal type
:set bfDiscard control characters

Summary

The vi editor is a powerful, universal text editor in UNIX/Linux.
It operates in three modes, supports efficient navigation, editing, and text manipulation, and offers customization through set commands. Mastering vi enhances productivity and command-line efficiency.