BY BOB ALDEN Using vi - an introduction What is "vi" and is there any reason for me to learn to use it? "vi" is a text editor (the vi stands for visual) that is common to all UNIX operating systems. If you are maintaining web pages on a server that uses UNIX or a UNIX based operating system, you may find it a handy tool as I have. It is one of the family of ex editors, ex, edit, and vi. I discovered vi while looking over my system manager's shoulder many years ago. To use this editor, you must be in a UNIX environment. Either you have decided to use an operating system like Linux on your pc or you have accessed a server that is running UNIX or Linux (or a proprietary version). You may be sitting at the server console or you may be sitting at your pc which is connected to the server via a dialup telephone connection using a modem or via a network (and so on). Let's take the case where you are at home using your pc and have logged on to the server where your web files are stored and you have file saving permission. You invoke a Telnet session. You should see a UNIX prompt. Please see my July 1999 column for more details if you are not familiar with UNIX commands or invoking a Telnet session. GETTING STARTED At the UNIX prompt, you can usually view the on-line manual for the vi editor by typing "man vi" followed by hitting the "enter" key. Depending on the versions of UNIX and vi being used you will likely see the manual for vi (or VIM). VIM stands for Vi IMproved. I will focus on a few of the basic commands common to all vi editor versions. You will realize from scanning the on-line manual that there are many more features and options. Or you can type "vi" and begin to edit a brand new file you are about to create (if you save it properly) or you can type "vi xx" and edit the file "xx" (assuming that "xx" is the name of an existing file in your current directory. Before explaining the various text editing commands, I will cover some basics that are needed in order to perform your editing conveniently and safely. Here are the four sections: 1 How to get in and out of vi 2 Two modes of operation 3 Moving around 4 Editing text 1. HOW TO GET IN AND OUT OF VI --For each of these commands, type the command and hit the "enter" key. TO INVOKE vi xx to invoke the editor from the UNIX prompt where xx is the filename To Read, Save and Exit -- note these commands begin with a colon. :r xx to read file xx into the edit buffer at the current cursor location :w to write (save) the edit buffer to the file as currently named :w yy to write the edit buffer to file yy :wq to write then quit (exit) the editor :q to quit the editor (if no modifications were made since the previous write command - you are expected to save your edit buffer contents first) :q! to quit the editor (this forces the quit, even if your edit buffer has not been saved) 2. TWO MODES OF OPERATION -- these are the command mode and the text input (or insert) mode. If you are in insert mode, you get into command mode by hitting the "Esc" key. If you are in command mode, you get into insert mode by hitting one of several keys, for example; i, a, or R. "i" inserts text before the cursor, "a" inserts text after the cursor, R replaces characters. These commands assume you have the cursor where you want it, so let's next look at moving around on the visible screen and moving the screen up and down (backwards and forwards in the text). 3. MOVING AROUND -- this allows you to find the relevant portion of text if there is more text than fits in the window or screen and to position the cursor within the visible text. Screen Control -- hit the following key combinations where ^F means Ctrl F (depress the Ctrl key while hitting the "F" key) ^F move a full screen forward ^D move a half screen forward ^B move a full screen backward ^U move a half screen backward Cursor Control -- hit the following keys spacebar move one character to the right backspace move one character to the left enter move to start of the next line - move to the start of the previous line 0 move to the start of the current line $ move to the end of the current line h move one character to the left j move one line down (same column) k move one line up (same column) l move one character to the right e move to the end of the word w move one word to the right b move one word to the left (back) 4. EDITING TEXT -- consists of inserting or replacing characters (already discussed) or deleting characters, words, lines and sets of lines. When a delete command is used, the deleted text is placed in a buffer which can subsequently be reinserted. Deleting Text -- hit the following keys (after first moving the cursor to the text you wish to delete) x delete the current character (hold down for several deletions) dw delete the word dd delete the line 3dd delete three lines (starting with current line) Other Editing Commands p place buffer contents after cursor (or below for lines) P place buffer contents before cursor (or above for lines) r* replace the current character with * (* is any character) u undo the last command (undelete) U undo all changes on the current line J join the next line to the current line /x...y searches for the pattern x...y There are many other features that you may wish to use as you develop your experience with vi but this should help to get you started.. FOR MORE INFO. UNIX commands are described in the various UNIX system manuals. You will probably find it easier to purchase a book and keep it handy as you learn and gain experience. Here are two of the many books. Neither is new (in the time scale of computer advances) but then vi has been around for a long while. The UNIX System, by S.R. Bourne (AT&T), 1983 ed, published by Addison-Wesley. The Waite Group's UNIX Primer Plus, 2nd ed 1991, by Michell Waite, Donald Martin, and Stephen Prata, published by SAMS. _________________________________________________________________ Robert T.H. (Bob) Alden is the IEEE Electronic Communications Advisor and a former IEEE vice president. In his other life, he is the past and founding director of the Power Research Laboratory at McMaster University in Hamilton, Ont., Canada. He welcomes your input via e-mail at r.alden@ieee.org. His home page is power.eng.mcmaster.ca/alden. Information about IEEE's information highway is available electronically. To find out what is available, view the Web page at "www.ieee.org/eleccomm" or send a message to "fileserver@ieee.org" and place the file name "info.email" by itself at the start of the first line in the message.