*** HELP FOR vi [UNIX-style] TEXT EDITOR *** Example commands in this tutorial expect that you will use your powers of inference and extrapolation to create and use similar commands to do similar tasks. For instance, the vi command :4,19d deletes lines 4 thru 19 inclusive. What do you think the command :43,96d would do if you entered it? Many vi commands accept a numeric argument to increase their scope of operation. Two Primary Modes of vi-Operation: Command and Insert I. QUICK OVERVIEW: = key. A. "vi file1" will load or create "file1" from the system command line. B. Use: i to go to insert Mode from Command Mode. to return to Command Mode from insert Mode. :w to write text to HD without leaving vi. :x to write text to HD and exit vi. :q! to quit vi with NO WRITE to HD. u to undo last command. II. COMMANDS A. Cursor Movement (^ = key) Arrow keys work as expected IN COMMAND ONLY! key moves to upper left IN COMMAND ONLY! w to move right word by word. 6w to move right 6 "words". 5W to move right 5 words. b to move left word by word. 4b to move left 4 words. 5B to move left 5 full words. L to move to bottom Left of screen. 0 ("zero") to move to left of line. $ to move to right of line. 9G Goto 9th line of file. ^f move down a screen. ^b move back a screen. z puts cursor line at top of screen. 8z puts line 8 at top of screen. 12+ moves cursor forward 12 lines. 7- moves cursor back 7 lines. 1G moves to top of file. $G moves to bottom (end) of file. mx mark your current position with an invisible "x". /string = find first occurence of "string". n = find next occurrence of "string". `x return to position previously marked (with mx). `` return to previous position from anywhere. B. Insert Mode Only is allowed during insert mode. No arrow keys. returns to Command mode from any insert mode. i goes from Command Mode to insert mode. I begin Insert at left-most character in line. a append to right of cursor. A Append at right end of cursor line. O ("oh") Open new line above cursor line and insert. o open new line below cursor line and insert. r Replace a single character with the next one typed. R go into unlimited overstrike mode. c3W overstrike the next 3 words with new text. ^v allows entry of "escape" or "control characters" into text while in any insert mode. III. DELETING TEXT x = delete single character at cursor. 3x = delete cursor character plus two to the right. dw = delete rest of word at cursor. 4dw = delete rest of cursor word plus three more to right. dd = delete entire cursor line. 5dd = delete cursor line and next 4 lines down. :27,43d = delete lines 27 through 43, inclusive. :.,+3d = delete current line and three more. d0 ("zero") = delete from cursor to left end of line. D = delete from cursor to right end of line. . = repeat type (and extent) of last deletion or command. u = undo last deletion or command. U = restore cursor line to unaltered condition. Note: you can not yet have moved to a new line. 3p = paste text deleted 3 deletions ago at cursor. :.,27d = delete from cursor line to line 27, inclusive. :13,.d = delete from line 13 to cursor line, inclusive. IV. HELPFUL THINGS: :set number = turn on line numbering (not made part of file). :set nonumber = turn off line numbering. :set list = display invisible characters. :set nolist = turn off display of invisible characters. :set = review environmental parameters as currently set. :set all = review all vi environmental parameter names. :set showmode = display insert mode indication (system dependent). ^g = display cursor line number and file name. ^l = redraw the display. ^v = allows insertion of "control characters" in text. :![command] = execute a system [command] without leaving vi. :e file2 = run vi on file2 without leaving vi and file1. WARNING: first save file1 (":w "). :e# = return to edit of previous file. J = attach next line to current (cursor) line. u = undo last command. U = return cursor line to original (unedited) status. :r fileA = reads ALL of fileA into current file at cursor. !!ls -l = place the results of the UNIX command in the current file at the cursor line. :ab pc computer = while in insert, vi will automatically expand "pc" into "computer" if "pc" has blank spaces on either end (i.e., is a separate word). :ab = display all current abbreviations. :una pc = cancels substitution of "pc". V. SEARCH-REPLACE /string = find first occurence of "string". n = find next occurrence of "string". :s/him/person = replace first "him" with "person" on cursor line. :5,14s/string/rope/g = replace "string" with "rope" between lines 5 and 14 inclusive, every occurence. :7,29s/cat/dog/gc = replace cat with dog between lines 7 and 29, inclusive, stopping at each occurence for a y/n entry by the User. y makes the replacement, n leaves that occurence unchanged. VI. COPYING TEXT "ayy = "yank" cursor line into memory "a" (a-z avail.). "ap = paste at cursor line the contents of memory "a". "c5yy = yank 5 lines into memory "c". "cp = paste at cursor line the contents of memory "c". :14,23co5 = copy lines 14 thru 23 inclusive AFTER line 5. mk = marks (invisibly) the cursor line in the text. "ay'k = "yanks" into memory "a" from cursor line to the mark. :15,19co. = copy lines 15 thru 19 to below cursor line. VII. MOVING TEXT ^g = display current line number and file name. :2,3m4 = moves lines 2 & 3 to come AFTER line 4. :7m3 = moves line 7 to come AFTER line 3. :45,51m. = moves lines 45 to 51, inclusive, to below cursor line. VIII. MOVING TEXT BETWEEN FILES (Use this sequence only) :w = writes current file (#1) to disk. :e newfile = brings newfile (#2) to the screen. mk = marks (invisibly) the cursor line in the text. Now move cursor to a new line. "ay'k = "yanks" into memory "a" from cursor line to the mark. :e# = brings file #1 to the screen. "ap = pastes text in memory "a" at the cursor position. :w = writes altered current file (#1) to disk. :e# = now rotates between file #2 and file #1. VIX. THE vi editor under MS-DOS The "vi" Text Editor for MS-DOS is identical to the one found on all UNIX-based systems. It is an extremely full-featured and powerful full-screen programming editor capable of editing two documents at once, macros, moving blocks of text, and swapping blocks of text among various documents. vi for MS-DOS by Mortice Kern Systems (phone: 519 884-2251) reads the file C:\EX.RC to do vi start-up things like mapping Function Keys (vi under UNIX always reads $HOME/.exrc). One line which MUST be contained in C:\EX.RC looks like this: set linedelete=0x18 This makes the insertion of the "@" symbol within a text file possible. The symbiote file EX.RC can be configured, for example, to set up your 'PC function keys to do this: shift = exit to DOS, NO SAVE = SAVE and EXIT TO DOS = WRITE, NO EXIT = edit previous document shift = Turn off Line numbering = Turn on Line numbering = Set [an invisible] MARK = "cut" text back to MARK = "cut" one line at the cursor = "paste" cut text below cursor Here is how to set up the Function Keys within the EX.RC file: set linedelete=0x18 map #1 :x map #11 :q! map #2 :w! map #3 :set number map #13 :set nonumber map #4 :e# map #5 mk map #6 "ay'k map #7 "ayy map #8 "ap Place EX.RC into the root directory of your hard disk. When VI.EXE runs, it always looks at the contents of \EX.RC. Create this new subdirectory: C:\TMP You MUST have a \TMP directory into which vi may write its temporary files. Append these lines to your AUTOEXEC.BAT file: set COMSPEC=C:\COMMAND.COM set TMPDIR=C:\TMP set ROOTDIR=C:\ It is hoped that this abbreviated vi tutorial has been useful. If you use both MS-DOS and UNIX/Linux systems, vi is your most efficient choice since it works identically in both environments. The need to learn only one editor (and the elimination of programming errors caused from continual switching between or among editors) is obviously more productive than any other alternative. I personally recommend the Mortice Kern Toolkit of UNIX utilities for anyone using MS-DOS. The Toolkit contains not only vi, but sh, awk, sed, tr and about a hundred other indispensable UNIX tools. Daniel R. Martin Data Base Management Services 14411 Quivira Road Olathe, KS 66062 (913) 897-2263