Last changed: 10 March 1999 vi Help file vi is the screen-oriented interface to the ex line editor. To start editting or create the file ``filename'', type vi filename where filename is the name of the text file to create or edit. You will begin at the start of the first line in the file. vi has two modes: insertion and command. In insertion mode, when you type, the characters are inserted into the file until you press the key. For example, typing 6 spaces puts ``6 spaces'' into the file. In command mode, when you type, the characters are read as editor commands. For example, if you type 6 spaces the cursor moves to right by six characters and changes the character under the cursor to ``paces''. These two examples are merely to illustrate the difference of typing the same sequence of characters in insertion and command mode. To be certain that you are in command mode, press the key a few times; the terminal may beep at you if you are already in command mode. In command mode, you can move around the file, use editor commands (including ones that switch to insertion mode), and invoke ex (line-editor) commands. ``Current character'' means the character under the cursor. ``Current line'' is the line where the cursor is. <> denote special keys. The key may be the key on some keyboards. means hold the key down and press the key; the other control keys are similar. Uppercase and lowercase letters are different commands although they may have similar actions. is the same as the key. A ``screen width'' is the number of lines for the screen or window. Typical values are 24 and 40 lines. Movement -------- All motion commands move the cursor as indicated or as far as allowed in the file. To move the cursor to the left by one column, press h or or or ; to move the cursor to the right by one column, press l or or ; to move the cursor up by one row, press k or ; to move the cursor down by one row, press j or or . To move the cursor to the beginning of the current line, press 0; to move the cursor to the first character of the current line that is not a or , press ^; to move the cursor to the end of the current line, press $; to move the cursor to the first character of the next line that is not a or , press (or ). To move the cursor to the beginning of the top line on the screen, press H; to move the cursor to the beginning of the middle line on the screen, press M; to move the cursor to the beginning of the bottom line on the screen, press L. To scroll forward by one terminal screen width, press ; to scroll backward by one terminal screen width, press . To scroll forward by half the terminal screen width, press ; to scroll backward by half the terminal screen width, press . To move the cursor to the beginning of line in the file, type ; to move the cursor to the beginning of the last line in the file, press G. Insertion: ---------- The following commands switch to insertion mode. Everything you type is part of the insertion except when you press the erase key or the key. To exit insertion mode, press the key. To delete the character to the left of the cursor, press the erase key (typically, or or ). To start inserting before the current character, press i; to start inserting before the beginning of the current line, press I. To start appending after the current character, press a; to start appending after the end of the current line, press A. To start a new line below the current line, press o; to start a new line above the current line, press O. Deletion: --------- To delete the current character, press x; to delete the character to the left of the cursor, press X. To delete the current line, type dd. Searching: ---------- To find the first occurrence forward from the current cursor position of , type / To find the first occurrence backward from the current cursor position of , type ? Searching will continue from the beginning/end of the file when the end/beginning of the file is reached. To repeat a search, type / or ? to search forward or backward; no will search for the last specified. The characters `.', `/', `\', `*', `[', have special meanings in search patterns so they are written as `\.', `\/', `\\', `\*', `\[', when the literal character is needed. Replacement/substitution: ------------------------- To replace the current character, type r and the will replace the current character. The following commands switch to insertion mode. Everything you type is part of the insertion except when you press the erase key or the key. To exit insertion mode, press the key. To delete the character to the left of the cursor, press the erase key (typically, or or ). To change the current character, press s. To change the current line, type cc. Repeat factor: -------------- Most of the commands above can be preceded by a numeric repeat factor; the insert, append, and open commands, for example, do not as repeating these operations makes no sense. For example, to delete the next six (6) characters, type 6x or xxxxxx To change the next two (2) lines, type 2cc type the change, and press to end. Undoing: -------- To undo the changes of the last command press u. Thus, to undo the changes of the undo command, press u. To undo the changes done on the current line, press U. However, if you move from a line that has changed, U will not have any effect on that line. Saving: ------- As with any editor, the changes made are done to a copy. To save the changes and update the file, type :w [Aside: the `:' command invokes ex (as opposed to vi) commands.] Exiting: -------- To exit vi if no changes have been made since the last write of the file, type :q To write changes to the file and exit vi, type either ZZ or :wq or :x To exit without writing changes to the file and you have made changes, type :q! Miscellaneous commands: ----------------------- To join the current line with the next line, press J. For example, if the current and next line are Current line Next line joining them will make the current line Current line Next line Note: a space character is included between the text of the two lines. Also, any space or characters at the beginning of the line are removed. To redraw the screen (removes messages from the system, line noise, etc.), press . To show status information (file name, if modified since last save, line number), press , and the information will appear at the bottom of the screen. The following are useful ex commands that may be useful, but they are not necessary to know. Messages written to the screen by the system (talk requests, arrival of mail), are not written to the buffer, but can be confusing/annoying while editting. To prevent messages (other than important ones like system shutdown messages), type :set nomesg vi does not automatically wrap long lines. This can be a problem if the screen wraps lines. To tell the vi to leave at least columns blank from the right margin, type :set wrapmargin= The default value of is 0. A value of 1 is usually enough. Keeping track of whether you are in insertion mode or command mode can be difficult even for the seasoned vi user. To have a message indicating that you are in insertion mode, type :set showmode and the mode will be indicated in the lower right part of the screen. No mode indicator means that you are in command mode. To execute command , type :! This is useful for running shell commands without exiting vi. Be aware that the effects are not necessarily the same as running the same command outside of vi. Summary of commands ------------------- Movement: <#> h, , , -- moves cursor one column to the left <#> l, , -- moves cursor one column to the right <#> k, -- moves cursor one row up <#> j, , -- moves cursor one row down 0 -- moves cursor to beginning of current line ^ -- moves the cursor to the first character of the current line that is not a or $ -- moves cursor to end of current line H -- moves cursor to beginning of top line of the screen M -- moves cursor to beginning of middle line of the screen L -- moves cursor to beginning of bottom line of the screen <#> -- scrolls forward by one screen length <#> -- scrolls backward by one screen length <#> -- scrolls forward by a half screen length <#> -- scrolls backward by a half screen length G -- move cursor to beginning of line in file G -- move cursor to beginning of last line in file Insertion commands (use to exit): i -- inserts before current cursor position I -- inserts before beginning of current line a -- appends after current cursor position A -- appends after end of current line Starting a new line (use to exit): o -- starts new line below current line O -- starts new line above current line Deletion commands: <#> x -- Deletes character under cursor <#> X -- Deletes character to left of cursor <#> dd -- Deletes current line Search commands: / -- forward search for ? -- backward search for / -- forward search for previously defined ? -- backward search for previously defined Replace commands: r -- replaces current character with next character input Change commands (use to exit): <#> s -- Changes current character <#> cc -- Changes current line Undo commands: u -- undo changes of last command U -- undo all changes on current line Join command: <#> J -- joins current line with next line Redraw the screen: -- redraws the screen Status information: -- displays file name, if modified since last save, and line number of total number of lines at bottom of the screen. Shell escape: :! -- executes command Setting environment variables: :set nomesg -- prevents messages from appearing while editting :set wrapmargin= -- lines are wrapped columns from the right margin :set showmode -- shows mode when in insertion mode Saving: :w -- writes changes to file Quitting: :wq, :x, ZZ -- writes changes to file and exits vi :q -- exits vi (no changes written to file) :q! -- exits vi (no changes written to file) Notes: 1) all commands on the same line are the same operation; 2) <#> indicates that the command may begin with a repetition factor, _e.g._, ``<#> x'' means that ``6x'' is allowed.