VI Basics - Starting Your VI Journey

Welcome to VI

VI (Visual Editor) is one of the most ubiquitous text editors in the UNIX/Linux world. Created by Bill Joy in 1976, it's available on virtually every UNIX-like system you'll encounter. Whether you're editing configuration files on a remote server, working in a minimal environment, or just want a fast, keyboard-driven editor, VI is an essential tool in your sysadmin toolkit.

Why Learn VI?

Understanding VI Modes

The most important concept in VI is that it operates in different modes. This is what confuses most beginners, but once you understand it, everything else makes sense.

The Three Essential Modes

1. Command Mode (Normal Mode)

This is the default mode when you start VI. In this mode, keys perform commands rather than inserting text. You can:

How to get here: Press ESC from any other mode

2. Insert Mode

In this mode, VI behaves like a normal text editor - what you type appears on screen. You enter text and make changes.

How to get here: Press i, a, o, or other insert commands from Command Mode

How to leave: Press ESC to return to Command Mode

3. Last Line Mode (Ex Mode)

This mode is for entering commands that appear at the bottom of the screen. You use it to save files, quit, search and replace, and more.

How to get here: Press : from Command Mode

How to leave: Press ESC or complete your command and press ENTER

Golden Rule: When in doubt, press ESC. It always takes you back to Command Mode, your safe home base.

Starting and Exiting VI

Opening Files

vi filename # Open or create a file
vi +25 filename # Open file at line 25
vi + filename # Open file at last line
vi +/pattern filename # Open file at first occurrence of pattern

Exiting VI

This is famously confusing for beginners. Here are all the ways to exit:

Command What It Does
:q Quit (fails if you have unsaved changes)
:q! Quit without saving (discard changes)
:w Write (save) the file
:wq Write and quit
:x Write and quit (only writes if changes were made)
ZZ Save and quit (from Command Mode, no colon needed)
:w newfile Save as newfile
TRY IT NOW: Open a test file with vi test.txt, type :q and press Enter to exit immediately.

Basic Navigation in Command Mode

Character Movement

Key Movement
h Left one character
j Down one line
k Up one line
l Right one character

Memory Aid: Think of j as pointing down (it has a descender), k pointing up. Or just use arrow keys until you're comfortable.

Word Movement

Key Movement
w Forward to beginning of next word
b Backward to beginning of word
e Forward to end of word
W Forward to next word (whitespace-delimited)
B Backward by word (whitespace-delimited)

Line Movement

Key Movement
0 Beginning of current line
^ First non-whitespace character of line
$ End of current line
+ or Enter First character of next line
- First character of previous line

Screen and File Movement

Key Movement
H Top of screen (High)
M Middle of screen
L Bottom of screen (Low)
Ctrl+f Forward one screen
Ctrl+b Backward one screen
Ctrl+d Down half screen
Ctrl+u Up half screen
G Go to last line of file
gg or 1G Go to first line of file
42G Go to line 42
TRY IT NOW: Open any text file and practice navigating with h, j, k, l. Then try w and b to move by words. Press 0 and $ to jump to line start/end.

Entering Insert Mode

There are several ways to enter Insert Mode from Command Mode. Each positions your cursor differently:

Key Action
i Insert before cursor
a Append after cursor
I Insert at beginning of line
A Append at end of line
o Open new line below current line
O Open new line above current line
Remember: Always press ESC when you're done inserting text to return to Command Mode!
PRACTICE EXERCISE:
  1. Create a new file: vi practice1.txt
  2. Press i to enter Insert Mode
  3. Type: "This is my first line"
  4. Press ESC to return to Command Mode
  5. Press o to open a new line below
  6. Type: "This is my second line"
  7. Press ESC
  8. Press O (capital) to open a line above
  9. Type: "This line goes in between"
  10. Press ESC
  11. Save and exit: :wq

Basic Editing - Delete, Change, and Undo

Deleting Text

Command Action
x Delete character under cursor
X Delete character before cursor
dw Delete word
dd Delete entire line
D Delete from cursor to end of line
d$ Delete to end of line (same as D)
d0 Delete from cursor to beginning of line

Changing Text

The c command is like delete, but it puts you in Insert Mode after deleting:

Command Action
cw Change word (delete word and enter Insert Mode)
cc Change entire line
C Change from cursor to end of line
s Substitute character (delete char and insert)
S Substitute line (same as cc)

Undo and Redo

Command Action
u Undo last change
U Undo all changes to current line
Ctrl+r Redo (vim only, not traditional vi)
TRY IT NOW: Open your practice file. Navigate to a word and press dw to delete it. Press u to undo. Move to a line and press dd to delete it, then u to bring it back.

Copy, Cut, and Paste

In VI, copying is called "yanking" and uses the y command. Pasting uses p.

Yank (Copy)

Command Action
yy Yank (copy) current line
yw Yank word
y$ Yank to end of line
y0 Yank to beginning of line

Put (Paste)

Command Action
p Put after cursor or below current line
P Put before cursor or above current line
Important: When you delete text with dd, dw, etc., that text is automatically placed in a buffer. You can paste it with p. So dd followed by p is effectively a "cut and paste" operation.
PRACTICE EXERCISE:
  1. Open a file with at least 5 lines
  2. Put cursor on line 2 and type yy to yank it
  3. Move to line 4 and type p to paste below
  4. Move to line 1 and type dd to delete it
  5. Move to the last line and type p to paste it
  6. Quit without saving: :q!

Basic Search

Searching is one of the most useful features in VI.

Search Commands

Command Action
/pattern Search forward for pattern
?pattern Search backward for pattern
n Repeat search in same direction
N Repeat search in opposite direction

Character Search on Current Line

Command Action
fx Find next occurrence of character x on line
Fx Find previous occurrence of character x on line
; Repeat last f or F command
, Repeat last f or F command in opposite direction
TRY IT NOW: Open /etc/passwd (read-only) with vi /etc/passwd. Type /root and press Enter to find "root". Press n to find next occurrence. Press :q to exit.

Using Numbers with Commands

You can precede almost any command with a number to repeat it. This is extremely powerful!

3dd # Delete 3 lines
5yy # Yank 5 lines
10j # Move down 10 lines
4w # Move forward 4 words
2dw # Delete 2 words
3x # Delete 3 characters
10i-ESC # Insert "-" 10 times (type 10, then i, then -, then ESC)
PRACTICE EXERCISE:
  1. Create a file with 20 lines of text
  2. Go to line 1: gg
  3. Delete 3 lines: 3dd
  4. Move down 5 lines: 5j
  5. Yank 4 lines: 4yy
  6. Move to end of file: G
  7. Paste: p

Essential Last Line Mode Commands

Remember, you enter Last Line Mode by pressing : from Command Mode.

Command Action
:w Write (save) file
:w filename Save as filename
:q Quit
:q! Quit without saving
:wq Write and quit
:x Write (if changes) and quit
:e filename Edit another file
:r filename Read filename and insert below cursor
:!command Execute shell command
:set number Show line numbers
:set nonumber Hide line numbers

Quick Reference Card

Print or save this for quick reference!

Mode Switching

ESC # Always returns to Command Mode
i, a, o # Enter Insert Mode
: # Enter Last Line Mode

Most Essential Commands

:wq # Save and quit
:q! # Quit without saving
dd # Delete line
yy # Copy line
p # Paste
u # Undo
/text # Search for text
n # Next search result
G # Go to end of file
gg # Go to start of file

Common Beginner Mistakes

  1. Trying to type while in Command Mode: Your text appears as garbled commands. Solution: Press ESC then i
  2. Forgetting which mode you're in: Press ESC to be sure you're in Command Mode
  3. Can't exit VI: ESC then :q! will always get you out
  4. Accidentally entering Replace mode: If text is overwriting instead of inserting, press ESC to get out
  5. Using arrow keys in Insert Mode: This can create weird characters in traditional vi. Use ESC and navigation commands instead

Your First Real-World Task

CHALLENGE: Edit a system configuration file (safely!)
  1. Create a copy of a config file: cp /etc/hosts ~/hosts.backup
  2. Edit the copy: vi ~/hosts.backup
  3. Navigate to the end of the file: G
  4. Add a new line: o
  5. Add a comment: # Test entry
  6. Exit Insert Mode: ESC
  7. Add another line with an entry: o then type: 192.168.1.100 testserver
  8. Exit Insert Mode: ESC
  9. Save and quit: :wq
  10. View your changes: cat ~/hosts.backup

What's Next?

Congratulations! You now know the basics of VI. You can:

The next tutorial will cover the commands you'll use 80% of the time - the efficient, powerful shortcuts that make VI users so fast.

Created for Linux System Administrators | Part 1 of the VI Learning Series

Remember: When in doubt, press ESC!

← Back to VI Index ↑ Back to EXPANDED