From: wes@terminator.cc.umich.edu (Wesley Craig)
Newsgroups: alt.sources,comp.editors,umich.unix
Subject: vilearn -- an interactive tutorial for vi in 5 movements
Message-Id: <1992Aug23.213500.24070@terminator.cc.umich.edu>
Date: 23 Aug 92 21:35:00 GMT
Organization: University of Michigan
Lines: 1710
Xref: news.duke.edu alt.sources:1771 comp.editors:1848
Status: RO
vilearn is a textual tutorial to be viewed by with vi, similar to the
GNU Emacs tutorials. This is a beta release. It is not quite complete,
but we've been sitting on it for quite some time and wanted to get it
out.
wes
#! /bin/sh
# This is a shell archive. Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file". To overwrite existing
# files, type "sh file -c". You can also feed this as standard input via
# unshar, or by typing "sh 'README' <'1basics' <>> mark exercises you should try. When you
Xwant to exit this tutorial type ZZ (type capital Z, twice).
X
XWhen you type commands in vi they do not appear on the screen. If the
Xletters you type unexpectedly appear on the screen, press the ESCAPE key.
X
X
XBASIC CURSOR MOVEMENT
X---------------------
XTo move through the tutorial use ^D (control d) and ^U (control u).
X
X ^D Move DOWN one half-screen
X (depress the control key and type d)
X
X ^U Move UP one half-screen
X (depress the control key and type u)
X
X>>> Now type ^D (control d) and ^U (control u) to move down and back up.
X
XWhen you are done reading a screen, you are expected to type ^D to move
Xdown to the next screen. You must remember to type ^D throughout the
Xtutorial.
X
XTo move the cursor line by line, or character by character, use the
Xfour keys h, j, k, and l.
X
X h Move left one character
X j Move down one line
X k Move up one line
X l Move right one character
X
XYou will notice that these keys are in a straight line on the
Xkeyboard. Study the diagram below showing the function of h, j, k, l.
X
X UP
X ....... ....... ....... .......
X : : : : : : : :
X LEFT : h : : j : : k : : l : RIGHT
X :.....: :.....: :.....: :.....:
X
X DOWN
X
X>>> Now type j or k a few times to bring the cursor to this line.
X
X>>> Try moving off the right end of a line using l . Notice that
X>>> vi will not allow you to move off the end of the line using l .
X>>> Likewise, you cannot use h and l on a blank line.
X
X>>> Try moving past the bottom of the screen using j . Notice how
X>>> how the screen scrolls downward.
X
X>>> Now practice using k to move up, and h to move left.
X
X
XDELETION
X--------
XTo delete characters and lines, use x and dd.
X
X x X-OUT one character
X dd DELETE one line
X
XTo undo your changes, use u.
X
X u UNDO last change only
X
X>>> Delete this SCRAP line. Move to this line with j or k , now type dd .
X>>> Try undoing the deletion with u .
X
X>>> Move to this line and x-out the Y's with x : "whY ask whY?"
X
X>>> Try undoing the deletion with u . Try typing u several times.
X>>> Notice that u only undoes the last change.
X
XHere are more lines on which to practice deleting and undoing (use: dd x u )
X
X Emacs is an evil creation. Emacs is an evil creation.
X Emacs is an evil creation. Emacs is an evil creation.
X Emacs is an evil creation. Emacs is an evil creation.
X
X
XQUIT COMMANDS
X-------------
X(DO NOT QUIT the tutorial at this time.)
X
XTo quit a file without saving any changes you have made (for instance,
Xwith the dd or x commands) use :q! . To quit and save your
Xchanges, use ZZ . When you are editing your own files, you normally
Xuse ZZ to quit.
X
X :q! QUIT without saving changes
X (type a colon, then the letter q, then an
X exclamation point, and press RETURN)
X
X ZZ Exit and save any changes
X (type capital Z, twice)
X
X
XINSERTION
X---------
XYou enter insert mode with i or o . Anything you type during insert
Xmode appears on the screen. When you are done inserting, press ESC
Xto exit insert mode. Type ^[ (control [ ), if you do not have an ESC key.
X
X o OPEN a line for inserting text
X i INSERT starting at the cursor
X
X ESC ESCAPE from insert mode
X
XDuring insert mode, use your erase character (usually backspace or
Xdelete) to delete mistakes. The characters you delete will remain on
Xthe screen until you press ESC.
X
X>>> Insert your name and phone number below the next blank line. To do this:
X>>> Open a line below using o .
X>>> Type your first and last name. Press RETURN.
X>>> Then type your phone number and press ESC.
X>>> Use x to erase part of your phone number.
X
X>>> Type the date below your phone number. To do this:
X>>> Open another line using o .
X>>> Type the date and press ESC.
X
X>>> Type u to undo the insertion.
X
X>>> Insert a nickname between your first and last names, using i. To do this:
X>>> Move the cursor to the spot between your names using h, j, k, l.
X>>> Press i .
X>>> Type the nickname, use DELETE or BACKSPACE to erase any typos.
X>>> Then press ESC.
X
XOn some computers, a line may be longer than the width of the screen.
XThis means that a very long line may appear to be two lines on the
Xscreen. This happens when you keep typing without pressing RETURN at
Xthe edge of the screen. To avoid any confusion when you're inserting
Xtext, be sure to press RETURN before reaching the right edge of the
Xscreen.
X
X
XFILE BASICS
X-----------
XTo edit a file using vi, at the UNIX command line, type:
X
X vi filename
X
Xwhere "filename" is the name of the file you want to edit. When you
Xuse vi to edit a file that does not exist, the file is not immediately
Xcreated. The file is created the first time you save it, until then it
Xonly exists in temporary space. If you make a mistake typing the name
Xof a file, just use :q! to abandon the new file.
X
X
XSUMMARY
X-------
XThese are the vi commands you should know after tutorial #1:
X
X ^D Move DOWN one half-screen
X (depress the control key and type d)
X
X ^U Move UP one half-screen
X (depress the control key and type u)
X
X h Move left one character
X j Move down one line
X k Move up one line
X l Move right one character
X
X dd DELETE one line
X x X-OUT one character
X
X u UNDO last change
X
X :q! QUIT without saving changes
X (type a colon, then the letter q, then an
X exclamation point, and press RETURN)
X
X ZZ Exit and save any changes
X (type capital Z, twice)
X
X o OPEN a line for inserting text
X i INSERT starting at the cursor
X
X ESC ESCAPE from insert mode
X
Xand to use vi from a UNIX command line:
X
X vi filename
X
X
XYou are now prepared to do simple editing on your own files. Practice
Xusing vi for a few days. Then take the second vi tutorial to learn
Xmore powerful and useful vi commands.
X
XCopyright (c) 1992 Jill Kliger and Wesley Craig. All Rights Reserved.
END_OF_FILE
if test 5941 -ne `wc -c '2moving' <>> mark
Xexercises you should try. When you want to exit this tutorial type ZZ .
X
X
XWORDS
X-----
XThere are many ways to move from one word to another. Consider these:
X
X w Move to the beginning of the next WORD
X e Move to the END of the next word
X b Move BACK to the beginning to the previous word
X
XFor w, e, and b, a word is delimited by any non-alphanumeric
Xcharacter. The capitalized versions, W, E, and B, also move from word
Xto word. The difference is that for W, E, and B, a word is delimited
Xby any blank space.
X
X>>> Try out w, b, e, on the lines provided below.
X>>> Next practice using B, W, b, E on the lines provided below.
X
X EX-PER-IMENT on these lines;test moving back &forth.
X EX-PER-IMENT on these lines;test moving back &forth.
X
X
XON THE LINE
X-----------
XYou can move immediately to any point on the current line.
X
X $ Move to the end of the line
X ^ Move to the first non-white character on the line
X
X 0 Move to the first column on the line (column zero)
X #| Move to an exact column on the line (column #) e.g. 5| 12|
X
X>>> Experiment with $ and ^ on the line provided below. Notice
X>>> that ^ moves to the first non-white character, not the beginning.
X
X This is a PRACTICE LINE. There is white space at the front. END
X
X0 (zero) will always take you to the far left edge of the screen.
X
X#| (number vertical-bar) is for moving to an explicit column on a line.
XJust type any number 1-80 and press | . For example: 5| 20| 30|
XNote that you can't move beyond the last column on a line.
X
X
XFINDING CHARACTERS
X------------------
XOften you want to move to a specific letter or character on a line.
X
X f char FIND the next occurrence of char on the line
X t char Move 'TIL the next occurrence of char on the line
X
X F char FIND the previous occurrence of char on the line
X T char Move 'TIL the previous occurrence of char on the line
X
X ; Repeat the last f, t, F, or T
X , Reverse the last f, t, F, or T
X
Xf and F land on the character. t and T land next to the character.
Xf and t move forward, while F and T move backward.
X
XIf the specified character is not on the line, vi will beep.
X
X>>> Move to the beginning of the line below, and try out these commands:
X>>> fe fE ; ; , , t@ TP te tE , ; , ;
X
X "PRACTICE line?" "Each and Every?" "Find thE char@cter and move to it.END
X
X
XMATCHING
X--------
Xvi has a handy way to determine if (), {}, and [] pairs match up.
X
X % Show matching () or {} or []
X
X>>> On the practice lines below, move your cursor over a (,),{,},[, or ].
X>>> Then type % .
X
X [TRY THIS. ((Whether) the pairs match up is the question.) [One]
X pair is incomplete]. Can you tell {which one? ]} END
X
X
XWINDOW POSITIONS
X----------------
XYou can move the cursor to the top, middle, or bottom of the vi window.
X
X H Move to the HIGHEST position in the window
X M Move to the MIDDLE position in the window
X L Move to the LOWEST position in the window
X
X>>> Try out these commands: type H then M and L and then M again.
X
X
XMARKING LOCATIONS
X-----------------
XYou can mark positions in the file and return to them.
X
X m char MARK this location and name it char
X ' char (quote character) return to line named char
X '' (quote quote) return from last movement
X
Xchar can be any lower case letter, a-z. A mark persists until you:
X 1) use the same char to mark another location
X or 2) delete the marked line
X
X>>> Move to this line and type ma to mark it a
X>>> Move to this line and type mb to mark it b
X>>> Move to this line and type mz to mark it z
X>>> Type 'a to return to line a
X>>> Type 'b to return to line b
X>>> Type 'z to return to line z
X
XCertain commands can move you large distances. These commands cause
Xyour last position to be remembered in the special mark named ' (quote).
XTo move to this special mark, just type '' (quote quote).
X
X>>> Try this: 'b to return to line b, and then '' to return here.
X
X
XGO TO A LINE
X------------
X
X G GO to the last line in the file
X #G GO to line #. (e.g., 3G , 5G , 124G )
X
XRead these directions carefully:
X>>> Type 1G to go to the top of the file, and then '' (quote quote)
X>>> to return here.
X>>> Now try G to go to the end of the file, and then '' to return here.
X
X
XBLOCKS OF TEXT
X--------------
XIt is often convenient to move through files jumping from one block of
Xtext to the next. To do this use braces and parentheses:
X
X { (left brace) Move to the beginning of a paragraph
X } (right brace) Move to the end of a paragraph
X
X ( (left paren) Move to the beginning of a sentence
X ) (right paren) Move to the beginning of the next sentence
X
X>>> Experiment with } and { on the two paragraphs provided below.
X>>> Note that paragraphs are separated by a blank line.
X
X EXPERIMENT on this first paragraph. The quick brown fox jumped
X over the seven lazy dogs. The fox must have been very large to
X jump over seven dogs!
X
X EXPERIMENT on this second paragraph. The quick brown dog
X jumped over the seven lazy foxes. The dog didn't have to be nearly
X as large, since foxes aren't too big.
X
X>>> Try out ) and ( on the two paragraphs provided above.
X>>> Notice that sentences are separated by two blank spaces.
X
XC programmers find it useful to move by sections, since sections may be
Xdelimited by a left brace in the first column. By placing the opening
Xbrace of a C subroutine in the first column, you can move to the top of
Xthe next subroutine, using [[ and ]] .
X
X [[ Move to the beginning of a section
X ]] Move to the end of a section
X
XNote that if vi does not find a left brace at the far left, it will
Xmove to the top or bottom of the file.
X
X>>> Now try ]] then ]] and [[ on the subroutines provided below:
X
Xmain()
X{
X helloworld();
X}
X
Xhelloworld()
X{
X printf( "Hello world\n" );
X}
X
X
XSEARCHING
X---------
XThis enables you to jump to the next occurrence of a string in a file.
XTo initially find the string use:
X
X /string Find string looking forward
X ?string Find string looking backward
X
XTo find additional occurrences of the string type:
X
X n Repeat last / or ? command
X N Reverse last / or ? command
X
Xvi may search past the bottom of the file and then start again at the top.
X(Or, vi may search past the top and then start again at the bottom.)
X
X>>> You are going to search for a string, find the next three
X>>> occurrences. Then flip directions and find the string until you
X>>> return to this location. To do this:
X>>> Type /the then press RETURN.
X>>> Type n three times.
X>>> Type N until you return to this location.
X
X
XSUMMARY
X-------
X
X w Move to the beginning of the next WORD
X e Move to the END of the next word
X b Move BACK to the beginning to the previous word
X
X $ Move to the end of the line
X ^ Move to the first non-white character on the line
X
X 0 Move to the first column on the line (column zero)
X #| Move to an exact column on the line (column #) e.g. 5| 12|
X
X f char FIND the next occurrence of char on the line
X t char Move 'TIL the next occurrence of char on the line
X
X F char FIND the previous occurrence of char on the line
X T char Move 'TIL the previous occurrence of char on the line
X
X ; Repeat the last f, t, F, or T
X , Reverse the last f, t, F, or T
X
X % Show matching () or {} or []
X
X H Move to the HIGHEST position in the window
X M Move to the MIDDLE position in the window
X L Move to the LOWEST position in the window
X
X m char MARK this location and name it char
X ' char (quote character) return to line named char
X '' (quote quote) return from last movement
X
X G GO to the last line in the file
X #G GO to line #. (e.g., 3G , 5G , 175G )
X
X { (left brace) Move to the beginning of a paragraph
X } (right brace) Move to the end of a paragraph
X
X ( (left paren) Move to the beginning of a sentence
X ) (right paren) Move to the beginning of the next sentence
X
X [[ Move to the beginning of a section
X ]] Move to the end of a section
X
X /string Find string looking forward
X ?string Find string looking backward
X
X n Repeat last / or ? command
X N Reverse last / or ? command
X
XYou should now be able to move around files very efficiently. These
Xcommands are especially useful if you are using vi over a slow modem.
XPractice the material in this lesson for a few days and then take
Xeither the third vi tutorial to learn how to copy, cut, and paste, or
Xthe forth vi tutorial to learn additional insertion techniques.
X
XCopyright (c) 1992 Jill Kliger and Wesley Craig. All Rights Reserved.
END_OF_FILE
if test 9001 -ne `wc -c '3cutpaste' <>> mark exercises you should try.
X
XWhen you want to exit this tutorial type ZZ to exit and save your
Xchanges. Or type :q! to exit without saving changes.
XRemember that typing u will UNDO your last change.
X
X
XCUTTING TEXT
X------------
XThe delete command can be combined with any of the movement commands
Xtaught throughout tutorial #2. The resulting command is of the form:
X
X dmovement DELETE to where the movement command specifies
X
XConsider the following examples:
X
X dw DELETE to the beginning of the next WORD
X d$ DELETE to the end of the line
X d) DELETE to the beginning of the next sentence
X dte DELETE 'TIL the next e
X dd DELETE a line (dd is a special case of the d command)
X
X>>> Experiment with dw d$ d) dte dd on the paragraph provided below:
X
X PRACTICE here. Now is the time for all good users to learn the
X editor. The quick brown fox jumped over the seven lazy fish. Now
X is the time for all good users to learn the editor. The quick
X brown computer jumped over the seven lazy users. END PRACTICE
X
X
XPASTING TEXT
X------------
XWhen text is deleted it is put into a buffer which contains the most
Xrecently deleted text. To paste the contents of this buffer elsewhere
Xin the file use the p or P command.
X
X P (upper p) PUT the contents of the buffer before the cursor
X p (lower p) PUT the contents of the buffer after the cursor
X
X>>> Try this sequence of commands on the practice lines below:
X>>> dd to delete one line
X>>> j to move down a line
X>>> p (lower p) to PUT the deleted text after the cursor
X>>> } to move to the end of the paragraph
X>>> P (upper p) to PUT the deleted text before the cursor
X
X PRACTICE line. Cut and Paste this line to the bottom of the
X paragraph. Here is some filler, feel free to cut and paste the
X text in this practice region. Remember that u undoes the last
X action. END OF PRACTICE
X
X>>> Try this sequence of commands at the beginning of a word:
X>>> dw w P
X
XThe fastest way to swap two letters is to type: xp
X
X>>> Use xp to correct the misspelled words below:
X
X PRACTICE. Thier weird quiet recieved an inconvenient shriek.
X Thier belief is that to recieve grief from nieghbors outwieghs
X all else. Biege skies lead to wierd science. END.
X
X
XNUMBERING
X---------
XConsider cutting and pasting 3 words. Based on previous exercises you
Xwould type dw , move to the new location, and type p , and repeat
Xthis procedure twice more. There is an easier way to do this:
X
X>>> Using the practice lines below, try the following sequence of commands:
X>>> Move to the beginning of the first sentence.
X>>> Type d3w to DELETE 3 WORDS.
X>>> Type w to move ahead one WORD.
X>>> Type P (upper p) to PUT the three words before the cursor.
X
X PRACTICE Numbering vi commands is easy to do. Now is the time for
X all good users to learn the editor. The quick brown fox jumped
X over the seven lazy dogs. Numbering vi commands is easy to do.
X Now is the time for all good users to learn the editor. END PRACTICE
X
X>>> Type d2d to DELETE 2 lines, using the practice paragraph above.
X>>> Move to the top of the paragraph.
X>>> Type p (lower p) to PUT the two lines after of the cursor.
X
XNumbering also works for movement commands.
X
X>>> Now try 4w to move ahead 4 WORDs, on the lines provided above.
X>>> Then use 3b to move BACK 3 words.
X
XWhen you type 4w THINK "4 words", when you type d4w think "delete 4
Xwords". In general, we can write
X
X #movement repeat movement # times
X d#movement DELETE to where the #movement command specifies
X
X
XCOPYING TEXT
X------------
XThe YANK command works just like the DELETE command, except y is used
Xinstead of d .
X
X ymovement YANK to where the movement command specifies
X
XYANK and DELETE are identical except that YANK only copies the specified
Xtext into the buffer.
X
X>>> Try this sequence of commands on the practice lines below:
X>>> yy to YANK a line (yy is a special case of the y command)
X>>> 3j to move down 3 lines
X>>> p (lower p) to PUT the yanked text after the cursor
X
X PRACTICE line. Copy and Paste this line to the bottom of the
X paragraph. Here is some filler, feel free to copy and paste the
X text in this practice region. Remember that u undoes the last
X action. END OF PRACTICE
X
XPlease note that copy, cutting, and pasting large blocks of text may
Xsignificantly alter the tutorial file. Remember that you can always get
Xa new copy of the tutorial file and that u UNDOes your last change.
X
XHere are some examples which show the similarity between y and d .
X
X yw YANK to the beginning of the next WORD
X y$ YANK to the end of the line
X y) YANK to the beginning of the next sentence
X yte YANK 'TIL the next e
X yy YANK a line
X
XHere are some more examples using commands from tutorial #2.
X
X yL YANK from here to the lowest point of the window
X y/and YANK from here to the word "and"
X y2} YANK 2 paragraphs
X y'a YANK from here to the marked line "a" (mark line first)
X
X>>> Experiment with yw yte y4w y2} y3y and y$ on the paragraph
X>>> provided below. Copy text AND use p or P to paste it.
X
X PRACTICE line. Copy and Paste this line to the bottom of the
X paragraph. Here is some filler, feel free to copy and paste
X the text in this practice region. Remember that u undoes the
X last action. END OF PRACTICE
X
X
XNUMBERED BUFFERS
X----------------
XIn all of the previous pasting exercises you've used the "un-named"
Xbuffer. The un-named buffer contains the text you most recently cut or
Xcopied. When you make a new cut or copy, the old contents of the
Xun-named buffer are moved to one of the "numbered" buffers. The
Xbuffers are numbered 1-9. Each time you cut or copy text,
X
X vi saves your current cut or copy in a buffer #1
X vi saves your 2nd to last cut or copy in a buffer #2
X The cut or copy before that is saved in a buffer #3 ...
X vi saves your 8th oldest cut or copy in a buffer #8
X vi saves your 9th oldest cut or copy in a buffer #9
X
XNote that buffer #1 is the same as the un-named buffer. Here's how to
Xpaste from the numbered buffers:
X
X "#P (upper p) PUT contents of buffer # before the cursor
X "#p (lower p) PUT contents of buffer # after the cursor
X
XFor example:
X
X "1p PUT buffer 1 after the cursor
X "7p PUT buffer 7 after the cursor
X
X>>> Delete this 1st line with dd
X>>> Delete this 2nd line with dd
X>>> Delete this 3rd block with d2d
X>>> (2nd half of block 3)
X>>> Delete this 4th block with dd
X>>> Now type "1p "2p "3p "4p
X
XIf you are using vi and have made accidental deletions, just PUT the
Xcontents of each numbered buffer to recover the deleted text.
X
X
XNAMED BUFFERS
X-------------
Xvi maintains the un-named and numbered buffers automatically. You can
Xmaintain your own buffers named a-z. That is, you can cut or copy text
Xinto buffer x and later paste the text from buffer x.
X
X "aDELETE DELETE text into buffer a
X "aYANK YANK text into buffer a
X "aPUT PUT text from buffer a
X
XNote, don't actually type 'DELETE', 'YANK', or 'PUT'; type one of the
XDELETE commands, YANK commands, or PUT commands. See the examples below:
X
X "ad} DELETE paragraph into buffer a
X "by3y YANK 3 lines into buffer b
X "cy200G YANK to line 200 into buffer c
X "dp PUT buffer d after the cursor
X "zP PUT buffer z before the cursor
X
XThe contents of a named buffer are lost if:
X 1) you store new text in a buffer with the same name
X or 2) you quit vi (using ZZ or :q! )
X
X>>> Delete this START line into buffer a by typing "add
X>>> Paste buffer a by typing "ap
X
X>>> Delete this INTERMEDIATE line into buffer b by typing "bdd
X>>> Paste buffer b by typing "bp
X
XTo put new material into buffer a
X>>> Delete this FINAL line into buffer a by typing "add
X>>> Paste buffer a by typing "ap
X
X
XSAVING WITHOUT QUITTING
X-----------------------
XWith ZZ you save changes and quit vi. With :w you can save and not
Xquit vi. It is a safe practice to save changes to a file regularly.
XThis reduces re-typing in the event your computer crashes.
X
X :w WRITE contents of the file (without quitting)
X (type a colon, type w , then press the RETURN key)
X
X>>> Try :w now. Note the message at the bottom of the screen.
X
X
XPASTING BETWEEN FILES
X---------------------
XThis is an extremely useful procedure in vi. Only one new command is
Xrequired for pasting between files, the EDIT command
X
X :e filename Begin EDITing the file called "filename"
X
XThe EDIT command allows you to edit another file without quitting vi.
XThis is useful since named buffers are lost when you quit vi.
X
XLet's say you want to copy 6 lines from the file called "3temp" into
Xthis file which is named "3cutpaste":
X(Note that "3temp" has already been created for you)
X
X 1) WRITE "3cutpaste". vi will not allow :w (press RETURN)
X you to edit another file without first
X saving any changes you've made.
X
X 2) EDIT "3temp" without quitting vi. :e 3temp (press RETURN)
X
X 3) YANK 6 lines from "3temp". "ay6y
X
X 4) Return to "3cutpaste". :e 3cutpaste (press RETURN)
X
X 5) PUT from buffer a "ap
X
XNote that the un-named and numbered buffers are lost when the EDIT
Xcommand is used. Only named buffers are preserved with EDIT.
X
X>>> Follow the 5-step procedure outlined above. Don't be concerned
X>>> with remembering all 5 steps, the instructions are repeated in
X>>> "3temp". Paste the text from "3temp" near this line of this file,
X>>> "3cutpaste".
X
XYou can use this 5-step procedure on any two files, with any cutting or
Xcopying action (here, y6y is the example).
X
X
XSUMMARY
X-------
X
X #movement repeat movement # times
X
X dmovement DELETE to where "movement" command specifies
X d#movement DELETE to where the #movement command specifies
X (e.g. dw d3w )
X
X ymovement YANK to where "movement" command specifies
X y#movement YANK to where the #movement command specifies
X (e.g. yw y3w )
X
X P (upper p) PUT the contents of the buffer before the cursor
X p (lower p) PUT the contents of the buffer after the cursor
X
X "#P (upper p) PUT contents of buffer # before the cursor
X "#p (lower p) PUT contents of buffer # after the cursor
X (e.g. "2p "7P )
X
X "aDELETE DELETE text into buffer a
X "aYANK YANK text into buffer a
X "aPUT PUT text from named buffer a
X (Note, don't actually type 'DELETE', 'YANK', or 'PUT';
X type one of the DELETE commands, YANK commands, or PUT
X commands, e.g. "ad} "by3y "cy200G "dp "zP )
X
X :w WRITE contents of the file (without quitting)
X (type a colon, type w , then press the RETURN key)
X
X :e filename Begin EDITing the file called "filename"
X
X
XYou are now prepared to handle all cutting, copying and pasting tasks
Xwhich may arise. If you practice what you've learned you'll find editing
Xin vi to be fast and convenient.
X
XCopyright (c) 1992 Jill Kliger and Wesley Craig. All Rights Reserved.
END_OF_FILE
if test 11082 -ne `wc -c '3temp' <'4inserting' <>> mark exercises you should try. When you want to exit this
Xtutorial type ZZ .
X
X
XSIMPLE INSERTION
X----------------
XYou spend most of your time in vi inserting text. As you might expect,
Xthere are several commands to begin insertion.
X
X o OPEN a line below the cursor
X O OPEN a line above the cursor
X
X i INSERT starting before the cursor
X I INSERT at the beginning of the line
X
X a APPEND starting after the cursor
X A APPEND at the end of the line
X
XRemember to type ESC to leave insert mode. If you don't have an ESC key
Xtype ^[ (control [ ).
X
X ESC ESCAPE from insert mode
X
X>>> Move the cursor to this line. Type O , enter your name. Press ESC.
X>>> Next type o , enter the date. Press ESC.
X
XNote that O opens the line above and puts you in insert mode,
Xwhile o opens the line below and also puts you in insert mode.
X
X>>> Type a on any line above, enter your name. Press ESC. Do the
X>>> same for A.
X
X>>> Read the following. Your goal is to take the sentence fragment below:
X
X BROWN FOX OVER THE SEVEN LAZY
X
X>>> and convert it to
X
X THE QUICK BROWN FOX JUMPED OVER THE SEVEN LAZY DOGS.
X
X>>> To do this type:
X>>> I to insert THE QUICK (then press ESC)
X>>> move the cursor to after the X in FOX
X>>> a to insert JUMPED (then press ESC)
X>>> A to insert DOGS. (then press ESC)
X>>> Now move to the sentence fragment and make the changes outlined above.
X
X
XJOINING LINES
X-------------
XOften it is convenient to join two short lines into one line. There
Xare several ways to do this. The easiest is the J command. Other
Xmethods will be explored in tutorial #5.
X
X J JOIN two lines
X
X>>> Go to the first line in the block below. Type J. Type J again.
X
X Example: NOW IS THE TIME
X the walrus said
X TO THINK OF MANY THINGS
X
XIn the event that joining lines creates a line which exceeds the width
Xof the screen, you can break the line by typing i and pressing RETURN.
X
X
XSUBSTITUTING TEXT
X-----------------
XSubstituting combines the delete command and the insert command into a
Xsingle step.
X
X #s SUBSTITUTE for # characters
X #S SUBSTITUTE for # whole lines
X
X
XIn order to substitute text you have to know how much text you want to
Xdelete. Consider the following examples:
X
X 3s SUBSTITUTE the next 3 characters for what will be typed
X 7s SUBSTITUTE the next 7 characters for what will be typed
X
X>>> Change the SAMPLE DEFINITION below. To do this:
X>>> move the cursor to the T in TWO
X>>> type 3s
X>>> type FOUR then press ESC
X
X SAMPLE DEFINITION: A string quartet is defined to be
X a group of TWO musicians.
X
X
XREPLACING TEXT
X--------------
XThe r and R commands allow you to directly type over existing text.
X
X r REPLACE character (NO need to press ESC)
X R enter over-type mode
X
X>>> Correct each of the TYPOs on the sample line below. To do this:
X>>> move the cursor to the misspelled character
X>>> type r
X>>> type the correct character
X
X SAMPLE: maintanence conveniance complience applience dilagent
X
X>>> Use the over-type command, R , on the sample line above.
X>>> Type R then type the name of a local restaurant. Press ESC.
X
X
XCHANGING TEXT
X-------------
XThe change command combines insertion, deletion, and the movement
Xcommands. (Recall that the movement commands were taught in tutorial
X#2.) Change is probably more useful than replace or substitute. The
Xgeneral form of the change command is:
X
X cmovement CHANGE to where the movement command specifies
X
XConsider the following examples:
X
X cw CHANGE to the beginning of the next WORD
X c$ CHANGE to the end of the line
X c) CHANGE to the beginning of the next sentence
X cte CHANGE 'TIL the next e
X c3w CHANGE the next 3 WORDS
X cc CHANGE a line (cc is a special case of the c command)
X c} CHANGE to the end of the paragraph
X
X>>> Follow these steps:
X>>> 1. move to the desired location in the practice paragraph below
X>>> 2. type cw (change to the beginning of the next WORD)
X>>> 3. type your name
X>>> 4. press ESC
X
X PRACTICE here. Now is the time for all good users to learn the
X editor. The quick red fox jumped over the seven lazy fish. Now
X is the time for all good users to learn the editor. The quick
X brown computer jumped over the seven lazy users. END PRACTICE
X
X>>> Experiment by using a variety of options for step #2. Try
X>>> out c$ c) cte c3w cc c} on the practice paragraph above.
X
XNote that the change command follows the same pattern as the delete
Xand yank commands which were explored in tutorial #3.
X
X
XSUMMARY
X-------
X
X o OPEN a line below the cursor
X O OPEN a line above the cursor
X
X i INSERT starting before the cursor
X I INSERT at the beginning of the line
X
X a APPEND starting after the cursor
X A APPEND at the end of the line
X
X ESC ESCAPE from insert mode
X
X J JOIN two lines
X
X #s SUBSTITUTE for # characters
X #S SUBSTITUTE for # whole lines
X
X r REPLACE character (NO need to press ESC)
X R enter over-type mode
X
X cmovement CHANGE to where the movement commands specifies
X (e.g. c3w c$ cc )
X
X
XThese commands should improve your ability to insert text efficiently.
XThe next tutorials deal with advanced commands and tricks which can
Xfurther speed up your editing.
X
XCopyright (c) 1992 Jill Kliger and Wesley Craig. All Rights Reserved.
END_OF_FILE
if test 5442 -ne `wc -c '5tricks' <>> mark exercises you should
Xtry. When you want to exit this tutorial type ZZ .
X
X
XCASE CONVERSION
X---------------
XWhen you want to change an upper-case character to a lower-case
Xcharacter (or lower-case to upper-case) there is a single command which
Xdoes both:
X
X ~ (tilde) Convert case of current character
X
X>>> Move the cursor to be OVER the first character in the example
X>>> line below. Press ~ until you have changed the case of the
X>>> entire line. ( ~ will advance to the right automatically).
X
X bOB WENT TO pARIS, fRANCE, TO SEE THE #1 CYCLING EVENT. end.
X
XNote that ~ only affects alphabetic characters.
X
X
XUNDOING
X-------
XYou have already learned the u command which UNDOes the last change.
XThe U command will undo more than one change.
X
X U (upper u) UNDO all changes made to the current line
X as long as your cursor has not moved off the line
X
XThere are three rules to learn:
X
X U will not re-do changes you have UNDOne (e.g. by pressing U twice)
X U will not restore a line which has been deleted
X U will not work if you have moved off the line you changed
X
X>>> Move the cursor to the first line of the example text. Delete
X>>> the words "FISH" and "RAT" from the first line. Now type U
X>>> (all changes to that line should be undone). Type U again
X>>> (nothing should happen).
X
X EXAMPLE: The FISH fed the cat. The RAT fed the cat. Example
X text is HARD to make interesting. The man fed the BUG. The
X worm fed the cat. Example text is hard to make interesting. END.
X
X>>> Move the cursor to the second line of the example text. Delete
X>>> the words "HARD" and "BUG" from the second line. Now type j .
X>>> Then type k . Then type U (nothing should happen).
X
X>>> Delete the first line of example text using dd . Then type U
X>>> (nothing should happen).
X
X
XREPEAT LAST COMMAND
X-------------------
XOften you want to make the same change at multiple locations in the
Xfile. To help accomplish this, vi remembers your previous action.
X
X . (dot) repeat last change
X
X>>> Go through the example below changing "FISH" to "TOAD":
X>>> Go to the "F" in the first instance of "FISH"
X>>> To change the word: type cw then type TOAD then press ESC
X>>> Move the cursor to "F" in the second occurence of "FISH"
X>>> Type . (dot)
X>>> Move the cursor to "F" in the final occurence of "FISH"
X>>> Type . (dot)
X>>> Now move the cursor to each occurence of "CROW"; Type . (dot)
X
X EXAMPLE: The FISH fed the cat. The CROW fed the cat. Example
X text is FISH to make interesting. The man fed the CROW. The
X worm fed the FISH. Example text is hard to make CROW. END.
X
X>>> Go through the example above deleting all occurences of "TOAD":
X>>> Move to the beginning of the EXAMPLE paragraph above.
X>>> Type /TOAD and press RETURN (recall tutorial #2)
X>>> Delete the word by typing dw
X>>> Type n to move to the next occurence of "TOAD"
X>>> Type . (dot) to repeat the dw command
X>>> Use n. to delete the remaining "TOAD"s
X
XNote that . only repeats changes, not cursor movements.
X
X
XWINDOW ACTIONS
X--------------
XYou are already familiar with the ^U (depress the control key and
Xtype u) and ^D commands from tutorial #1.
X
X ^D Move DOWN one half-screen
X ^U Move UP one half-screen
X
XThere are several related commands:
X
X ^F Move FORWARD one full-screen
X ^B Move BACKWARD one full-screen
X
X ^E Move the window down one line without moving cursor
X ^Y Move the window up one line without moving cursor
X
XThe ^E and ^Y commands may seem obscure; however, notice that on
Xthe keyboard, e and y are close to d and u respectively. This
Xshould help you remember that ^E moves DOWN, and ^Y moves UP.
X
XRecall the H M L (HIGH MIDDLE LOW) window commands from Tutorial 2.
XConsider a scenario where you want to yank from the current line to a
Xline near the top of the window. You could use ^E and ^Y to
Xposition the text in the window before you use the yH command.
X
XThe z command also moves the window without moving your cursor:
X
X z Position the current line to top of window
X z. Position the current line to middle of window
X z- Position the current line to bottom of window
X
X>>> Move to this line. Type z and press RETURN. Notice that
X>>> this text and the cursor have moved to the top of the window.
X>>> Try z- and z. also.
X
X
XFILE AND DISPLAY CONTROL
X------------------------
XHere are two commands which you may need to use during a vi session but
Xdo not involve your text.
X
X ^G Show status of current file
X ^L Refresh screen
X
XWhen you type ^G (depress the control key and type g) a line appears
Xat the bottom of the window describing the current file. The line
Xcontains:
X
X the name of the file
X the message "[Read only]" if the file is read only
X the message "[Modified]" if there are unsaved changes
X the position of the cursor in the file
X the percentage of the file above the cursor
X
XThe ^L command clears and redraws your window. This is useful when
Xsomething other than vi outputs to your window, e.g. line noise from a
Xmodem, or incoming mail. If there is no "garbage" in your window,
Xpressing ^L may not do anything noticable.
X
X
XSUSPENDING VI
X-------------
XHere is a way to go back and forth between vi and the Unix command
Xline:
X
X ^Z Suspend this job and return to the Unix command line
X fg (foreground) Continue the suspended job
X
XWith this pair of commands you can SUSPEND the vi session, do something
Xfrom the Unix command line, then resume the suspended session by
Xtyping fg at the command line. Note that fg stands for foreground.
X
XRead these instructions CAREFULLY before suspending vi.
X>>> (Imagine you have an urge to type ls at the command line).
X>>> Type ^Z to suspend vi.
X>>> At the command line type ls and then fg to foreground vi.
X
XIf your shell does not support job control, e.g. /bin/sh, then the ^Z
Xcommand will beep instead of suspending vi.
X
X
XBANG COMMAND
X------------
XThe exclamation point, ! (aka BANG), command allows you to feed text
Xto any Unix command. The output of the Unix command replaces the
Xoriginal text. Here is a useful Unix command to use from within vi:
X
X !}fmt Format the paragraph, joining and filling lines to
X produce output lines of up to 72 characters
X
X>>> Move to the example paragraph below. Type !}fmt and press
X>>> RETURN. Notice the paragraph will be reformatted such that
X>>> the lines are of approximately equal length.
X
X EXAMPLE:
X So we grow together,
X Like to a double cherry, seeming parted,
X But yet an union in partition;
X Two lovely berries moulded on one stem;
X So, with two seeming bodies, but one heart;
X END.
X
XAnother useful command is:
X
X !}sort Sort lines of a paragraph alphabetically
X
X>>> Move to the example text below. Type !}sort and press RETURN.
X
X OBERON king of the fairies.
X PUCK or Robin Goodfellow.
X HERMIA daughter to Egeus, in love with Lysander.
X HELENA in love with Demetrius.
X LYSANDER in love with Hermia.
X DEMETRIUS in love with Hermia.
X
XRemember, any Unix command may be used this way.
X
X
XSHIFTING TEXT
X-------------
XIt is possible to shift large blocks of text right and left with the >
Xand < commands.
X
X >movement Shift right to where the movement command specifies
X } Shift right to the end of the paragraph
X > Shift the current line right
X << Shift the current line left
X
X>>> Move the cursor to the first line of the paragraph below.
X>>> Type >> and << to shift the line back and forth. Next
X>>> try >} to shift the paragraph to the right, then >> it left, then type . until all four lines start at the left edge.
X
X THIS IS THE FIRST LINE OF EXAMPLE TEXT
X IS
X EXAMPLE
X TEXT END
X
X
XSUMMARY
X-------
X
X ~ (tilde) Convert case of current character
X
X U (upper u) UNDO all changes made to the current line
X as long as your cursor has not moved off the line
X
X . (dot) repeat last change
X
X ^D Move DOWN one half-screen
X (depress the control key and type d)
X
X ^U Move UP one half-screen
X (depress the control key and type u)
X
X ^F Move FORWARD one full-screen
X ^B Move BACKWARD one full-screen
X
X ^E Move the window down one line without moving cursor
X ^Y Move the window up one line without moving cursor
X
X z Position the current line to top of window
X z. Position the current line to middle of window
X z- Position the current line to bottom of window
X
X ^G Show status of current file
X ^L Refresh screen
X
X !}fmt Format the paragraph, joining and filling lines to
X produce output lines of up to 72 characters
X
X !}sort Sort lines of a paragraph alphabetically
X
X >movement Shift right to where the movement command specifies
X 'Makefile' < vilearn
X install -m 755 vilearn ${BINDIR}/vilearn
X
Xclean :
END_OF_FILE
if test 635 -ne `wc -c 'outline' < shift
X
X*
X* tutorial 6 PROPOSED
X* commands from hell
X*
X
X: colon commands
XQ quit
X^R redraw
X@ execute buffer as macro
X& like :&
X
X^T shift (insert)
X^D unshift (insert)
END_OF_FILE
if test 1160 -ne `wc -c 'vilearn.sh' <