vi usage reference

Recently I had to do some work on a Solaris Server. This meant two things: finding a new appreciation for the user friendliness of the linux console and second it meant that I had to use the barebones vi. You know, the one where the arrows and backspace won’t work. Smart. So here’s a little list compiled with the more important functions

First I had to

export TERM=xterm

before vi worked correctly.

Create new file or Open existing file in vi vi without any file name will open a new file where you can enter and edit text and when coming out you will be asked to enter a file name where 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 one.

**Modes ** vi operates in following two modes :

  • 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

- Insert Mode: To enter the text you have to put vi in insert by pressing ‘i’ or ‘a’ after which you can write the text and you will see it on the screen. To switch between these modes press Esc

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

  • Quit without saving : If you don’t want to save the work :q will take you out without saving your editing in vi.
  • Write & quit : . Simple :w saves the current file but don’t exit. For save and quit :wq is used in vi.
  • Forced Quite : An ! after exit commands ( :q! , :wq! ) forces quit from vi after ignoring editing (for :q!) or writing (for :wq!) all the change
(vi) Command
Moving Cursor in File
Lefth
Rightl
Upk
Downj
Line
First^ or B
Last$
Sentence
Next sentence)
Previous sentence(
Paragraph
Next}
Previous{
file
Go to end of file:$
on character forward:w
One word forward:W
go to a line number:line_number
display file info^g
Inserting and appending text
inserts text to the left of cursori
inserts in the beginning of lineI
appends text to right of cursora
appends to the end of lineA
Adding new line
add a new line below the current lineo
adds a new line above the current line.O
Deleting the text :
deletes text above the textx
deletes text character on right of cursorX
deletes line 2525d
deletes current linedd
delete till end of current lineD
Replacing a character & word
replace the character above the cursorr
replaces characters until Esc is pressedR
replaces the word from cursor to the end indicated by $cw
replaces till end of lineC
Substitute
substitutes current characters
substitutes entire lineS
Undo last changes
undo last changeu
undo changes to the current lineU
Copy and pasting lines
copies the current line into bufferyy
copies 5 lines from the current line5yy
pastes the current bufferp
Searching
Searches for name in the file:/name
n continues search forward.n
N searches backwardsN
Saving
saves the text but does not quit:w
saves & quit:wq!
saveZZ
Quit without savingq!
Search & Replaces///g .
Repeat last command.
Recovering an unsaved filevi -r filename

I created this page using vi’s man page and various pages found on the web.