maart@cs.vu.nl (Maarten Litmaath) (09/27/90)
Included is a document containing Lee Sailer's Vi Tutorial plus the Vi Reference, both converted to LaTeX by Frank Wouters. Special thanks to him and Arie Kaizer <kaizer@prl.philips.nl>. : This is a shar archive. Extract with sh, not csh. : This archive ends with exit, so do not worry about trailing junk. : --------------------------- cut here -------------------------- PATH=/bin:/usr/bin:/usr/ucb echo Extracting 'vi.doc.tex' sed 's/^X//' > 'vi.doc.tex' << '+ END-OF-FILE ''vi.doc.tex' X\documentstyle[11pt]{article} X\addtolength{\textheight}{2cm} X\addtolength{\textwidth}{2cm} X\addtolength{\oddsidemargin}{-1cm} X\setlength{\parindent}{0pt} X\setlength{\parskip}{5pt} X\begin{document} X X% X% LaTex version by Frank Wouters X% X X\thispagestyle{empty} X\vspace*{4cm} X X\begin{flushright} X\Huge\bf XVi Manual\\[2cm] X\LARGE\bf XVi Tutorial\\[0.6cm] X\large\bf XLee Sailer\\[1.2cm] X X\LARGE\bf XVi Reference\\[0.6cm] X\large\bf XVersion 7.0\\[0.6cm] XMaarten Litmaath X\end{flushright} X\normalsize\rm X X\newpage X\section{Tutorial} X X\subsection{Introduction} XVi is a text editor that will be found on nearly all Unix Xmachines you will ever encounter. Even if you find another Xeditor you like better, it is handy to know vi for those Xoccasions when you find yourself using another machine. More Ximportantly, many other applications you will find on Unix Xmachines model their user interface after vi, so good vi skills Xwill transfer to other programs, too. X XTo learn vi (short for "visual"--people in the know say "vee Xeye") you can start by reading this file and trying the things Xthat are suggested. X XGood luck!\\[2ex] XLee Sailer, National Fuel Gas. X X X\subsection{Moving through the file} XYou can use {\it $\hat{}$ U} \footnote{$\hat{}$ U is an abbreviation Xfor $<$ctrl$>$U, pressing both the $<$ctrl$>$ and the $<$U$>$ keys Xat the same time.} and {\it $\hat{}$ D} to move back and forth through Xthe file. XTry it a few times. They move by half screens. {\it $\hat{}$ F} and X{\it $\hat{}$ B} move by full screens. X XYou can go directly to the twentieth line by typing {\it 20G} (that's XG, not g). Any line number will work. No line number defaults Xto the last line in the file. Try {\it G} and {\it 1G}, and then come back Xhere for more. X X\subsection{Modes in vi} XThere are three "modes" of operation in vi. Right now you are in X{\bf command} mode. Everything you type will be interpreted as a Xcommand. Most editors treat the characters you type as text to Xbe inserted, so this is one way that vi is different, and it Xtakes some getting used to. In a minute, we'll see {\bf insert} mode, Xwhich is how you create new text. The third mode is a X{\bf line editor} mode. X X\subsection{Moving around the screen} XAnother thing you need to know is how to move the cursor around Xthe screen. The simplest way is to use the {\it h}, {\it j}, {\it k}, and {\it l} keys. XThey work like you'd expect arrow keys to work. Try typing the {\it j} Xkey a few times now and see what happens. Then try {\it h}, {\it k}, and {\it l}, Xtoo. The {\it j} and {\it k} keys (up and down, right?) try to stay in the Xsame column if they can, but will jump to the left if they Xencounter a short line that is not padded out with blanks. Also, Xif a line is so loooooooooooong that vi has to wrap it around so that you can Xread it (like this one), then it is still only one Xline, so that {\it j} and {\it k} will skip over the second part of it. X XAlso handy are the 0 (zero) which moves to the start of a line, \$ Xwhich moves to the end, and {\it $\hat{} $} (it's over the 6, usually) that Xmoves to the first non blank in a line. X X\footnotesize X\begin{verbatim} X Try typing 0, ^, and $ on this line to see how they work. X\end{verbatim} X\normalsize X XTry the {\it w}, {\it b}, and {\it e} commands to move to the beginning of the next Xword, beginning of the previous word, or to the end of the Xcurrent word. X XNow try typing the parentheses `(' and `)' and the braces `\{' and `\}'. XThey move by sentence and block of text, more or less. X XThere are lots more commands like these, but that's enough for Xnow. X X\subsection{Creating new text} XThe three most used commands for inserting new text into a file Xare {\it i} (insert), {\it a} (append), and {\it o} (open). When you type an {\it i}, vi Xstarts inserting new text just before the place where the cursor Xis. {\bf Remember} that while you are inserting text, the commands we Xjust learned in command mode no longer work. In insert mode, Xeverything you type will be inserted as text. X XTo get back to command mode, type the {\it $<$esc$>$} key. In other Xwords, type an {\it i}, then some text, and then an {\it $<$esc$>$} to return to Xcommand mode. Try the {\it i} command now. Move up a few lines, type Xan {\it i} followed by some text, and then an {\it $<$esc$>$}, and see what Xhappens. You can type pages and pages of text while in insert Xmode. X XThe {\it a} command works the same way, except that it begins inserting Xtext just after the cursor. The {\it o} command creates a new blank Xline just below the current line, and begins inserting text Xthere. There is also an {\it O} command, that opens a blank line just Xabove the current line. Try {\it a}, {\it o}, and {\it O} now. X X\subsection{Deleting text} XWhile you are in insert mode, the backspace key works the usual Xway. Also, {\it $\hat{}$ W} deletes the previous word, and your 'kill' Xcharacter backs up to the beginning of the current line. (The Xkill character is usually @ or {\it $\hat{}$ U}, but might be something else. Use X{\it stty -a} to see.) X XOtherwise, to delete text you should usually be in command mode X(so type {\it $<$esc$>$}). An {\it x} deletes the current character. Type {\it dw} Xto delete to the beginning of the next word, {\it d}) to delete to the Xend of the sentence, and {\it d}\} to delete to the end of the block of Xtext. In fact, a {\it d} followed by almost any cursor moving command Xsuch as {\it b}, {\it e}, or `\{` will delete the stuff the cursor would have Xmoved over. One that is not so obvious is {\it dd}, which deletes the Xentire current line. X X\subsection{Oops} XSome commands modify the text that you are working on. The {\it u} Xcommand will undo the most recent text modifying command. XEspecially useful when you accidently delete the 10 pages you Xjust typed in. Just type {\it u} to get it back. X X\subsection{Starting a vi session} XThe command to start vi is X X\footnotesize X\begin{verbatim} X $ vi [file ...] X\end{verbatim} X\normalsize X Xat the command prompt. You can type the names of zero or more files Xafter vi. If you don't type any filename, vi will show you an Xempty buffer to work in, and you can write it to a file later. XIf you give it the name of a file that doesn't exist, it will Xcreate it for you. If you give vi more than one file name, you Xcan edit them one after another. X XVi does not modify the file on the disk until you tell it to do Xso. All operations are done on a copy of the file kept in main Xmemory. Note that this sometimes places a limit on the maximum Xsize file vi can handle, though it helps make vi pretty fast. X X\subsection{Ending a session} XTo modify the file on disk, so that it includes all your edits, Xyou need to use the third mode, line editing. Type a `:' to enter Xline mode. To write the buffer out to the file, type a {\it w}, Xfollowed by a carriage return (or whatever it is called on your Xterminal). All the `:' commands need a carriage return at the Xend--look for $<$cr$>$ in this tutorial. X XVi does {\bf not}, repeat {\bf not not not}, make a backup copy of your file in Xits original state. As soon as you do the {\it :w} command, the Xoriginal version of the file is replaced with the current Xversion. If you want a backup copy, make one yourself with a Xcommand such as X X\footnotesize X\begin{verbatim} X $ cp april.data april.data.bak X\end{verbatim} X\normalsize X XYou can even do this while in vi, before the first time you write Xwith a {\it :w} by "forking a shell." The `:!' command allows you Xto execute any Unix shell command from within vi, so X X\footnotesize X\begin{verbatim} X :!cp file file.bak<cr> X\end{verbatim} X\normalsize X Xwill create a backup file, X X\footnotesize X\begin{verbatim} X :!ls -l<cr> X\end{verbatim} X\normalsize X Xwill show you a directory Xlisting, and X X\footnotesize X\begin{verbatim} X :!vi anotherfile<cr> X\end{verbatim} X\normalsize X Xwill even start up another vi session in the middle of the Xcurrent one. X XTo exit from vi, use the {\it :q} command. If the file has been Xmodified, vi will warn you. If you really want to quit without Xsaving the modified file, add the "I really mean it" command like Xthis X X\footnotesize X\begin{verbatim} X :q!<cr> X\end{verbatim} X\normalsize X XYou can combine {\it :w} and {\it :q} into one command, X X\footnotesize X\begin{verbatim} X :wq<cr> X\end{verbatim} X\normalsize X Xwhich will write the buffer and then quit. If you are editing a Xlist of more than one file, you can go to the next with the X X\footnotesize X\begin{verbatim} X :n<cr> X\end{verbatim} X\normalsize X Xcommand. X XAt any time, you can write the current buffer into or over some Xother file by adding a filename to the {\it :w} command. If you want Xto write this buffer out to a file in your current directory Xnamed tutbuf, just use X X\footnotesize X\begin{verbatim} X :w tutbuf<cr> X\end{verbatim} X\normalsize X X\subsection{Searching} XThe search commands are a handy way to make big jumps around a Xfile. Searching can be done from wither command mode or line Xmode. In command mode, the `/' searches forward in the file and Xthe ? searches backwards. Try typing X X\footnotesize X\begin{verbatim} X ?tutbuf<cr> X\end{verbatim} X\normalsize X Xnow, to search backwards for the string tutbuf. Vi remembers Xthe last string you searched for, so now another ?{\it $<$cr$>$} will Xsearch again. The `/' works the same way in the forward Xdirection. By the way, vi searches wrap around to the other end Xof the file when they hit the end. X XSuppose you wanted to go back to the section on delete. Just search for some Xpattern that you guess appears there--how about "delete"? XSearch for "delete" with X X\footnotesize X\begin{verbatim} X /delete<cr> X\end{verbatim} X\normalsize X Xand see what happens. X XThe pattern you search for can use the (almost) standard Unix Xpattern matching language. It is very powerful, so we won't Xconsider it in detail here, but as useless but neat examples, X X\footnotesize X\begin{verbatim} X /Th[^e]<cr> X\end{verbatim} X\normalsize X Xwill search for any "Th" followed by any character except an X"e", and X X\footnotesize X\begin{verbatim} X /^[A-Z].*\. *$<cr> X\end{verbatim} X\normalsize X Xwill find any line that begins with a capital letter and ends Xwith a period and any number of blanks. Try it. Then try Xsomething like X X\footnotesize X\begin{verbatim} X /"e"<cr> X\end{verbatim} X\normalsize X Xto get back here. X XThe same pattern matching can be used in line mode. For example, Xto change the first occurrence of "the" on the current line to X"that", you can type X X\footnotesize X\begin{verbatim} X :s/the/fathead/<cr> X\end{verbatim} X\normalsize X Xand to change all the occurrences, you add the "global" modifier, X X\footnotesize X\begin{verbatim} X :s/the/fathead/g<cr> X\end{verbatim} X\normalsize X X\subsection{Line mode commands} XThe commands that are introduced with the `:' throw vi into a Xversion of Unix's venerable old line editor, ed. The current Xversion is named ex. Ex and vi are really two modes in one Xeditor. There are many powerful commands in line mode. One that Xis used often is two substitute one string for every occurrence Xof another, not just in one line but in the entire file. The {\it :s} Xcommand above can be modified by putting line numbers in front of Xthe {\it s}. To change all "the" into "fathead" in every line of the Xfile, try X X\footnotesize X\begin{verbatim} X :1,$s/the/fathead/g X\end{verbatim} X\normalsize X XYou can type {\it u} to undo it. The {\it 1,\$} in front of the {\it s} says X"starting at line 1 and continuing to the last line of the file". XIf you wanted to do the substitution in lines 10 through 20, just Xuse 10,20. In this context, the . means the current line, so if Xyou just want to do the subs in the first through the current Xline, use 1,. and so on. X XAnother handy line mode command is delete, {\it :d}. For example X X\footnotesize X\begin{verbatim} X :d delete current line X :1,3d delete lines 1 to 3 X :1,.d delete first through current line X :.,$d delete current through last line X :.,/fathead/d delete current through next line that X contains the pattern "fathead" X\end{verbatim} X\normalsize X XOn last handy line mode command finds all the lines in the file Xthat match a pattern, and preints them on the screen without Xmodifying the file. Try this command to get all the lines that Xinclude the word "fathead". X X\footnotesize X\begin{verbatim} X :g/fathead/p X\end{verbatim} X\normalsize X X X\subsection{More editing} XThere are other useful commands. The {\it c} command lets you change Xtext. For example, if you type {\it cw}, a dollar sign will appear at Xthe end of the current word, and you will be put in insert mode. XEverything you type will replace the current word when you type X{\it $<$esc$>$}. Likewise, {\it c)} will allow you to change the rest of the Xsentence, {\it c(} will change the first part of the sentence, {\it ct}, will Xchange everything up to the next comma, and more. The {\it cc} command Xchanges the whole line. You can put a small number at the Xbeginning of any of these commands: {\it 3cw} changes the next three Xwords, and so on. X XWhen characters are deleted from the buffer, they go into an Xunnamed area from which they can be recalled. Suppose you delete Xthree lines with a {\it 3dd} command. Later, a {\it p} command will put Xthose three lines immediately after the current line. Try it. XA {\it P} command puts the lines before the current line. X X\subsection{Vi meets the shell} XThe Unix shell offers many useful utilities such as sort, awk, Xwc, plus dozens of others. These commands can be "integrated" Xinto vi using the `!' command (not to be confused with the `:!' line Xmode command. Using `!' and a cursor motion command, you can send Xportions of your buffer to a Unix command and get the stdout of Xthe command back in your file. Suppose you have a list of names Xyou've typed X X\footnotesize X\begin{verbatim} X Lou X Lee X Jim X Mark X Glenn X\end{verbatim} X\normalsize X X(only many more names) and now you wish they were sorted. Since Xthe lines make up a block of text, you can use the `!'\} command to Xsend them to sort. Just move the cursor to the line containing X"Lou" and type X X\footnotesize X\begin{verbatim} X !}sort "send block to sort" X\end{verbatim} X\normalsize X Xand the lines will be replaced with X X\footnotesize X\begin{verbatim} X Glenn X Jim X Lee X Lou X Mark X\end{verbatim} X\normalsize X XThere are five names. Want the names back? Use {\it u} to undo. X XSay you needed to know how many names there were. The wc -l Xcommand counts the number of lines in its input, so send the Xblock to wc. Use !\}{\it wc -l}. The block is removed from the text, Xsent to wc, and the output of wc replaces the original text. X X\subsection{Macros and abbreviations} XYou can define simple macros with the {\it :map} command. Suppose we Xused the block sort above a lot. Then we might use X X\footnotesize X\begin{verbatim} X :map <^K> !}sort X\end{verbatim} X\normalsize X Xto define {\it $\hat{}$ K} to be the sorter. (You cannot type a {\it $\hat{}$ M} directly. XUse {\it $\hat{}$ V} to quote the {\it $\hat{}$ M} so that vi will ignore it.) X XAre you sick and tired of typing National Fuel Gas? Try an Xabbreviation X X\footnotesize X\begin{verbatim} X :abbr nfg National Fuel Gas X\end{verbatim} X\normalsize X XNow, every time you type the word, {\it nfg}, it is replaced magically Xby National Fuel Gas. X X\subsection{What next} XPractice makes perfect. Use vi to create files, even if there is Xa better way to do it. You need the practice so that when you Xneed to do heavy modifications to that TSE input file, you can do Xthem quickly and efficiently. X XThe other thing to do is to read whatever vi manual you can find. X X\newpage X\section{VI Reference} X% X\subsection{VI Reference} XWarning: some vi versions don't support the more esoteric features Xdescribed in this document. You can edit/redistribute this document Xfreely, as long as you don't make false claims on original authorship. X X\subsection{Legenda} X X\footnotesize X\begin{verbatim} X default values : 1 X <*> : `*' must not be taken literally X [*] : `*' is optional X ^X : <ctrl>X X <sp> : Space X <cr> : Carriage return X <lf> : Linefeed X <ht> : Horizontal tab X <esc> : Escape X <erase> : Your erase character X <kill> : Your kill character X <intr> : Your interrupt character X <a-z> : An element in the range X N : Number (`*' = allowed, `-' = not appropriate) X CHAR : Char unequal to <ht>|<sp> X WORD : Word followed by <ht>|<sp>|<lf> X\end{verbatim} X\normalsize X X\newpage X\subsection{Move commands (See also Display commands)} X X\footnotesize X\begin{verbatim} X N | Command | Meaning X ---+--------------------+----------------------------------------------- X * | h | ^H | <erase> | <*> chars to the left. X * | j | <lf> | ^N | <*> lines downward. X * | l | <sp> | <*> chars to the right. X * | k | ^P | <*> lines upward. X * | $ | To the end of line <*> from the cursor. X - | ^ | To the first CHAR of the line. X * | _ | To the first CHAR <*> - 1 lines lower. X * | - | To the first CHAR <*> lines higher. X * | + | <cr> | To the first CHAR <*> lines lower. X - | 0 | To the first char of the line. X * | | | To column <*> (<ht>: only to the endpoint). X * | f<char> | <*> <char>s to the right (find). X * | t<char> | Till before <*> <char>s to the right. X * | F<char> | <*> <char>s to the left. X * | T<char> | Till after <*> <char>s to the left. X * | ; | Repeat latest `f'|`t'|`F'|`T' <*> times. X * | , | Idem in opposite direction. X * | w | <*> words forward. X * | W | <*> WORDS forward. X * | b | <*> words backward. X * | B | <*> WORDS backward. X * | e | To the end of word <*> forward. X * | E | To the end of WORD <*> forward. X * | G | Go to line <*> (default EOF). X * | H | To line <*> from top of the screen (home). X * | L | To line <*> from bottom of the screen (last). X - | M | To the middle line of the screen. X * | ) | <*> sentences forward. X * | ( | <*> sentences backward. X * | } | <*> paragraphs forward. X * | { | <*> paragraphs backward. X - | ]] | To the next section (default EOF). X - | [[ | To the previous section (default begin of file). X - | `<a-z> | To the mark. X - | '<a-z> | To the first CHAR of the line with the mark. X - | `` | To the cursor position before the latest absolute X | jump (of which are examples `/' and `G'). X - | '' | To the first CHAR of the line on which the cursor X | was placed before the latest absolute jump. X - | /<string> | To the next occurrence of <string>. X - | ?<string> | To the previous occurrence of <string>. X - | n | Repeat latest `/'|`?' (next). X - | N | Idem in opposite direction. X - | % | Find the next bracket and go to its match X | (also with `{'|`}' and `['|`]'). X\end{verbatim} X\normalsize X X\subsection{Searching} X X\footnotesize X\begin{verbatim} X :ta <name> | Search in the tags file[s] where <name> is X | defined (file, line), and go to it. X ^] | Use the name under the cursor in a `:ta' command. X ^T | Pop the previous tag off the tagstack and return X | to its position. X :[x,y]g/<string>/<cmd> | Search globally [from line x to y] for <string> X | and execute the `ex' <cmd> on each occurrence. X :[x,y]v/<string>/<cmd> | Execute <cmd> on the lines that don't match. X\end{verbatim} X\normalsize X X\subsection{Undoing changes} X X\footnotesize X\begin{verbatim} X u | Undo the latest change. X U | Undo all changes on a line, while not having X | moved off it (unfortunately). X :q! | Quit vi without writing. X :e! | Re-edit a messed-up file. X\end{verbatim} X\normalsize X X\subsection{Appending text} XEnd appending text with $<$esc$>$ X X\footnotesize X\begin{verbatim} X * | a | <*> times after the cursor. X * | A | <*> times at the end of line. X * | i | <*> times before the cursor (insert). X * | I | <*> times before the first CHAR of the line X * | o | On a new line below the current (open). X | The count is only useful on a slow terminal. X * | O | On a new line above the current. X | The count is only useful on a slow terminal. X * | ><move> | Shift the lines described by <*><move> one X | shiftwidth to the right. X * | >> | Shift <*> lines one shiftwidth to the right. X * | ["<a-zA-Z1-9>]p | Put the contents of the (default undo) buffer X | <*> times after the cursor. X | A buffer containing lines is put only once, X | below the current line. X * | ["<a-zA-Z1-9>]P | Put the contents of the (default undo) buffer X | <*> times before the cursor. X | A buffer containing lines is put only once, X | above the current line. X * | . | Repeat previous command <*> times. If the last X | command before a `.' command references a X | numbered buffer, the buffer number is X | incremented first (and the count is ignored): X | X | "1pu.u.u.u.u - `walk through' buffers 1 X | through 5 X | "1P.... - restore them X\end{verbatim} X\normalsize X X\subsection{Deleting text} XEverything deleted can be stored into a buffer. This is achieved by Xputting a `"' and a letter $<$a-z$>$ before the delete command. The Xdeleted text will be in the buffer with the used letter. If $<$A-Z$>$ Xis used as buffer name, the adjugate buffer $<$a-z$>$ will be augmented Xinstead of overwritten with the text. The undo buffer always Xcontains the latest change. Buffers $<$1-9$>$ contain the latest 9 XLINE deletions (`"1' is most recent). X X\footnotesize X\begin{verbatim} X * | x | Delete <*> chars under and after the cursor. X * | X | <*> chars before the cursor. X * | d<move> | From begin to endpoint of <*><move>. X * | dd | <*> lines. X - | D | The rest of the line. X * | <<move> | Shift the lines described by <*><move> one X | shiftwidth to the left. X * | << | Shift <*> lines one shiftwidth to the left. X * | . | Repeat latest command <*> times. X\end{verbatim} X\normalsize X X\subsection{Changing text} XEnd changing text with $<$esc$>$ X X\footnotesize X\begin{verbatim} X * | r<char> | Replace <*> chars by <char> - no <esc>. X * | R | Overwrite the rest of the line, X | appending change <*> - 1 times. X * | s | Substitute <*> chars. X * | S | <*> lines. X * | c<move> | Change from begin to endpoint of <*><move>. X * | cc | <*> lines. X * | C | The rest of the line and <*> - 1 next lines. X * | =<move> | If the option `lisp' is set, this command X | will realign the lines described by <*><move> X | as though they had been typed with the option X | `ai' set too. X - | ~ | Switch lower and upper cases X | (should be an operator, like `c'). X * | J | Join <*> lines (default 2). X * | . | Repeat latest command <*> times (`J' only once). X - | & | Repeat latest `ex' substitute command, e.g. X | `:s/wrong/good'. X - | :[x,y]s/<p>/<r>/<f>| Substitute (on lines x through y) the pattern <p> X | (default the last pattern) with <r>. Useful X | flags <f> are `g' for `global' (i.e. change X | every non-overlapping occurrence of <p>) and X | `c' for `confirm' (type `y' to confirm a X | particular substitution, else <cr>). Instead X | of `/' any punctuation CHAR unequal to <lf> X | can be used as delimiter. X\end{verbatim} X\normalsize X X\subsection{Substitute replacement patterns} XThe basic meta-characters for the replacement pattern are `\&' and ` $\tilde{}$ '; Xthese are given as `$\backslash$\&' and `$\backslash \tilde{}$ ' when nomagic is set. Each instance Xof `\&' is replaced by the characters which the regular expression Xmatched. The meta-character ` $\tilde{}$ ' stands, in the replacement Xpattern, for the defining text of the previous replacement Xpattern. Other meta-sequences possible in the replacement pattern Xare always introduced by the escaping character `$\backslash$'. The sequence X`$\backslash$n' (with `n' in [1--9]) is replaced by the text matched by the Xn--th regular subexpression enclosed between `$\backslash$(' and `$\backslash$)'. The Xsequences `$\backslash$u' and `$\backslash$l' cause the immediately following character Xin the replacement to be converted to upper- or lower-case Xrespectively if this character is a letter. The sequences `$\backslash$U' and X`$\backslash$L' turn such conversion on, either until `$\backslash$E' or `$\backslash$e' is Xencountered, or until the end of the replacement pattern. X X\subsection{Remembering text (yanking)} XWith yank commands you can put `"$<$a--zA--Z$>$' before the command, just as Xwith delete commands. Otherwise you only copy to the undo buffer. XThe use of buffers $<$a-z$>$ is {\bf the} way of copying text to another file; Xsee the `:e $<$file$>$' command. X X\footnotesize X\begin{verbatim} X * | y<move> | Yank from begin to endpoint of <*><move>. X * | yy | <*> lines. X * | Y | Idem (should be equivalent to `y$' though). X - | m<a-z> | Mark the cursor position with a letter. X\end{verbatim} X\normalsize X X\subsection{Commands while in append or change mode} X X\footnotesize X\begin{verbatim} X ^@ | If typed as the first character of the X | insertion, it is replaced with the previous X | text inserted (max. 128 chars), after which X | the insertion is terminated. X ^V | Deprive the next char of its special meaning X | (e.g. <esc>). X ^D | One shiftwidth to the left. X 0^D | Remove all indentation on the current line X | (there must be no other chars on the line). X ^^D | Idem, but it is restored on the next line. X ^T | One shiftwidth to the right X ^H | <erase> | One char back. X ^W | One word back. X <kill> | Back to the begin of the change on the X | current line. X <intr> | Like <esc> (but you get a beep as well). X\end{verbatim} X\normalsize X X\subsection{Writing, editing other files, and quitting vi} XIn `:' `ex' commands `\%' denotes the current file, `\#' is a synonym for Xthe alternate file (which normally is the previous file). XMarks can be used for line numbers too: '$<$a-z$>$. XIn the `:w' $\mid$ `:f' $\mid$ `:cd' $\mid$ `:e' $\mid$ `:n' commands shell Xmeta-characters can be used. X X\footnotesize X\begin{verbatim} X :q | Quit vi, unless the buffer has been changed. X :q! | Quit vi without writing. X ^Z | Suspend vi. X :w | Write the file. X :w <name> | Write to the file <name>. X :w >> <name> | Append the buffer to the file <name>. X :w! <name> | Overwrite the file <name>. X :x,y w <name> | Write lines x through y to the file <name>. X :wq | Write the file and quit vi; some versions quit X | even if the write was unsuccessful! X | Use `ZZ' instead. X ZZ | Write if the buffer has been changed, and X | quit vi. If you have invoked vi with the `-r' X | option, you'd better write the file X | explicitly (`w' or `w!'), or quit the X | editor explicitly (`q!') if you don't want X | to overwrite the file - some versions of vi X | don't handle the `recover' option very well. X :x [<file>] | Idem [but write to <file>]. X :x! [<file>] | `:w![<file>]' and `:q'. X :pre | Preserve the file - the buffer is saved as if X | the system had just crashed; for emergencies, X | when a `:w' command has failed and you don't X | know how to save your work (see `vi -r'). X :f <name> | Set the current filename to <name>. X :cd [<dir>] | Set the working directory to <dir> X | (default home directory). X :cd! [<dir>] | Idem, but don't save changes. X :e [+<cmd>] <file> | Edit another file without quitting vi - the X | buffers are not changed (except the undo X | buffer), so text can be copied from one file to X | another this way. [Execute the `ex' command X | <cmd> (default `$') when the new file has been X | read into the buffer.] <cmd> must contain no X | <sp> or <ht>. See `vi startup'. X :e! [+<cmd>] <file> | Idem, without writing the current buffer. X ^^ | Edit the alternate (normally the previous) file. X :rew | Rewind the argument list, edit the first file. X :rew! | Idem, without writing the current buffer. X :n [+<cmd>] [<files>] | Edit next file or specify a new argument list. X :n! [+<cmd>] [<files>] | Idem, without writing the current buffer. X :args | Give the argument list, with the current file X | between `[' and `]'. X\end{verbatim} X\normalsize X X\subsection{Display commands (See also Move commands)} X X\footnotesize X\begin{verbatim} X ^G | Give file name, status, current line number X | and relative position. X ^L | Refresh the screen (sometimes `^P' or `^R'). X ^R | Sometimes vi replaces a deleted line by a `@', X | to be deleted by `^R' (see option `redraw'). X [*]^E | Expose <*> more lines at bottom, cursor X | stays put (if possible). X [*]^Y | Expose <*> more lines at top, cursor X | stays put (if possible). X [*]^D | Scroll <*> lines downward X | (default the number of the previous scroll; X | initialization: half a page). X [*]^U | Scroll <*> lines upward X | (default the number of the previous scroll; X | initialization: half a page). X [*]^F | <*> pages forward. X [*]^B | <*> pages backward (in older versions `^B' only X | works without count). X\end{verbatim} X\normalsize X XIf in the next commands the field $<$wi$>$ is present, the windowsize Xwill change to $<$wi$>$. The window will always be displayed at the Xbottom of the screen. X X\footnotesize X\begin{verbatim} X [*]z[wi]<cr> | Put line <*> at the top of the window X | (default the current line). X [*]z[wi]+ | Put line <*> at the top of the window X | (default the first line of the next page). X [*]z[wi]- | Put line <*> at the bottom of the window X | (default the current line). X [*]z[wi]^ | Put line <*> at the bottom of the window X | (default the last line of the previous page). X [*]z[wi]. | Put line <*> in the centre of the window X | (default the current line). X\end{verbatim} X\normalsize X X\newpage X\subsection{Mapping and abbreviation} XWhen mapping take a look at the options `to' and `remap' (below). X X\footnotesize X\begin{verbatim} X :map <string> <seq> | <string> is interpreted as <seq>, e.g. X | `:map ^C :!cc %^V<cr>' to invoke `cc' (the C X | compiler) from within the editor X | (vi replaces `%' with the current file name). X :map | Show all mappings. X :unmap <string> | Deprive <string> of its mapping. When vi X | complains about non-mapped macros (whereas no X | typos have been made), first do something like X | `:map <string> Z', followed by X | `:unmap <string>' (`Z' must not be a macro X | itself), or switch to `ex' mode first with `Q'. X :map! <string> <seq> | Mapping in append mode, e.g. X | `:map! \be begin^V<cr>end;^V<esc>O<ht>'. X | When in append mode <string> is preceded by X | `^V', no mapping is done. X :map! | Show all append mode mappings. X :unmap! <string> | Deprive <string> of its mapping (see `:unmap'). X :ab <string> <seq> | Whenever in append mode <string> is preceded and X | followed by a breakpoint (e.g. <sp> or `,'), it X | is interpreted as <seq>, e.g. X | `:ab ^P procedure'. A `^V' immediately X | following <string> inhibits expansion. X :ab | Show all abbreviations. X :unab <string> | Do not consider <string> an abbreviation X | anymore (see `:unmap'). X @<a-z> | Consider the contents of the named register a X | command, e.g.: X | o0^D:s/wrong/good/<esc>"zdd X | Explanation: X | o - open a new line X | 0^D - remove indentation X | :s/wrong/good/ - this input text is an X | `ex' substitute command X | <esc> - finish the input X | "zdd - delete the line just X | created into register `z' X | Now you can type `@z' to replace `wrong' X | with `good' on the current line. X @@ | Repeat last register command. X\end{verbatim} X\normalsize X X\newpage X\subsection{Switch and shell commands} X X\footnotesize X\begin{verbatim} X Q | ^\ | <intr><intr> | Switch from vi to `ex'. X : | An `ex' command can be given. X :vi | Switch from `ex' to vi. X :sh | Execute a subshell, back to vi by `^D'. X :[x,y]!<cmd> | Execute a shell <cmd> [on lines x through y; X | these lines will serve as input for <cmd> and X | will be replaced by its standard output]. X :[x,y]!! [<args>] | Repeat last shell command [and append <args>]. X :[x,y]!<cmd> ! [<args>] | Use the previous command (the second `!') in a X | new command. X [*]!<move><cmd> | The shell executes <cmd>, with as standard X | input the lines described by <*><move>, X | next the standard output replaces those lines X | (think of `cb', `sort', `nroff', etc.). X [*]!<move>!<args> | Append <args> to the last <cmd> and execute it, X | using the lines described by the current X | <*><move>. X [*]!!<cmd> | Give <*> lines as standard input to the X | shell <cmd>, next let the standard output X | replace those lines. X [*]!!! [<args>] | Use the previous <cmd> [and append <args> to it]. X :x,y w !<cmd> | Let lines x to y be standard input for <cmd> X | (notice the <sp> between the `w' and the `!'). X :r!<cmd> | Put the output of <cmd> onto a new line. X :r <name> | Read the file <name> into the buffer. X\end{verbatim} X\normalsize X X\newpage X\subsection{Vi startup} X X\footnotesize X\begin{verbatim} X vi [<files>] | Edit the files, start with the first page of X | the first file. X\end{verbatim} X\normalsize X XThe editor can be initialized by the shell variable `EXINIT', which Xlooks like: X X\footnotesize X\begin{verbatim} X EXINIT='<cmd>|<cmd>|...' X <cmd>: set options X map ... X ab ... X export EXINIT (in the Bourne shell) X\end{verbatim} X\normalsize X XHowever, the list of initializations can also be put into a file. XIf this file is located in your home directory, and is named `.exrc' X{\bf and} the variable `EXINIT' is {\bf not} set, the list will be executed Xautomatically at startup time. However, vi will always execute the Xcontents of a `.exrc' in the current directory, if you own the file. XElse you have to give the execute (`source') command yourself: X X\footnotesize X\begin{verbatim} X :so file X\end{verbatim} X\normalsize X XIn a `.exrc' file a comment is introduced with a double quote character: Xthe rest of the line is ignored. Exception: if the last command on the Xline is a `map[!]' or `ab' command or a shell escape, a trailing comment Xis not recognized, but considered part of the command. X XOn-line initializations can be given with `vi $+<$cmd$>$ file', e.g.: X X\footnotesize X\begin{verbatim} X vi +x file | The cursor will immediately jump to line x X | (default last line). X vi +/<string> file | Jump to the first occurrence of <string>. X\end{verbatim} X\normalsize X XYou can start at a particular tag with: X X\footnotesize X\begin{verbatim} X vi -t <tag> | Start in the right file in the right place. X\end{verbatim} X\normalsize X XSometimes (e.g. if the system crashed while you were editing) it is Xpossible to recover files lost in the editor by `vi -r file'. A plain X`vi -r' command shows the files you can recover. XIf you just want to view a file by using vi, and you want to avoid any Xchange, instead of vi you can use the `view' or `vi -R' command: Xthe option `readonly' will be set automatically (with `:w!' you can Xoverride this option). X X\newpage X\subsection{The most important options} X X\footnotesize X\begin{verbatim} X ai | autoindent - In append mode after a <cr> the X | cursor will move directly below the first X | CHAR on the previous line. However, if the X | option `lisp' is set, the cursor will align X | at the first argument to the last open list. X aw | autowrite - Write at every shell escape X | (useful when compiling from within vi). X dir=<string> | directory - The directory for vi to make X | temporary files (default `/tmp'). X eb | errorbells - Beeps when you goof X | (not on every terminal). X ic | ignorecase - No distinction between upper and X | lower cases when searching. X lisp | Redefine the following commands: X | `(', `)' - move backward (forward) over X | S-expressions X | `{', `}' - idem, but don't stop at atoms X | `[[', `]]' - go to previous (next) line X | beginning with a `(' X | See option `ai'. X list | <lf> is shown as `$', <ht> as `^I'. X magic | If this option is set (default), the chars `.', X | `[' and `*' have special meanings within search X | and `ex' substitute commands. To deprive such X | a char of its special function it must be X | preceded by a `\'. If the option is turned off X | it's just the other way around. Meta-chars: X | ^<string> - <string> must begin the line X | <string>$ - <string> must end the line X | . - matches any char X | [a-z] - matches any char in the range X | [^a-z] - any char not in the range X | [<string>] - matches any char in <string> X | [^<string>] - any char not in <string> X | <char>* - 0 or more <char>s X | \<<string> - <string> must begin a word X | <string>\> - <string> must end a word X modeline | When you read an existing file into the buffer, X | and this option is set, the first and last 5 X | lines are checked for editing commands in the X | following form: X | X | <sp>vi:set options|map ...|ab ...|!...: X | X | Instead of <sp> a <ht> can be used, instead of X | `vi' there can be `ex'. Warning: this option X | could have nasty results if you edit a file X | containing `strange' modelines. X nu | number - Numbers before the lines. X para=<string> | paragraphs - Every pair of chars in <string> is X | considered a paragraph delimiter nroff macro X | (for `{' and `}'). A <sp> preceded by a `\' X | indicates the previous char is a single letter X | macro. `:set para=P\ bp' introduces `.P' and X | `.bp' as paragraph delimiters. Empty lines and X | section boundaries are paragraph boundaries X | too. X redraw | The screen remains up to date. X remap | If on (default), macros are repeatedly X | expanded until they are unchanged. X | Example: if `o' is mapped to `A', and `A' X | is mapped to `I', then `o' will map to `I' X | if `remap' is set, else it will map to `A'. X report=<*> | Vi reports whenever e.g. a delete X | or yank command affects <*> or more lines. X ro | readonly - The file is not to be changed. X | However, `:w!' will override this option. X sect=<string> | sections - Gives the section delimiters (for `[[' X | and `]]'); see option `para'. A `{' beginning a X | line also starts a section (as in C functions). X sh=<string> | shell - The program to be used for shell escapes X | (default `$SHELL' (default `/bin/sh')). X sw=<*> | shiftwidth - Gives the shiftwidth (default 8 X | positions). X sm | showmatch - Whenever you append a `)', vi shows X | its match if it's on the same page; also with X | `{' and `}'. If there's no match at all, vi X | will beep. X taglength=<*> | The number of significant characters in tags X | (0 = unlimited). X tags=<string> | The space-separated list of tags files. X terse | Short error messages. X to | timeout - If this option is set, append mode X | mappings will be interpreted only if they're X | typed fast enough. X ts=<*> | tabstop - The length of a <ht>; warning: this is X | only IN the editor, outside of it <ht>s have X | their normal length (default 8 positions). X wa | writeany - No checks when writing (dangerous). X warn | Warn you when you try to quit without writing. X wi=<*> | window - The default number of lines vi shows. X wm=<*> | wrapmargin - In append mode vi automatically X | puts a <lf> whenever there is a <sp> or <ht> X | within <wm> columns from the right margin X | (0 = don't put a <lf> in the file, yet put it X | on the screen). X ws | wrapscan - When searching, the end is X | considered `stuck' to the begin of the file. X :set <option> | Turn <option> on. X :set no<option> | Turn <option> off. X :set <option>=<value> | Set <option> to <value>. X :set | Show all non-default options and their values. X :set <option>? | Show <option>'s value. X :set all | Show all options and their values. X\end{verbatim} X\normalsize X X\end{document} + END-OF-FILE vi.doc.tex chmod 'u=rw,g=,o=' 'vi.doc.tex' set `wc -c 'vi.doc.tex'` count=$1 case $count in 48768) :;; *) echo 'Bad character count in ''vi.doc.tex' >&2 echo 'Count should be 48768' >&2 esac exit 0 -- "That's right, you wanted the "Self-Destruct" button, to your left." (John Woods)