Class 11 of 37 • VI Editor Mastery \t\t\t\t

VI Commands Cheat Sheet - Linux / VIM Editor Reference Guide

The VI (Visual Editor) and its modern enhanced descendant VIM (Vi IMproved) are the default screen-oriented text editors installed on virtually every Linux, Unix, and POSIX system. Unlike graphical editors (like VS Code or Notepad), VI is a modal editor operated entirely via keyboard commands without relying on a mouse. This cheat sheet organizes all essential VI commands by operational category for beginner BCA students, Linux system administrators, and software developers.

VI Editor Modes Transition Diagram

Figure 1: State transitions between Command Mode, Insert Mode, and Last-Line (Ex) Mode in VI/VIM.

Closing and Saving Files

When you edit a file in vi, you are actually editing a copy of the file held in a temporary memory buffer rather than the original disk file. The following sections describe commands used when saving a file, quitting vi, or doing both.

Quitting and Saving a File

The uppercase command ZZ (in command mode) will write changes to the disk file and exit vi immediately, returning you to the shell prompt. Alternatively, you can use the colon (:) Ex mode commands listed below:

Sr.No

Command

Descriptions

01

:w

Saves (writes) the buffer contents to the file without quitting vi.

02

:q

Quits vi if no modifications have been made since the last write.

03

:wq or :x

Writes changes to the file and exits vi in one step.

04

ZZ

Quick command-mode shortcut to save and exit immediately (no colon needed).

Quitting without Saving Edits

To discard all edits made to the file during the current session and either start over or quit safely, choose from the following commands:

Sr.No

Command

Descriptions

01

:e!

Discards current modifications and reloads the original file from disk so you can start over.

02

:q!

Forces vi to exit without saving, discarding all unsaved edits made since opening.

Cursor Movement (Command Mode)

You must be in Command Mode to move the cursor. If you were typing text, press the Esc key first. While modern arrow keys work in VIM, the classical UNIX touch-typing keys (h, j, k, l) keep your fingers on the home row for maximum editing speed.

Sr.No

Command

Descriptions

01

h

Move cursor one character to the left.

02

j

Move cursor down one line.

03

k

Move cursor up one line.

04

l

Move cursor one character to the right.

05

w

Jump forward to the beginning of the next word.

06

b

Jump backward to the beginning of the previous word.

07

e

Jump forward to the end of the current word.

08

0 (zero)

Move to the very beginning of the current line.

09

^

Move to the first non-blank character of the current line.

10

$

Move to the end of the current line.

11

gg

Jump to the first line of the document.

12

G

Jump to the last line of the document.

13

:n or nG

Jump directly to line number n (e.g., :45 or 45G).

Screen Movements & Scrolling

To reposition the cursor relative to the visible screen terminal without moving the entire file buffer:

Sr.No

Command

Descriptions

01

H

High: Move cursor to the top line of the screen.

02

M

Middle: Move cursor to the middle line of the screen.

03

L

Low: Move cursor to the bottom line of the screen.

04

Ctrl + F

Scroll forward one full screen (downward).

05

Ctrl + B

Scroll backward one full screen (upward).

06

Ctrl + D

Scroll down half a screen.

07

Ctrl + U

Scroll up half a screen.

Entering Insert & Append Mode

To type text into the buffer, transition from Command Mode into Insert Mode using one of the following key triggers:

Sr.No

Command

Descriptions

01

i

Insert text immediately before the current cursor position.

02

I

Insert text at the beginning of the current line (first non-space character).

03

a

Append text immediately after the current cursor position.

04

A

Append text at the very end of the current line.

05

o

Open a new blank line below the current line and enter insert mode.

06

O

Open a new blank line above the current line and enter insert mode.

07

r

Replace single character under cursor with the next character typed (stays in command mode).

08

R

Enter Overwrite/Replace mode; overwrites characters continuously until Esc is pressed.

Deleting Text

In VI, deleting text automatically cuts it into the unnamed buffer, meaning you can paste it back immediately using the p or P command.

Sr.No

Command

Descriptions

01

x

Delete character directly under the cursor.

02

X

Delete character immediately to the left of the cursor.

03

dd

Delete the entire current line.

04

5dd

Delete 5 consecutive lines starting from the cursor position.

05

dw

Delete from the current character to the start of the next word.

06

d$ or D

Delete from the current character to the end of the line.

07

d0

Delete from the cursor backward to the beginning of the line.

08

cw

Change word: Deletes word and switches immediately into Insert mode.

Copying (Yank) & Pasting (Put)

In VI terminology, copying text is called Yank (y) and pasting text is called Put (p). Yanked text is stored in an internal buffer:

Sr.No

Command

Descriptions

01

yy or Y

Yank (copy) the entire current line into the memory buffer.

02

3yy

Yank (copy) 3 consecutive lines starting from cursor.

03

yw

Yank from the cursor to the end of the current word.

04

y$

Yank from cursor to the end of the line.

05

p

Paste (put) buffer content after cursor (or on the line below for line yanks).

06

P

Paste (put) buffer content before cursor (or on the line above for line yanks).

Search and Replace (Regex in Ex Mode)

Searching and global pattern replacement are among VI's most powerful capabilities, leveraging standard POSIX regular expressions:

Sr.No

Command

Descriptions

01

/pattern

Search forward for the specified pattern or string.

02

?pattern

Search backward for the specified pattern or string.

03

n

Repeat search in the same direction (next occurrence).

04

N

Repeat search in the opposite direction (previous occurrence).

05

:s/old/new/g

Replace all occurrences of 'old' with 'new' in the current line.

06

:%s/old/new/g

Replace all occurrences of 'old' with 'new' throughout the entire file.

07

:%s/old/new/gc

Global replace with interactive confirmation prompt (y/n/a/q) for each match.

08

:set ic / :set noic

Enable / disable case-insensitive search matching.

Visual Mode & Block Editing

VIM introduced Visual Mode, allowing interactive text selection just like using a mouse in modern code editors:

  • v : Enters character-wise Visual mode. Move with arrow keys or h/j/k/l to highlight characters.
  • V : Enters line-wise Visual mode. Highlights full lines. Press d to delete or y to copy highlighted lines.
  • Ctrl + v : Enters Column Block Visual mode. Useful for commenting or indenting multiple lines at once.

Pro Tip: Commenting Multiple Lines in VI / VIM

# 1. Place cursor at the first character of the first line
# 2. Press Ctrl + v to enter Block Visual mode
# 3. Press 'j' down to select the lines you want to comment
# 4. Press Shift + i (capital 'I') to enter Insert mode
# 5. Type '#' (the comment character)
# 6. Press Esc twice -> '#' will appear on all selected lines!

Environment Settings (Ex Mode Commands)

You can customize the VI editing environment during your session by typing : from Command mode:

Setting Command

Description

:set number (or :set nu)

Displays line numbers alongside the left margin.

:set nonumber (or :set nonu)

Hides line numbers from the margin.

:set autoindent (or :set ai)

Enables automatic indentation matching the preceding line.

:set tabstop=4

Sets tab space width to 4 columns (ideal for shell and Python scripts).

:syntax on

Enables code syntax highlighting in VIM for scripts and C programs.

Miscellaneous Essential Commands

Sr.No

Command

Descriptions

01

Ctrl + G

Displays file name, total line count, and cursor position percentage at the bottom status line.

02

. (dot)

Repeats the last modifying command (e.g., repeat delete or insert).

03

u

Undo the most recent editing action.

04

Ctrl + r

Redo the last undone change (VIM).

05

U

Restore current line to its original state before moving cursor away.

06

J

Join the line directly below the cursor onto the end of the current line.

07

~ (tilde)

Toggle character case under cursor between lowercase and uppercase.

08

:sh

Temporarily drop to shell to run commands. Type exit to resume VI.

09

:!command

Execute any Linux command directly from VI (e.g., :!ls -l or :!date).

10

Ctrl + L

Clear and redraw the terminal screen if visual artifacts occur.

Lab Practice Drill: 10-Minute VI Mastery Workout

Follow this exact step-by-step exercise in your Linux terminal to test your muscle memory:

  1. Open terminal and launch vi with a new file: vi practice.txt
  2. Press i to enter Insert Mode. Type the three lines below:
    Linux programming with Bash shell.
    Vi is a modal text editor in Unix.
    Operating systems rely on system calls.
  3. Press Esc to return to Command Mode.
  4. Navigate to line 2 using 2G or j. Yank the entire line using yy.
  5. Move to the bottom of the file using G and press p to paste it as a new line.
  6. Substitute the word "Unix" with "Linux" globally: type :%s/Unix/Linux/g and hit Enter.
  7. Turn on line numbering: type :set nu and hit Enter.
  8. Save changes and exit: type :wq and hit Enter.
  9. Verify file content from shell: cat practice.txt.

Frequently Asked Questions (FAQs)

Press Esc to ensure you are in Command mode. Type :wq (or ZZ) and press Enter. This writes the buffer contents to disk and safely exits to the shell.
Press Esc to return to Command mode, type :q!, and press Enter. The exclamation mark forces vi to abort without recording any modifications.
Press : to enter Ex mode and run :%s/old_word/new_word/g. To prompt for interactive confirmation at each match, add c at the end: :%s/old_word/new_word/gc.
In Command mode, position the cursor on the target line and press dd. To delete 5 consecutive lines, type 5dd.
VI is the original 1976 visual editor written by Bill Joy. VIM (Vi IMproved) was created by Bram Moolenaar in 1991 and introduces syntax highlighting, multi-level undo/redo, visual block selection (Ctrl+v), split windows, and script plugins while maintaining 100% backward compatibility with vi commands.

Next Step in Your Linux Learning Path:

Begin automated programming and explore the 5-part script anatomy in Class 12: Overview of Shell Script.

Continue to Class 12: Overview of Shell Script → Previous: ← Types of Modes | Full Syllabus

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!