VI Basic Commands for UNIX in Nutshell

Quick How To Guide

Vi has 3 basic modes of operation: command (default), input, last line mode.

Command mode

In command mode you can run commands to search, copy, move, remove text.

Input mode

In input mode you can insert text into file. Everything you type will be interpreted as text. many ways how to activate input mode (vi is case sensitive):
  • i - Inserts text before the cursor.
  • I - Inserts text at the beggining of the line.
  • o - Opens a new blank line below the cursor.
  • O - Opens a new blank line above the cursor.
  • a - Appends text after the cursor.
  • A - Appends text at the end of the line.

Last line mode

To get into last line mode type ':' only from command mode. After type ':' you will see a colon character appear at the beginning of the last line of your vi editor. It means vi is ready for type a "last line command". To end vi type 'q' from last line mode.

You can return to command mode from input or last line mode pressing Esc.

Moving the cursor

Key Cursor movement
w Forward one word.
b Back one word.
e To the end of the current word.
$ To the end of the line.
0(zero) To the beginning of the line.
^ To the first non-white space character on the line.
G Goes to the last line of the file.
lG Goes to the first line of the file.
Ctrl + F Pages forward one screen.
Ctrl + B Pages back one screen.
Ctrl + D Scrolls down one-half screen.
Ctrl + U Scrolls up one-half screen.
Ctrl +L Refreshes the screen.

Text-deletion commands

Command Function
R Overwrites or replaces characters on the line at and to the right of cursor. To terminate press Esc.
C Changes or overwrites characters from cursor to the end of the line.
s Substitutes a string for a character at the cursor.
x Deletes a character at the cursor.
dw Deletes a word or part of the word to the right of the cursor.
dd Deletes the line containing the cursor.
D Deletes the line from the cursor to the right end of the line.
:n, nd Deletes lines n-n. Example :2,80d deletes lines 2-80.

Text-changing commands

Command Function
cw Changes or overwrites characters at the cursor location to the end of that word.
r Replaces the character at the cursor with one other character.
J Join the current line and the line below.
xp Transposes the character at the cursor and the character to the right of the cursor.
~ Changes the case of the letter, either uppercase or lowercase, at the cursor.
u Undo the previous command.
. Repeats previous command.

Text-replacing commands

Command Function
/string Searches forward for the string from cursor.
?string Searches backward for the string.
n Searches for the next occurrence of the string. Use this command after searching for a string.
N Searches for the previous occurrence of the string. Use this command after searching for a string.
:%s/old/new/g Searches for the old string and replaces it iwth the new string globally.

Copy and paste commands

Command Function
yy Yanks a copy of line
p Puts yanked or deleted text under the line containing the cursor.
P Put
:n,n co n Copies lines n-n and puts them after line n. Example :1,5 co 8 copies lines 1-5 and puts them after line 8.
:n,n m n Moves lines n-n to line n. Example :1,5 m 8 moves lines 1-5 to the line 8.

File save and quit commands

Command Function
:w Saves the file with changes by writing to the disk
:w new_file Writes the contents of the buffer to new_file.
wq Saves the changed file and quits editor vi.
:x Saves the changed file and quits editor vi.
ZZ Saves the changed file and quits editor vi.
:q! Quits without saving changes.

Customizing vi session

Command Function
:set nu Shows line numbers.
:set nonu Hides line numbers.
:set ic Instructs searches to ignore case.
:set noic Instructs searches to be case sensitive.
set list Display invisible characters.
:set showmode Display the current mode of operation.
:set noshowmode Turns off the mode of operation display.
:set Displays all the vi variables that are set.
:set all Display all vi variables and their values.

Customizing vi session

To automatic customization for all vi session do following steps:
  1. Create a file in your home directory named ' . exrc'
  2. Enter any of the set variables into the ' . exrc' file.
  3. Enter each 'set variable' command on one line.
Vi reads 'exrc' file every time before starting vi session.
Command Function
:set nu Shows line numbers.
:set nonu Hides line numbers.
:set ic Instructs searches to ignore case.
:set noic Instructs searches to be case sensitive.
set list Display invisible characters.
:set showmode Display the current mode of operation.
:set noshowmode Turns off the mode of operation display.
:set Displays all the vi variables that are set.
:set all Display all vi variables and their values.