Home | Hire | Products | News | Support | Training | Contacting Us | Freelance | Links | Faq

vi text editor

Command mode

This is the mode that you are in when vi opens. This mode enables you to move around the file, delete text, and save the file.

Moving around

The arrow keys will move the cursor one character left and right, or one line up and down.
If the cursor keys do not work (this happens in some terminal windows), you can use the following keys:

h left      j down      k up      l right

Other moves:

w    moves forward one word
W    moves forward one word, ignoring punctuation
b    moves back one word
B    moves back one word, ignoring punctuation
e    move to end of word (or next word)
E    move to end of word (or next word), ignore punc.

0    moves to start of line (zero)
$    moves to end of line
G    moves to the end of the file
nG   moves to line ‘n’
1G   moves to start of document
n    move down ‘n’ lines
}    move down one paragraph
{    move up one paragraph`

H,M,L move to the top, middle or bottom of the screen

/word  find next occurrence of word
?word  as above, but search upwards


Deleting text

x    delete the character under the cursor
nx   delete n characters
dd   delete the current line
ndd  delete n lines (from current line down)
dw   delete the current word
dW   delete the current word, including punctuation
D    delete from current character to end of line
dG   delete from current line to end of file
rchar  replace current character with char

Insert Mode

To add text to a file, you need to be in insert mode. You will stay in insert mode until you press <Esc> to take you back to command mode.

i    start adding text from current cursor position
a   start adding text to the right of the current cursor position (append)
  start adding text at the end of the line
R   start overwriting current text
o   open new line beneath cursor
O   open new line above cursor

cw   replace up to end of current word
cW   as above, including punctuation
c$   replace up to end of current line

Other editing commands:

J    join the next line of text to current line
yy   copy current line into buffer
P    put copied buffer before current line
p    put copied buffer after current line
     (P and p also work with lines deleted by dd)
u    undo previous command

File commands

Commands are issued from the command mode (press escape to make sure you are in command mode) by pressing :
This will open the command line interface at the bottom of the vi screen.

:w        write (save) the file
:w file   write as filename file
:wq       write and quit
:q!       quit vi without saving

The addition of a ! at the end of a write command will force the operation (eg on a read only file)

:set showmode
This will notify you in the bottom right hand corner of the screen when you are in a mode other than command mode.

:set number   Display the line numbers
:set nonumber Turn off line numbers

The set commands can be stored in a .exrc file in your home directory. They will then be applied each time vi is run. (Colons not needed)

<CTRL – g>    Display file information, total lines and current line number

Search and Replace

:s/old/new   Replace first occurrence of old on current line with new

:s/old/new/g  Replace all occurrences of old on current line with new

:1,$s/old/new/g Replace all occurrences of old with new
              (whole file)

:1,$s/old/new/gc Replace all occurrences of old with new, with individual confirmation

Alternatively (and quicker)

:g/string/s/old/new/g

Find lines containing string and then replace all old with new

:g/old/s//new/g
If string to search is also same as to replace 

:g/\<old\>/s//new/g

Only replace if old is a complete word (also with punctuation)

Metacharacters are the same as standard unix

 
back to top
 

Partners
XTFX Partners

XTFX
About Us | Site Map | Privacy Policy | Contact Us | ©2007 XTFX Ltd
Flame Rental UK