Editing and Saving Files with vi

team bbl


vi operates in one of two modes. vi behaves differently, depending on which mode it's in:

  • Normal mode: The characters you type are editing commands that perform operations on your file, such as moving the cursor, deleting a character or a line, etc. There are two types of commands:

    • Keys sequences: Pressing keys issues commands to vi. For instance, pressing x deletes the character after the cursor.

    • Command line: Pressing a semicolon allows you to type commands with arguments. The cursor moves to the bottom of the screen, waiting for you to type a command.

  • Insert mode: When you type keys, the characters are inserted into the file. Typing certain commands, such as i (the insert command), puts vi into Insert mode. Pressing the <Esc> key ends Insert mode, returning vi to normal mode.

vi opens a file in Normal mode. vi changes to insert mode when you enter a command that inserts text into a file. For instance, the <i> key is the insert command. It changes vi to insert mode and allows you to type text into the file. vi stays in insert mode until you press <Esc>, which ends Insert mode and returns to Normal mode. Another command that changes vi to insert mode is <a>, the append command, which allows you to insert text after the cursor.

In Normal mode, keys are commands. Most commands are one key, although some require a two key sequence. Almost all the commands allow you to type a number before the command. The command is repeated the number of times specified by the number. For instance, <dd> is the command to delete a line. <3dd> deletes 3 lines. If you enter no number before the command, the number 1 is assumed.

A small set of vi commands is described in this chapter. This set is sufficient for most editing tasks. However, vi has many abilities not presented here. If you often edit at the command line, you will appreciate learning more of vi's capabilities. See the vi (or vim) documentation on your system for complete documentation.

After adding/editing the file content as described in the remainder of this chapter, save the file by pressing <ZZ> (notice that ZZ is uppercase). You can close a file without saving any changes by typing:

 :q! 

You can save a file without exiting vi with the command :w.

    team bbl



    Spring Into Linux
    Spring Into Linux
    ISBN: 0131853546
    EAN: 2147483647
    Year: 2005
    Pages: 362
    Authors: Janet Valade

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net