vi Survival Kit


Kostis Netzwerkberatung
Konstantinos Kostis
Talstr. 25, D-63322 Rödermark
Tel. +49 6074 881056, FAX 881058
http://www.kostis.net/, kosta@kostis.net

You may use this information free of charge at your own risk

1999-07-05


vi Basics

In order to understand vi it is important to know that vi is comprised of two parts: ex and vi. ex is a line based editor built into vi using commands just like MS-DOS edlin. Because of this vi has three modes: Movement Mode, Edit Mode and Command Mode. When started vi is in Movement Mode. vi needs to be told which terminal type you are using. Normally this is detected during login and stored in the environment variable TERM but Microsoft's telnet is often detected as "ansi" when it really should be "vt100". In order to change this type before starting vi: export TERM=vt100

vi Movement Mode

In Movement Mode you can move the cursor, delete lines relative to your cursor and many more things. Some actions in Movement Mode will switch vi to Edit Mode or Command Mode.

Keys   Function
dnd   delete n lines of text at cursor position
if n is omitted only the current line is deleted.
x   delete character under cursor
r   replace character under cursor with next key pressed
R   enter Edit Mode and Replace at current position
i   enter Edit Mode and Insert at current position
o   insert line below current line, then enter Edit Mode and Insert
O   insert line above current line, then enter Edit Mode and Insert

vi Command Mode

Command Mode is enabled by typing colon (":") or ("/") and some other possible keys when in Movement Mode.

Cmds   Function
:q!   quit vi discarting changes made
:wq   write file and then quit vi
/string   search next occurence of "string".
:g/search/s//replace/g   search and replace all occurences of "search" with "replace".

vi Edit Mode

What you can do in Edit Mode heavily depends on the terminal emulation you use. I urgently recommend to use DEC VT220 or better. DEC VT100 or ANSI just isn't good enough, really. With DEC VT100 you won't have PageUp/PageDn/Home/End and Ins/Del keys ready as you have with DEC VT220 or better. Unfortunately terminal emulation coming with Microsoft Windows 95/98/NT can emulate DEC VT100, only.

With a proper terminal emulation you will be free to move your cursor using cursor keys. If you have a DEC VT220 or better you can also switch between Replace and Insert by hitting the Ins key on your keyboard without exiting Edit Mode. To exit Edit Mode back to Movement Mode, press the Esc key. Some terminal emulations will not let you do this by simply pressing Esc. Consult your terminal emulation users guide if Esc does not work. Esc may be moved to a different key.

In Edit Mode just type text like in any other text editor. You can remove what you just typed pressing BS. On some terminal emulations you can also delete characters by pressing Del on your keyboard and your cursor can be moved page wise etc. Using DEC VT100 you will have to exit Edit Mode, use x for delete and reenter Edit Mode by pressing i again. Not very comfortable. Use DEC VT220 or better if you can.

vi Literature

There normally is some online help: man vi. There is also a number of usable books such as this one: