Quick Guide to "vi" commands: 11th April, 1986 Alastair Milne (see "man vi" for command-line options) I. Single-Character Commands: Command Effect of Command 1. Movement Commands: a. Movement by Single Positions: up + down arrows, move to previous or next line of text respectively. Stay in same column if it is within line, otherwise move over to the line. Repeat Factor: move lines. left + right arrows, move left or right on one line of text, , by one character, staying on that line. Repeat Factor: move characters. (vi's actual directional move commands. The terminals arrow keys, if any, are macros mapped onto these commands. Repeat Factor: as above.) h move left l move right j move down k move up b. Movement to Named Positions: ("markers" are single letters 'a' through 'z' which can be used to label specific character positions in the file. They exist only during a single vi session; they are not saved with the file.) m waits for letter as marker, and associates it with cursor's position (line and character) in the file. ` waits for letter as marker name, and jumps to associated character position, if defined. ' waits for letter as marker name, and jumps to first non-space character on associated line, if defined. '' jumps to first non-space character on line of most recent "remembered" position in file `` jumps to most recent "remembered" position in file (similar to "=" in p-System editor). Repeat Factor: NOT ALLOWED c. Movement by words: (lower case forms consider punctuation characters and white space delimiters; upper case considers only white space delimiters) w, W move to start of next word. Repeat Factor: move to 'th word (NOTE: see NOTEs under "C" and "D" for special case) e, E move to end of current word, or of next if already at end of current. Repeat Factor: move to 'th word b, B move to beginning of current or previous word. Repeat Factor: move to 'th word d. Movement by Lines: H, M, L go to highest, middle, and lowest line of text on screen respectively, first character on line. Repeat Factor: go to line from respective end of screen (ignored by "M") G go to final line in file, first character Repeat Factor: go to line ; 1st line is 1 - move to first character on previous line Repeat Factor: move to first character lines up. + move to first character on next line Repeat Factor: move to first character lines down. e. Movement on Lines: $ move to last character on current line Repeat Factor: use line past current. _ (underscore) move to first character on current line Repeat Factor: use line past current. ^ move to first character on current line Repeat Factor: NO EFFECT | move to column on current line. move to column 1 if no factor given ^D (ctrl-D) WHEN INSERTING: if beginning insertion on a new line, ^D moves left past the start of insertion, as far as the leftmost column. Does not wrap back to previous line. Not otherwise effective in insertion. Repeat Factor: impossible to enter OTHERWISE: See Movement by Screenfuls, below. f. Movement by Structures: (,) move to start of current and next sentence, respectively. "(" Moves to start of previous sentence if already at start of current. Sentence end is at least 2 spaces following ".", "!", or "?" Repeat Factor: move to start of 'th sentence in indicated direction {,} as for "(" and ")", but by paragraphs, not sentences. Paragraphs are delimited by completely blank lines (not even white space). Repeat Factor: move to start of 'th paragraph in indicated direction [[,]] as for "(" and ")", but by "sections", not sentences. "Sections" are a text formatting concept; they are delimited by any of the list of delimiters specified by the variable "sections"; see "Variables", below. If file has no such delimiters, it is all considered one section. Repeat Factor: move to start of 'th section in indicated direction % If the cursor is on a bracket, parenthesis, or brace bracket, "%" jumps to its mate, skipping any pairs nested. Sounds a tone and doesn't move if no match is found. "<" and ">" are not considered brackets. Repeat Factor: ignored g. Movement by Screenfuls and Partial Screenfuls: ^F (ctrl-F), ^B (ctrl-B) jump by a screenful of text, with 2 or 3 lines of overlap. ^F jumps forward, ^B backward. Repeat Factor: ^F moves ahead pages ^B action unclear. ^D (ctrl-D), ^U (ctrl-U) scroll by half a screen. ^D scrolls down, ^U scrolls up. Keeps cursor on same row of screen. Repeat Factor: scroll lines. ^E (ctrl-E), ^Y (ctrl-Y) scroll the screen one line, moving the cursor on the screen to keep its position in the text. ^E moves the screen up, ^Y moves it down. Repeat Factor: moves lines. 2. Find Commands: a. single line: These commands apply only to the line containing the cursor. If the search target is not in the line, it is not found. If the search fails, the cursor is not moved. f, F wait for entry of character to find, and move onto it. ESCAPE instead of character cancels find. (Lower case searches to the right, upper case to the left) Repeat Factor: find 'th occurrence of the char in the respective direction. t, T wait for entry of character to find, and move to character immediately ahead of it, searching to the right. ESCAPE instead of character cancels find. (Upper case searches to the left, and moves immediately after it.) Repeat Factor: find 'th occurrence of the char. ; repeats most recent f, F, t, or T command, to search for next occurrence of same character. Any repeat factor given with that command is not used. Repeat Factor: find 'th occurrence of the char. ,(i.e. comma) repeats most recent f, F, t, or T command, but in the opposite direction. Useful if ; was pressed too often. Any repeat factor given with that command is not used. Repeat Factor: find 'th occurrence of the char. b. whole file: /, ? searches forward or backward, respectively, for a pattern. The user is prompted with a "?" or "/" at the bottom of the screen, and enters the pattern to search for, finishing with either or . If the pattern can't be found, a highlighted message saying so is given, and the cursor is not moved. The user can avoid doing the search by backspacing out of the entry of the pattern (i.e., backing one more than the length of pattern entered). If no pattern is entered, the most recent one used is used again. Repeat Factor: NO EFFECT Searches are controlled by 3 vi variables: magic: if set, the pattern is treated as a regular expression describing the class of acceptable patterns; if not, patterns are treated as literal strings. wrapscan: if set, if the search reaches one end of the file without success, it is resumed from the opposite end. If not, the search ends at the end of the file. ignorecase: if set, case of letters is ignored in the search. n,N repeats most recent / or ? command. Uses current settings of search control variables. lower case: search in same direction upper case: search in opposite direction Repeat Factor: NO EFFECT 3. Insert Commands: (allow insertion of text into file buffer. Any existing text following the insertion is moved back. All insertions are ended by pressing . Unwanted insertions can be eliminated with "undo" (see below) ) i, I start insertion of text immediately ahead of cursor position. (Upper case moves to first text position on line before inserting). Repeat Factor: causes copies of user's insertion to be inserted. a, A start insertion of text immediately after cursor position. (Upper case moves to end of line before inserting) Repeat Factor: as above o, O insert a new, blank line in text, leaving cursor at its start for insertion of text. Indentation of new line depends on setting of autoindent flag (see vi variables) and indentation of current line. (Upper case inserts new line ahead of current one, lower case inserts after) Repeat Factor: NO EFFECT. 4. Shifting Text Left or Right (Adjustment): >> moves the current line one shiftwidth to the right. (See vi variables, below). Repeat Factor: move lines, including the current, one shiftwidth. << As above, but to the left. > Waits for a movement command specifying how many lines to affect, and moves them one shiftwidth to the right. Beeps and does nothing if movement covers less than full line. Repeat Factor: the movement command is multiplied by . < As above, but to the left. 5. Undoing: u undoes the effect of the most recent insertion, deletion, change, replacement, or shift command (except that the deletion buffers (see below) remain as that command left them). It also undoes the effect of filtering a piece of text through another program (see 10., below). If used following a macro's execution, it undoes the entire macro. Used repeatedly in a row, each use undoes the effect of the previous one: undo can itself by undone. Repeat Factor: NO EFFECT U undoes all changes on the current line since the last U. NOTE on BUFFERS: A number of buffers are available for use the with deletion, change, yank, and put commands. Every deletion, change, or yank moves the affected text into the "unnamed" buffer, whence it may be immediately recovered. These operations also use 9 buffers named "1" through "9". Every time text is moved into the unnamed buffer, the contents of the buffers "1" to "8" are moved to the next buffer along, and the unnamed buffer is moved into buffer "1", before receiving its new text. This means that the text from the 10 most recent deletions, changes, and yanks, is always accessible. Besides these buffers, vi makes 26 buffers named "a" through "z" available to the user for keeping pieces of text. Vi itself does not modify them. Specifying one of them before doing a deletion, change, yank, or put causes it to be used instead of the unnamed buffer. They are accessed by typing just ahead of the delete, change, yank, or put command that will use them. These buffers can also be executed as vi macros. (See below under Macros.) Buffers are not preserved with the file. They last only until the vi session ends. Also, the command ":e" is used to change files, the unnamed buffer is lost, but the named buffers are preserved (see below under ":" Commands.) 6. Change commands: ("changing" means writing new text over a specified range of old text. If the new text does not fully cover the old text, the remaining old text is deleted. If the new text requires more room than the old, the text beyond the range specified is moved back to make room. The specified range starts where the cursor is at the time the change command is entered; its end is indicated on the screen by a '$' superimposed on the last character in the range. The text that has been replaced is moved into a deletion buffer (see "NOTE on BUFFERS", above.)) c, C lower case: waits for movement command to specify extent of change. Commands covering more than one line cause the immediate deletion of the old text, leaving vi in insert mode. Rearward as well as forward movement can be used. Repeat Factor: movement is multiplied by upper case: range is from cursor to end of line. Repeat Factor: range is extended by entire lines following current line. (NOTE: the movement commands "W" and "w" are treated as "E" and "e", so that the change will not include the first character of the word following the range). s, S lower case: range is current character. Repeat Factor: range is characters. upper case: range is entire current line, left end to right. Repeat Factor: range is entire lines. cc same as S 7. In-place modification commands: (These permit existing text or white space to be modified in place. They are not truly insertion commands, though they have the intelligence to insert if "replacement" continues beyond existing text or space.) R reads a string entered from the keyboard, replacing each successive character in the text with the next character in the string. ends replacement. Repeat factor: inserts into the file buffer additional copies of the entered string after is pressed. NOTE: INSERTS, does not extend REPLACEMENT. r waits for a single character from the keyboard, and replaces the character at the cursor with it. Cursor remains on character. Repeat factor: replaces character from the cursor with the new character. ~ if the character at the cursor is a letter, its case (upper or lower) is reversed; no effect if the character is not a letter. Cursor is advanced (letter or not). Repeat factor: NO EFFECT 8. Delete commands: (Eliminate a specified range of text from the file buffer. The range is either implicit in the command or given by a movement command for which the delete command waits. The text is eliminated immediately. Movement commands can be used with repeat factors. Macros (including arrow keys) cannot be used as movement commands. Deleted text is saved in one of a number of buffers. See above under NOTE on BUFFERS. ) d waits for movement command from user, and deletes the range covered by it. Movement command may take own repeat factor. Repeat Factor: movement repeat factor multiplied by this. (NOTE: when the movement commands "W" and "w" are used, everything they cover is deleted EXCEPT the first character of the word to which they move.) dd deletes entire current line. Repeat Factor: deletes entire lines (also allowed: "dd") x, X deletes character under cursor upper case: deletes character to left of cursor. Repeat Factor: effect is multiplied by NOTE: the command sequences "xp" and "Xp" exchange the character at the cursor with the one immediately following (xp) or preceeding (Xp) it. D deletes from current position through end of current line. Repeat Factor: NO EFFECT 9. Commands for Moving and Copying: (The move and copy commands copy text back and forth between the file buffer and any of several deletion buffers. See above, under NOTE on BUFFERS.) y Waits for a movement command, then "yanks" the range of text so specified into the unnamed buffer, or a named buffer if one is given. The text itself is not disturbed. Repeat Factor: the movement command is multiplied by . (NOTE: the movement commands "W" and "w" are modified as for the delete commands.) Y, yy "yanks" the entire current line, as described for "y". Repeat Factor: lines are yanked. p, P Inserts ("puts") a copy of the contents of the unnamed buffer into the text, immediately following the cursor for "p", immediately preceeding it for "P". A named buffer will be used if specified. Repeat Factor: copies of the buffer are inserted. J "Joins" two lines: appends the text of the next line to that of the current line, separating them by one space, and deleting the next line. Repeat Factor: lines, including the current one, are joined. Factors of 1 or 2 give normal Join. 10. Applying Other Programs to File: (vi permits pieces of text to be processed in place by other programs, such as sort, fmt, etc.. ) ! - waits for a movement command; - prompts with an exclamation mark at the bottom line and waits for the entry of a program call. The area from the current position to where the movement goes is given to the program called as standard input; it is deleted and replaced with the program's standard output. Repeat Factor: is applied to the movement. EXAMPLE: the sequence "!}sort -nr " (without the quotes) replaces the text from the cursor to the end of the current paragraph with the same text sorted by leading number, greatest to least. NOTE: the movement command must cover at least a full line. 11. Commands to Repeat Previous Commands: n, N, ",", ";" See movement commands & repeats most recent substitute (":s") command, applying it to current line. . (i.e. period) Repeats effect of most recent insertion, deletion, change, or merge command, at current cursor position, including the repeat factor that was used for it. For repeating deletion or change commands, a different deletion buffer may be specified. See NOTE on BUFFERS, above. Repeat factor: repeat factor of repeated command is multiplied by NOTE: if "." is entered again, the previous repeat factor is the full repeat factor for the previous ".". II. ":" commands 1. (":" commands belong to ex, the line-oriented editor on which vi is built. Pressing ":" causes the cursor to go the bottom of the screen, echoing the colon, to wait for the rest of the command. Some ex commands permit a range of lines to be specified. The syntax is ::= <1st-line-spec>, where the last line given must be no less than the first. Lines may be specified either by their numbers (starting at 1), or by certain notations: . (i.e. period) the current line (where the cursor is). $ the last line. - the 'th line up from the current one. + the 'th line down from the current one. ' the line on which marker has been set. Examples: 1,$ first line to last, therefore the whole file. .,+10 the 11 lines starting with the current one. -22,. the 23 lines ending with the current one. 1,'c from the first line to the one where marker c was set. 'a,'b from the line where marker a was set to the one where marker b was set. ) (NOTE: elements given in []'s are optional) :e Edit another file. - ":e ": re-reads the current file from disc to restart editing; - ":e ": reads and edits the file ; - ":e #": switches editing to file from which last :e command changed; thereafter switches between the two files, retaining the current editing position in each. The unnamed buffer is lost; the named buffers are preserved. If changes in current file have not been saved, requires form ":e!" to edit new file. Changes are saved using :w . :g globally apply a command to lines containing a certain pattern Only known at this writing is "d": delete. - ":g//" applies the to every line in the file containing the . - ":g//" restricts its actions to the . Examples: - to delete all lines containing the pattern "junk", enter :g/junk/d. - to delete all lines from 5 to 10 containing the pattern "procedure", enter :5,10g/procedure/d. :n next file; if vi was executed with a list of files as argument, close the current file and edit the next in the list. Gives advisory note if no next file; requires form ":n!" if changes to current file haven't been saved. Changes are saved using :w . :q quit; requires form ":q!" if changes to current file haven't been saved. Changes are saved using :w . :s substitute one pattern for another in indicated range of lines. - ":s//" substitutes the for the first occurence of the on the current line. - ":s///g" same, but replaces all occurrences on current line. If "s" is preceeded with in these, the effect is applied to all the lines specified. :w write buffer to file. - ":w " : copies the buffer back to the file on disc from which it comes. - ":w " : copies the buffer to the file , creating it if it doesn't yet exist. - ": w " : copies the lines in the buffer specified by the back to the file on disc from which it came. - ": w " : copies the lines in the buffer specified by to the file , creating it if it doesn't yet exist. :! start a new csh command interpreter within vi to execute (a) command(s). - ":! " : creates a new shell and passes it the UNIX command for execution. The new shell has the same capabilitities as the one from which vi was executed, except that on exit, it will return to vi, rather than logging out. NOTE: If the vi variable "autowrite" is set, vi will write the buffer contents back to the file before creating the shell. (see below under "vi variables".) 2. Defining Special Strings: a. Defining abbreviations: (Whenever a word (as detected by the 'w', 'b', and 'e' commands) which is on the current list of abbreviations is entered in insert, change, or replace commands, the string it represents is immediately substituted into place, before the user types the next character of the insertion. Abbreviation recognition is always case sensitive: to be recognised, the abbreviation must be inserted just as it appears in its definition.) :ab Abbreviation: define one string to be an abbreviation for another. - ":ab" : lists all the abbreviations currently defined. - ":ab " : defines the to be an abbreviation of the ; - The must be an alphanumeric string containing no punctuation or delimiters -- these will prevent its being recognised and expanded. - The is any string following the , and may consist of anything that may normally be inserted into text. :unab delete an abbrevation definition. - ":unab " : removes from the list of abbreviations. NOTE: when, in entering the command, is entered, it is replaced with the string it abbreviates; but the correct abbreviation is removed nevertheless. EXAMPLES: - :ab ETC Educational Technology Center whenever ETC is entered in the text as a distinct word, vi immediately replaces it with "Educational Technology Center". - :ab teh the if the user is prone to misspelling "the" as "teh", an abbreviation like this causes vi to correct it immediately upon entry. b. Defining macros: (vi provides two ways for macros to be defined and executed: - using the :map command to assign to a particular key a string of vi commands, and simply pressing that key to execute the macro; - placing the body of the macro in one of the named buffers (a through z), and typing @ to execute it. The advantage of the first way is that the key can be directly defined and executed. The advantage of the second is that it permits the use of the movement command "|", which cannot be used in the :map command, since ex regards it as a delimiter; also, the body of the macro can be kept in the text, making it easier to see, edit, and redefine. The effect of the entire macro can be eliminated with 'undo'. Repeat Factors: has its normal effect on the first command in the macro, and no other effect. ) :map map a key onto a macro, i.e. a string of vi commands. - ":map" : lists all the macros currently defined. - ":map " - the may be any single character, or # to indicate a function key. If it is already a vi command character, its new meaning as a macro takes precedence. - the list of commands is simply a string of commands, one after the other, just as they would be entered from the keyboard. Control characters, such as escape and return, can be entered by preceding them with ^V (ctrl-V). A macro can therefore contain the and characters. Finish the command list (and therefore the map command) by pressing . EXAMPLE: :map #1 1GOThis is the head of the file.^M^[ - defines function key 1 to go to the start of the file (1G), insert a new line at the beginning (O), write a notice in it, return(^M), and finish inserting (^[). :map v $2bhD2$ - defines the letter v to jump to the end of the current line($), move back 2 words (2b), move back a space (h), delete the tail of the line from there (D), then move to the end of the next line down (2$). :unmap eliminates the macro assigned to . 's previous meaning, if it had one, is restored. III. Vi variables (vi variables are either boolean, numeric, or textual. Booleans occur in two forms, and no. If the form is being used, the variable is set; if no, then unset. Numerics simply have a numeric value. Textuals are strings containing such things as file names or lists of delimiters. Several variables (such as "term") exist which cannot be changed from within vi. They are omitted here. ) :set reports and sets vi variable values - ":set" : reports the values of all variables that have been changed from their defaults. - ":set " : if is numeric or textual, reports its value; if is boolean, makes it set (true). - ":set no" : makes boolean unset (false). - ":set =" : sets numeric or textual to - ":set all" : lists all the variables and their values. autowrite: when vi is stopped (by ^Z), or a new shell is started within it, the contents of the buffer are automatically written back to their file. Unset: buffer contents are not written. NOTE: does not affect file-changing commands, such as :e or :n. autoindent: (boolean) when a new line is created, its indentation is that of the line from which it was created. Unset: all new lines start from leftmost column. ignorecase: searches for patterns are done case blind. Unset: searches are done case sensitive. lisp: (boolean) special recognition is turned on for lisp-language structures, including matching parentheses. Unset: no special treatment for lisp. list: (boolean) tab characters are displayed as ^I, rather than being expanded, and line ends are marked with "$"'s. Unset: tab characters are expanded, and line ends are invisible. magic: (boolean) patterns entered as search patterns are treated as regular expressions; several characters are reserved as expression delimiters, and must be backslashed if they are to be part of the pattern. Unset: patterns entered are regarded as literal strings; only "/" is a delimiter. number: (boolean) causes vi to display line numbers to the left of each line. Unset: the display appears entirely screen-oriented. paragraphs: (textual) the string of nroff commands which are to be considered to delimit paragraphs, and for which the "{" and "}" commands will look in addition to completely blank lines. Each successive pair of letters in the string is taken as a command; to be recognised in the text, it must be a word immediately following a period. readonly: (boolean) refuse to write the buffer, altered or not, back to the file from which it was read. "view" sets this automatically. Acts independently of file protection bits. Unset: vi will allow the buffer to be written back to the file. redraw: (boolean) write to the terminal the altered screen display caused by editing actions. Necessary on terminals lacking screen editing features. Unset: no screen re-write is done. It is assumed that terminal editing features have kept the display correct during editing. report: (numeric) the largest number of lines an editing action can affect without having vi report the action on the bottom line. scroll: (numeric) the largest number of lines by which the display window can move forward or backward by scrolling, rather than by having screen re-drawn. Used only if scrolling the relevant direction is possible. shiftwidth: (numeric) the number of columns by which the "<" and ">" commands shift the lines they affect. Also the number of columns ^D moves to the left in insert mode. showmatch: (boolean) every time a closing parenthesis or brace bracket is inserted, the cursor jumps briefly to its mate to show which pair it closes. Unset: no special action for closing parentheses. window: (numeric) the number of lines of text vi believes are displayed. wrapscan: (boolean) pattern searches that fail on encountering one end of the file are restarted at the other end. They fail if the entire file is searched without finding the pattern. Unset: searches fail as soon as they reach an end of the file. wrapmargin: (numeric) the number of columns ahead of the right margin at which, if insertion is in process, vi is to move insertion to the start of the next line ("wrap words"). If set to 0, no word wrapping is done. (NOTE: see "autoindent" for controlling the column where the new line starts.) IV. Initialising vi: To define permanently a set of macros and abbreviations for vi, assign to the UNIX envariable EXINIT a string of :ab and :map commands which define them, separating the commands with the vertical bar "|". Vi reads and interprets EXINIT when it is called. (NOTE: in EXINIT, do NOT place colons ahead of the commands.) EXAMPLE: % setenv EXINIT 'map g 1G|ab yhe the|ab proc PROCEDURE'