Categorized | Basics

vi editor Quick Reference


vi  pronounced as ” vee eye ” is a unix editor available on almost all the unix  operating systems , solaris , bsd ,aix , hpux etc.
This document is a quick reference to vi editor and will be of help if your are new to unix , learning unix  or just refreshing your vi knowledge after a few years.

In order to work correctly the vi need  correct terminal type (TERM) setting  .The TERM setting depends on the   type of terminal you have . Commonly used TERM types are vt100 , vt220 and ansi .  In most cases vt100 will  work  fine . In case  vi is not able to understand the TERM you have given, it starts in open mode   giving you a line by line display .
Generally TERM is taken from .profile or  /etc/profile  but can be set at the command line as :
$export TERM

echo $TERM will display the current TERM set.

Create new file or  Open existing file in vi
vi without any file name will open a   new file where you can enter the text and edit but while coming out you will  be asked to enter a valid file name to save the text.  vi with a  file name as argument will  open that file for editing  if the file already exists it opens it  otherwise it creates a new file by the argument.
Example :  $vi  testfile
Creates or opens the existing file called testfile

Modes in vi

vi operates in following  two modes :
i. ) Command Mode : After a file is opened it is opened  in command
mode ,that is , input from the keyboard will be treated as vi commands
and you will not see the words you are typing on the screen .

ii.) Insert Mode: To enter the text you have to put vi in insert  by pressing ‘i’ or ‘a’  after  which you can add the text and whatever is being type will be seen on the  screen. . To switch between these mode Esc key is used .   Esc i
(text mode)  Esc (command mode)

Saving  & Exiting  vi editor
You can exit vi in different ways :

1.) Quit without saving : If you don’t  want to save the work :q  will take you out without saving  your editing in vi.
2.) Write & quit : . Simple :w saves the current file but don’t exit. For save and quit  :wq is  used in vi.
3.) Forced Quite : An ! (Exclamation  sign at the end of  exit commands ( :q! , :wq! )  causes a forced  quit from vi  after ignoring editing (for :q!)  or writing (for :wq!)  all the change

Vi Commands – Reference
Moving Cursor in File
Left h
Right l
Up k
Down j
Beginning ^ or B
end $
Next sentance )
Previous sentance (
Next }
Previous {
Go to end of file :$
on chacter forword :w
One word forword :W
go to a line number :line_number
display file info . ^g
Inserting and appending text
inserts text to the left of cursor i
nserts in the beginning of line I
appends text to right of cursor a
appends to the end of line A
Adding new line
add a new line below the current line o
adds a new line above the current line. O
Deleting the text :
deletes text above the text x
deletes text character on right of cursor X
deletes line 20 20d
deletes current line dd
delete till end of current line. D
Replacing a character & word
replace the character above the cursor. r
replces characters until Esc is pressed. R
replaces the word from cursor to the end indicated by $ sign . cw
replaces till end of line. C
subistutes current charcater. s
substitutes entire line. S
Undo last changes
undo last change. u
undo changes to the current line. U
Copy and pasting lines
copys the current line into buffer. yy
copies 5 lines from the current line. 5yy
pastes the current buffer. p
Searches for the word name in the file :/name
n continues search forward. n
N searches backwards N
saves the text does not quit. :w
saves & quit the editor . :wq!
save ZZ
Quit without saving q!
Search & Replace s/<search>/<replace>/g .
Repeating last command .
Recovering a unsaved vi file.
vi -r filename

Related Posts

  • Checking and Repairing Unix File system with fsckChecking and Repairing Unix File system with fsck (1)
    fsck is a Unix utility for checking and repairing file system inconsistencies. Learn about syntax running phases and troubleshooting with fsck.
  • Crontab – Quick ReferenceCrontab – Quick Reference (17)
    cron is a unix, solaris utility that allows tasks to be automatically run in the background at regular intervals by the cron daemon. Learn setting up cronjobs in Unix and Solaris in this document.
  • Booting Process in SolarisBooting Process in Solaris (0)
    Understanding the booting process is important in the sense that you can get a clear idea when a system faces a booting problem if you are familiar with the booting sequence and steps involved. Lea...
  • Unix Date Format ExamplesUnix Date Format Examples (0)
    Unix date format is needed  in the scripts, timestamped log files and  script generated reports .  This article attempts to simply date formatting with several examples .  Date command in Unix is u...
  • Unix Commands TutorialUnix Commands Tutorial (0)
    A unix command tutorial with some of the most popular and useful commands used in unix operating system arranged by functionality .
  • Unix Tech TipsUnix Tech Tips (0)
    Some tech tips that can save you a lot of time , one liner scripts , find system information etc.

2 Comments For This Post

  1. PCSH Says:

    Hi, I am wondering what is the difference between normal quit (:q or :wq) and forced quit (:q! or :wq!)? What is it forcing to do? To override something? Please help. Thanks!

  2. Eric Says:

    q will exit, but does not exit if there are changes.
    q! will exit with unsaved changes.
    wq will save and exit.

    Looks like a good reference, but there are a few errors:

    20d does not delete line 20, it deletes 20 lines.
    :20d deletes line 20.

    This command will replace the character 1 after the cursor with everything after the first keystroke:
    s///g .
    This command will perform a search and replace on the current line:
    This command will perform a search and replace on the entire document:

    For simplicity (when working with searches involving slashes) I use the modified syntax:

Leave a Reply