vi Beginner Help

VI Quick Start

VI is a text editor (vi is most likely short for "visual" editor as screen editors were called many years ago though some claim it is the roman numerals for 6 as it was originally developed on Version 6 Unix. It doesn't really matter, does it?) Starting VI: world% vi file.name # or just vi Moving around: hjkl the four keys h j k l are left, down, up and right. ^D (control-D) go down half a screen ^U up half a screen ^ go to beginning of line $ go to end of line Inserting text: i go into insert mode, then just type, hitting ESCape ends insert a append, same as insert but starts after the cursor rather than before. AN IMPORTANT POINT TO REMEMBER IN VI IS IF YOUR "COMMANDS" ARE BEING INSERTED INTO THE FILE INSTEAD OF BEING EXECUTED HIT THE ESCAPE KEY TO END INSERT MODE. This one event drives new (and some old) vi users nuts until their fingers learn to do this without thinking. Inserting lines: o open new blank line "above" cursor. O ...or "below" cursor. Deleting: x delete the char under the cursor dd delete this whole line Searching: /string/ search for string (down) ?string? search for string (up) There are a bunch of commands that are entered after typing : which moves the cursor to the lower left of the screen and waits for you on the bottom line (hitting return on an empty line cancels a : prompt). Quitting vi: q quit if there's nothing to save :wq write changes and quit :w just write (save), don't quit :w file.name specify file name to save to and write :q! quit instantly, throw away changes Also: ZZ (no : ) writes and exits. Some other commands: :!unix command run any unix command, including a new shell. Return to vi from a shell by typing exit to the shell. :n,m command perform command on lines n thru m for example: :1,10d delete lines 1-10 :1,$s/foo/goo/ on all lines ($ means last line), substitute the *first* occurance of foo with goo. :1,$s/foo/goo/g same but *all* occurances of foo with goo :4 go to line 4 :$ go to last line :$= tell me the line number of last line but don't go there. :.= tell me the number of this line I'm on :,.$s/foo/goo/ substitute foo with goo on lines from here to the bottom (that is, dot means this line, $ means last line.) Finally, there's pattern directed commands: :/foo/d find the next line with a "foo" in it and delete it. :g/foo/d find all lines with a foo in it and delete them. :u undo the last thing I screwed up (good to know with a command like g/foo/d) :v/foo/d delete every line WITHOUT a foo (v is opposite sense of g). :g/foo/s/hello/goodbye/ on every line with a foo in it substitute hello with goodbye (if it's there.) Finally, the answer to a deep dark mystery: The search patterns between // and ?? are known as REGULAR EXPRESSIONS. They can contain wild cards like /x.*z/ matches any line with an x followed by anything and then a z (for example, xyz, xyyyz, xabcdez, and so on.) This is very powerful. The full explanation is on the grep manual page (world% man grep), grep is a command that searches text files using the same wildcard patterns as in: world% grep 'x.*y' file [file2 file3 ...] grep prints out the lines matched. The equivalent command in vi would be: :g/r.e./p That is, globally search for regular expression and print it (to the screen.) So now you know where grep got its name (actually, from ed which was an earlier line editor, but same idea, same command without the : .)
HOME Top of Help Desk Eye On The World  The World Kiosk

             For further assistance, please send an email 
                       request to support@world.std.com

        Copyright 1995 Software Tool & Die, Inc.  All Rights Reserved.

   Software Tool & Die, Inc., 1330 Beacon St. Suite 215 Brookline, MA 02146