These classnotes are depreciated. As of 2005, I no longer teach the classes. Notes will remain online for legacy purposes

UNIX01/Using The Vi Editor

Classnotes | UNIX01 | RecentChanges | Preferences

The Two Modes of VI

The first thing most users learn about the VI editor is that it has two modes: command and insert. The command mode allows the entry of commands to manipulate text. These commands are usually one or two characters long, and can be entered with few keystrokes. The insert mode puts anything typed on the keyboard into the current file.

VI starts out in command mode. There are several commands that put the VI editor into insert mode. The most commonly used commands to get into insert mode are a and i. These two commands are described below. Once you are in insert mode, you get out of it by hitting the escape key. If your terminal does not have an escape key, ^[ should work (control-[). You can hit escape two times in a row and VI would definitely be in command mode. Hitting escape while you are already in command mode doesn't take the editor out of command mode. It may beep to tell you that you are already in that mode.

How to Type Commands in Command Mode

The command mode commands are normally in this format: (Optional arguments are given in the brackets)

 [count] command [where] 

Most commands are one character long, including those which use control characters. The commands described in this section are those which are used most commonly the VI editor.

The count is entered as a number beginning with any character from 1 to 9. For example, the x command deletes a character under the cursor. If you type 23x while in command mode, it will delete 23 characters.

Some commands use an optional where parameter, where you can specify how many lines or how much of the document the command affects, the where parameter can also be any command that moves the cursor.

Some Simple VI Commands

Here is a simple set of commands to get a beginning VI user started. There are many other convenient commands, which will be discussed in later sections.

  • a : enter insert mode, the characters typed in will be inserted after the current cursor position. If you specify a count, all the text that had been inserted will be repeated that many times.
  • h : move the cursor to the left one character position.
  • i : enter insert mode, the characters typed in will be inserted before the current cursor position. If you specify a count, all the text that had been inserted will be repeated that many times.
  • j : move the cursor down one line.
  • k : move the cursor up one line.
  • l : move the cursor to the right one character position.
  • r : replace one character under the cursor. Specify count to replace a number of characters
  • u : undo the last change to the file. Typing u again will re-do the change.
  • x : delete character under the cursor. Count specifies how many characters to delete. The characters will be deleted after the cursor.

Text Buffers in VI

The VI editor has 36 buffers for storing pieces of text, and also a general purpose buffer. Any time a block of text is deleted or yanked from the file, it gets placed into the general purpose buffer. Most users of VI rarely use the other buffers, and can get along without the other buffers. The block of text is also stored in another buffer as well, if it is specified. The buffer is specified using the " command. After typing ", a letter or digit specifying the buffer must be entered. For example, the command: "mdd uses the buffer m, and the last two characters stand for delete current line. Similarly, text can be pasted in with the p or P command. "mp pastes the contents of buffer m after the current cursor position. For any of the commands used in the next two sections, these buffers can be specified for temporary storage of words or paragraphs.

Cutting and Yanking

The command commonly used command for cutting is d. This command deletes text from the file. The command is preceded by an optional count and followed by a movement specification. If you double the command by typing dd, it deletes the current line. Here are some combinations of these:

  • d^ : deletes from current cursor position to the beginning of the line.
  • d$ : deletes from current cursor position to the end of the line.
  • dw : deletes from current cursor position to the end of the word.
  • 3dd : deletes three lines from current cursor position downwards.

There is also the y command which operates similarly to the d command which take text from the file without deleting the text.

Pasting

The commands to paste are p and P. The only differ in the position relative to the cursor where they paste. p pastes the specified or general buffer after the cursor position, while P pastes the specified or general buffer before the cursor position. Specifying count before the paste command pastes text the specified number of times.

Indenting Your Code and Checking

The VI editor has features to help programmers format their code neatly. There is a variable that to set up the indentation for each level of nesting in code. In order to set it up, see the customization section of this tutorial. For example, the command to set the shift width to 4 characters is :set sw=4.

The following commands indent your lines or remove the indentation, and can be specified with count:

  • << : Shifts the current line to the left by one shift width.
  • >> : Shifts the current line to the right by one shift width.

The VI editor also has a helpful feature which checks your source code for any hanging parentheses or braces. The % command will look for the left parenthesis or brace corresponding to a particular right parenthesis or brace and vice versa. Place the cursor onto a parenthesis or brace and type % to move the cursor to the corresponding parenthesis or brace. This is useful to check for unclosed parentheses or braces. If a parenthesis or brace exists without a matching parenthesis or brace, VI will beep at you to indicate that no matching symbol was found.

Word and Character Searching

The VI editor has two kinds of searches: string and character. For a string search, the / and ? commands are used. When you start these commands, the command just typed will be shown on the bottom line, where you type the particular string to look for. These two commands differ only in the direction where the search takes place. The / command searches forwards (downwards) in the file, while the ? command searches backwards (upwards) in the file. The n and N commands repeat the previous search command in the same or opposite direction, respectively. Some characters have special meanings to VI, so they must be preceded by a backslash (\) to be included as part of the search expression.

  • Special characters:
    • ^ : Beginning of the line. (At the beginning of a search expression.)
    • . : Matches a single character.
    • * : Matches zero or more of the previous character.
    • $ : End of the line (At the end of the search expression.)
    • [ : Starts a set of matching, or non-matching expressions... For example: /f[iae]t matches either of these: fit fat fet In this form, it matches anything except these: /a[^bcd] will not match any of these, but anything with an a and another letter: ab ac ad
    • < : Put in an expression escaped with the backslash to find the ending or beginning of a word. For example: /\<the\> should find only word the, but not words like these: there and other.
    • > : See the '<' character description above.

The character search searches within one line to find a character entered after the command. The f and F commands search for a character on the current line only. f searches forwards and F searches backwards and the cursor moves to the position of the found character.

The t and T commands search for a character on the current line only, but for t, the cursor moves to the position before the character, and T searches the line backwards to the position after the character.

These two sets of commands can be repeated using the ; or , command, where ; repeats the last character search command in the same direction, while , repeats the command in the reverse direction.

Abbreviations and Mapping Keys to Other Keys

One editor command that is useful in the VI editor is the abbreviate command. This lets you set up abbreviations for specific strings. The command looks like this: :ab string thing to substitute for. For example, if you had to type the name, "Humuhumunukunukuapua`a" but you didn't want to type the whole name, you could use an abbreviation for it. For this example, the command is entered like this:

  :ab 9u Humuhumunukunukuapua`a

Now, whenever you type 9u as a separate word, VI will type in the entire word(s) specified. If you typed in 9university, it will not substitute the word.

To remove a previously defined abbreviation, the command is unabbreviate. To remove the previous example, the command would be ":una 9u" To get your listing of abbreviations, simply just type :ab without any definitions.

Another editor command that is useful for customization is the mapping command. There are two kinds of mapping commands. One for command mode, and the other for insert mode. These two commands are :map and :map! respectively. The mapping works similarly to the abbreviation, and you give it a key sequence and give it another key sequence to substitute it with. (The substituted key sequences are usually VI commands.)

Recovering Your Work When Something Goes Wrong with Your Terminal

The VI editor edits a temporary copy of your file, and after the editing is complete, or when you tell it to save, it puts the contents of the temporary copy into the original file. If something goes wrong while you are editing your file, the VI editor will attempt to save whatever work you had in progress, and store it for later recovery. (Note: If VI dies while you were working on any file, it sends you an email message on how to recover it. The -r option stands for recovery. If you were editing the file vitalinfo, and you accidentally got logged out, then the -r option of the 'vi' editor should help. The command would look somewhat like this: vi -r vitalinfo After using the -r option once, though, you MUST save what you have recovered to the actual file... The -r option only works once per failed VI session.

Summary of VI commands

This list is a summary of VI commands, categorized by function. There may be other commands available, so check the on-line manual on VI. For easy reference, you can save this file as text and delete any commands you don't think you would use and print out the resulting shorter file.

Cutting and Pasting/Deleting? text

  • " : Specify a buffer to be used any of the commands using buffers. Follow the " with a letter or a number, which corresponds to a buffer.
  • D : Delete to the end of the line from the current cursor position.
  • P : Paste the specified buffer before the current cursor position or line. If no buffer is specified (with the " command.) then 'P' uses the general buffer.
  • X : Delete the character before the cursor.
  • Y : Yank the current line into the specified buffer. If no buffer is specified, then the general buffer is used.
  • d : Delete until where. "dd" deletes the current line. A count deletes that many lines. Whatever is deleted is placed into the buffer specified with the " command. If no buffer is specified, then the general buffer is used.
  • p : Paste the specified buffer after the current cursor position or line. If no buffer is specified (with the " command.) then 'p' uses the general buffer.
  • x : Delete character under the cursor. A count tells how many characters to delete. The characters will be deleted after the cursor.
  • y : Yank until , putting the result into a buffer. "yy" yanks the current line. a count yanks that many lines. The buffer can be specified with the " command. If no buffer is specified, then the general buffer is used.

Inserting New Text

  • A : Append at the end of the current line.
  • I : Insert from the beginning of a line.
  • O : (letter oh) Enter insert mode in a new line above the current cursor position.
  • a : Enter insert mode, the characters typed in will be inserted after the current cursor position. A count inserts all the text that had been inserted that many times.
  • i : Enter insert mode, the characters typed in will be inserted before the current cursor position. A count inserts all the text that had been inserted that many times.
  • o : Enter insert mode in a new line below the current cursor position.

Moving the Cursor Within the File

  • ^B : Scroll backwards one page. A count scrolls that many pages.
  • ^D : Scroll forwards half a window. A count scrolls that many lines.
  • ^F : Scroll forwards one page. A count scrolls that many pages.
  • ^H : Move the cursor one space to the left. A count moves that many spaces.
  • ^J : Move the cursor down one line in the same column. A count moves that many lines down.
  • ^M : Move to the first character on the next line.
  • ^N : Move the cursor down one line in the same column. A count moves that many lines down.
  • ^P : Move the cursor up one line in the same column. A count moves that many lines up.
  • ^U : Scroll backwards half a window. A count scrolls that many lines.
  • $ : Move the cursor to the end of the current line. A count moves to the end of the following lines.
  • % : Move the cursor to the matching parenthesis or brace.
  • ^ : Move the cursor to the first non-whitespace character.
  • ( : Move the cursor to the beginning of a sentence.
  • ) :Move the cursor to the beginning of the next sentence.
  • { : Move the cursor to the preceding paragraph.
  • } : Move the cursor to the next paragraph.
  • | : Move the cursor to the column specified by the count.
  • B : Move the cursor back one word, skipping over punctuation.
  • E : Move forward to the end of a word, skipping over punctuation.

Replacing Text

  • C : Change to the end of the line from the current cursor position.
  • R : Replace characters on the screen with a set of characters entered, ending with the Escape key.
  • S : Change an entire line.
  • c : Change until . "cc" changes the current line. A count changes that many lines.
  • r : Replace one character under the cursor. Specify a count to replace a number of characters.
  • s : Substitute one character under the cursor, and go into insert mode. Specify a count to substitute a number of characters. A dollar sign ($) will be put at the last character to be substituted.

Searching for Text or Characters

  • , : Repeat the last f, F, t or T command in the reverse direction.
  • / : Search the file downwards for the string specified after the /.
  • ; : Repeat the last f, F, t or T command.
  • ? : Search the file upwards for the string specified after the ?.

Manipulating Character/Line? Formatting

  • ~ : Switch the case of the character under the cursor.
  • < : Shift the lines up to where to the left by one shiftwidth. "<<" shifts the current line to the left, and can be specified with a count.
  • > : Shift the lines up to where to the right by one shiftwidth. ">>" shifts the current line to the right, and can be specified with a count.
  • J : Join the current line with the next one. A count joins that many lines.

Saving and Quitting

  • ZZ : Exit the editor, saving if any changes were made.
  • :w : Write out the current file.
  • :w > filename : Write the buffer to the filename specified.
  • :w >> filename : Append the contents of the buffer to the filename.
  • :wq : Write the buffer and quit.


Classnotes | UNIX01 | RecentChanges | Preferences
This page is read-only | View other revisions
Last edited July 12, 2003 7:45 pm (diff)
Search:
(C) Copyright 2003 Samuel Hart
Creative Commons License
This work is licensed under a Creative Commons License.