Web Hosting Forum | Lunarpages

Author Topic: Vi Introduction  (Read 6435 times)

Offline perestrelka

  • Master Jedi
  • *****
  • Posts: 1397
Vi Introduction
« on: May 07, 2005, 12:51:23 PM »

In the beginning, UNIX had only one editor, the line editor "ed." UNIX still has ed, but few people use it. Line-by-line editors really are adequate only for making short, quick corrections to files or programs. A medieval version of UNIX contained a new program called "ex." The most notable new feature of ex was a display editing facility which allowed a user to work with a full screen of text by giving the command vi . The new display editor proved so popular that AT&T UNIX System V included "vi" as a separate program. Vi is included in most operating systems.

This handout explains the basics of vi: creating files, moving the cursor and entering and editing text. After you have read it and practiced for a short time at your console, you should be able to do basic editing with vi. If you wish to use some of vi's more complicated resources, we recommend a close study of the man pages.

You will encounter three different editing modes when you use vi: command, input and last line.

Command Mode

When you call vi with a command line such as

Code: [Select]
darkstar% vi editfile

 you are automatically placed in Command mode. While in Command mode you can move your cursor around on the screen, view different parts of the file, delete text and carry out searches.

Input Mode

To enter text while in vi, you need to be in Input mode. You may enter Input mode by using any of five different commands: insert (i), append (a), open (o), change (c) or replace (r). To return from any Input mode to Command mode, press the escape key.

 The insert command, i , puts vi in Input mode and places text before the current character.

 The append command, a , puts vi in Input mode and places text after the current character. (A places text after the last character on the current line).

 The open commands, o and O , open a blank line within existing text, put the cursor at the beginning of that line and put vi into Input mode. o opens a line below the current line, O opens a line above the current line.

 The replace commands, R and r , overwrite existing text. r will replace exactly one character and return you to Command mode immediately after replacing that single character; R causes all subsequent entries to overwrite existing text until you press the escape key and return to Command mode.

The change commands, C and c , also replace existing text with new text. However, with the c commands, new text need not replace existing text exactly. You may replace a word with several words, a sentence with several sentences, and so on. The C(hange) command will delete the amount of text specified by the number that follows it; it puts vi into Input mode. After you have finished entering text, you can press the escape key to change the old text into the new.

 Whenever you are in Input mode, you can correct text by using the delete and kill keys. Control-W will move you to the beginning of the file you are currently editing. You can edit text on the current line only with these techniques.

Last Line Mode

Last Line mode is used primarily to save text and exit vi, or to exit vi without saving your text into a permanent file.

Moving the Cursor

Vi has a variety of commands designed to simplify the process of editing text. You must be in Command mode in order to use them. Most of the following commands take numerical arguments which precede the command letter. For example, if you want to move the cursor 15 words forward, you can enter the command 15w ; to move the cursor 15 characters forward, enter 15l . If a command does not take a numerical argument of this form, we will note that fact in the following list of cursor commands. If we say nothing, you can assume that the standard forms will work.

By Characters

The space bar moves the cursor forward one character at a time, toward the right side of the screen. The l key and the right-arrow key perform the same function. In each case, the cursor moves to the beginning of the next line after reaching the end of the line. To move backward by characters one character at a time use the command h or the left-arrow key. (Note that the arrow keys don't necessarily work on all terminals, so it might be a good idea to get used to using the h and l keys.)

By Words

To move the cursor to the first letter of the next word enter w . W moves the cursor to the right by blank-delimited words. (Blank-delimited words are the same as words except that they include adjacent punctuation.) B and b move the cursor to the left, word by word.

By Lines

To move the cursor to the beginning of the next line, press the return key; pressing the j and down-arrow key will move the cursor to the character immediately below the character the cursor is on. If there is no character immediately below the cursor, the cursor will move to the end of the line. The up-arrow key and k move the cursor to the line immediately above the one containing the cursor. (Again, the arrow keys don't always work, so j and k are preferable.)

By Sentences

To move the cursor forward to the beginning of the next sentence use the character ) ; to move the cursor forward to the beginning of the next paragraph use } . To reverse the actions, to move the cursor to the beginning of the previous sentence or paragraph, use ( and { .

By Screen

To scroll the screen forward, press Control-D; to scroll the screen backward press Control-U. Vi also allows you to move through your file by larger units than a screen. To go to a specific line number enter a line number followed by G (goto).

Deleting Text

The various delete commands which follow operate only in Command mode. If you are in Input mode and you want to delete text up to the beginning of the line you are presently working on, you may do so by either backspacing over the text you have just entered, or by using your delete key (if any). However, if you delete text while in Input mode, you will not be able to undo such deletes: the text you have removed stays removed.

Deleting Characters

To delete only the current character, use the command x . To delete several consecutive characters, beginning with the current character, precede x with a repeat factor (any whole number). The d command deletes text from the file. How much text it deletes depends upon the repeat factor which precedes d and the unit of measure which follows the command. In most cases, the unit of measure duplicates the commands for moving the cursor. Thus, to delete to the end of a word, you should enter the command dw (combining "delete" with "go to the next word"). Entering db will delete to the beginning of a word.

 These patterns are consistent for words, blank delimited words, sentences (d) deletes to the end of the sentence; d( deletes to the beginning of the sentence), and paragraphs ({ and } ), but they do not hold for lines or units larger than paragraphs. To delete to the beginning of a line use the command dO ; enter dd to delete the current line. To delete through the last line on a screen, enter dL ; to delete through the first line on a screen enter dH . To delete through the end of the file, enter dG ; to delete through the beginning of the file enter d1G . All of these commands are summarized in tabular form in the command summary at the end of this section.


With vi, you can search forward or backward through your text. To find the next occurrence of a string, press the slash key / and then enter the text you want to find. As you enter the string, it will be displayed on the status line at the bottom of the screen. After you have finished entering the text, press the return key, and vi will search for the string. Once it finds the string it will position the cursor on the first character of the string. Substituting a question mark ? for the slash will cause vi to search for the previous occurrence of the string. To repeat the search exactly, simply enter n ; N will repeat the search in the opposite direction from the original search. If the search reaches the end of the file without finding the string, normally it will "wrap around" --- search the text at the beginning of the file which precedes the cursor position. During a backward search, vi will wrap around to the end of the file.

Saving Text and Exiting vi

You may enter Last Line mode by typing a colon,: , while in Command mode. Note that unlike those of Input and Command modes, commands issued while in Last Line mode are not complete until you press the return key.

 To exit the working file without saving it into a permanent file, enter the command line: q! Either of two commands will save your working file into a permanent file. If you have named your file when you began your editing session with the command line

Code: [Select]
darkstar% vi myfilename

 you may save it in Command mode by entering ZZ . If you have not named your file, but simply called up vi, you will receive the message "no file". To save the text you have been working with into a permanent file, enter Last Line mode and give the file a name with the command: w filename . After you have given the file a name, you can exit the file and return to the shell by entering the command: q .

On occasion you may lose text you were editing in vi because of a system crash or connection lost. You can often reclaim such text if you enter the command line

Code: [Select]
darkstar% vi -r filename

 In most cases, this command will restore the file you have been working on.

vi Command Summary: Calling vi

    vi filename: edit filename starting at line 1
        vi +nfilename: edit filename starting at line number n
        vi +filename: edit filename starting at the last line
        vi -rfilename: recover filename after a system crash
Moving the Cursor

    space key, l, right-arrow key: one space to the right
        h, left-arrow key: one space to the left
        w: one word to the right
        W: one blank-delimited word to the right
        b: one word to the left
        B: one blank-delimited word to the right
        O: beginning of line
        j , down-arrow: down one line
        k, up arrow: up one line
        ): end of sentence
        (: beginning of sentence
        }: end of paragraph
        {: beginning of paragraph
        Control-F: forward one screen
        Control-B: backward one screen
        nG: to line n
        G: to last line
          Most of the above commands can be preceded by a number to iterate them.

Saving and Exiting

    ZZ: save named file while in Command mode
        :: Last Line mode
        q!: exit without saving
        w filename: give file the name "filename"
        q: save named file[/list:u]

Adding Text

    i: insert text before the cursor
        I: insert text before the first character on the screen
        a: insert text after the cursor
        A: insert text at the end of the line
        o: open a line below the current line
        O: open a line above the current one

        r: replace current character and return to Command mode
        R: overwrite characters, starting with current character

          Except for r all of the above commands put vi into Insert mode. You may return to Command mode at any time by pressing the escape key.
Kind Regards,
Vlad Artamonov


Share |