You are previewing Learning the vi and Vim Editors, 7th Edition.

Learning the vi and Vim Editors, 7th Edition

Cover of Learning the vi and Vim Editors, 7th Edition by Arnold Robbins... Published by O'Reilly Media, Inc.
  1. Dedication
  2. Special Upgrade Offer
  3. Preface
    1. Scope of This Book
    2. How the Material Is Presented
      1. Discussion of vi Commands
      2. Conventions
      3. Keystrokes
      4. Problem Checklist
    3. What You Need to Know Before Starting
    4. Comments and Questions
    5. Safari® Books Online
    6. About the Previous Editions
    7. Preface to the Seventh Edition
      1. What’s New
      2. Versions
      3. Acknowledgments from the Sixth Edition
      4. Acknowledgments for the Seventh Edition
  4. I. Basic and Advanced vi
    1. 1. The vi Text Editor
      1. A Brief Historical Perspective
      2. Opening and Closing Files
      3. Quitting Without Saving Edits
    2. 2. Simple Editing
      1. vi Commands
      2. Moving the Cursor
      3. Simple Edits
      4. More Ways to Insert Text
      5. Joining Two Lines with J
      6. Review of Basic vi Commands
    3. 3. Moving Around in a Hurry
      1. Movement by Screens
      2. Movement by Text Blocks
      3. Movement by Searches
      4. Movement by Line Number
      5. Review of vi Motion Commands
    4. 4. Beyond the Basics
      1. More Command Combinations
      2. Options When Starting vi
      3. Making Use of Buffers
      4. Marking Your Place
      5. Other Advanced Edits
      6. Review of vi Buffer and Marking Commands
    5. 5. Introducing the ex Editor
      1. ex Commands
      2. Editing with ex
      3. Saving and Exiting Files
      4. Copying a File into Another File
      5. Editing Multiple Files
    6. 6. Global Replacement
      1. Confirming Substitutions
      2. Context-Sensitive Replacement
      3. Pattern-Matching Rules
      4. Pattern-Matching Examples
      5. A Final Look at Pattern Matching
    7. 7. Advanced Editing
      1. Customizing vi
      2. Executing Unix Commands
      3. Saving Commands
      4. Using ex Scripts
      5. Editing Program Source Code
    8. 8. Introduction to the vi Clones
      1. And These Are My Brothers, Darrell, Darrell, and Darrell
      2. Multiwindow Editing
      3. GUI Interfaces
      4. Extended Regular Expressions
      5. Enhanced Tags
      6. Improved Facilities
      7. Programming Assistance
      8. Editor Comparison Summary
      9. Nothing Like the Original
      10. A Look Ahead
  5. II. Vim
    1. 9. Vim (vi Improved): An Introduction
      1. Overview
      2. Where to Get Vim
      3. Getting Vim for Unix and GNU/Linux
      4. Getting Vim for Windows Environments
      5. Getting Vim for the Macintosh Environment
      6. Other Operating Systems
      7. Aids and Easy Modes for New Users
      8. Summary
    2. 10. Major Vim Improvements over vi
      1. Built-in Help
      2. Startup and Initialization Options
      3. New Motion Commands
      4. Extended Regular Expressions
      5. Customizing the Executable
    3. 11. Multiple Windows in Vim
      1. Initiating Multiwindow Editing
      2. Opening Windows
      3. Moving Around Windows (Getting Your Cursor from Here to There)
      4. Moving Windows Around
      5. Resizing Windows
      6. Buffers and Their Interaction with Windows
      7. Playing Tag with Windows
      8. Tabbed Editing
      9. Closing and Quitting Windows
      10. Summary
    4. 12. Vim Scripts
      1. What’s Your Favorite Color (Scheme)?
      2. Dynamic File Type Configuration Through Scripting
      3. Some Additional Thoughts About Vim Scripting
      4. Resources
    5. 13. Graphical Vim (gvim)
      1. General Introduction to gvim
      2. Customizing Scrollbars, Menus, and Toolbars
      3. gvim in Microsoft Windows
      4. gvim in the X Window System
      5. GUI Options and Command Synopsis
    6. 14. Vim Enhancements for Programmers
      1. Folding and Outlining (Outline Mode)
      2. Auto and Smart Indenting
      3. Keyword and Dictionary Word Completion
      4. Tag Stacking
      5. Syntax Highlighting
      6. Compiling and Checking Errors with Vim
      7. Some Final Thoughts on Vim for Writing Programs
    7. 15. Other Cool Stuff in Vim
      1. Editing Binary Files
      2. Digraphs: Non-ASCII Characters
      3. Editing Files in Other Places
      4. Navigating and Changing Directories
      5. Backups with Vim
      6. HTML Your Text
      7. What’s the Difference?
      8. Undoing Undos
      9. Now, Where Was I?
      10. What’s My Line (Size)?
      11. Abbreviations of Vim Commands and Options
      12. A Few Quickies (Not Necessarily Vim-Specific)
      13. More Resources
  6. III. Other vi Clones
    1. 16. nvi: New vi
      1. Author and History
      2. Important Command-Line Arguments
      3. Online Help and Other Documentation
      4. Initialization
      5. Multiwindow Editing
      6. GUI Interfaces
      7. Extended Regular Expressions
      8. Improvements for Editing
      9. Programming Assistance
      10. Interesting Features
      11. Sources and Supported Operating Systems
    2. 17. Elvis
      1. Author and History
      2. Important Command-Line Arguments
      3. Online Help and Other Documentation
      4. Initialization
      5. Multiwindow Editing
      6. GUI Interfaces
      7. Extended Regular Expressions
      8. Improved Editing Facilities
      9. Programming Assistance
      10. Interesting Features
      11. elvis Futures
      12. Sources and Supported Operating Systems
    3. 18. vile: vi Like Emacs
      1. Authors and History
      2. Important Command-Line Arguments
      3. Online Help and Other Documentation
      4. Initialization
      5. Multiwindow Editing
      6. GUI Interfaces
      7. Extended Regular Expressions
      8. Improved Editing Facilities
      9. Programming Assistance
      10. Interesting Features
      11. Sources and Supported Operating Systems
  7. IV. Appendixes
    1. A. The vi, ex, and Vim Editors
      1. Command-Line Syntax
      2. Review of vi Operations
      3. vi Commands
      4. vi Configuration
      5. ex Basics
      6. Alphabetical Summary of ex Commands
    2. B. Setting Options
      1. Solaris vi Options
      2. nvi 1.79 Options
      3. elvis 2.2 Options
      4. Vim 7.1 Options
      5. vile 9.6 Options
    3. C. Problem Checklists
      1. Problems Opening Files
      2. Problems Saving Files
      3. Problems Getting to Visual Mode
      4. Problems with vi Commands
      5. Problems with Deletions
    4. D. vi and the Internet
      1. Where to Start
      2. vi Web Sites
      3. A Different vi Clone
      4. Amaze Your Friends!
      5. Tastes Great, Less Filling
      6. vi Quotes
  8. Index
  9. About the Authors
  10. Colophon
  11. Special Upgrade Offer
  12. Copyright

Opening and Closing Files

You can use vi to edit any text file. vi copies the file to be edited into a buffer (an area temporarily set aside in memory), displays the buffer (though you can see only one screenful at a time), and lets you add, delete, and change text. When you save your edits, vi copies the edited buffer back into a permanent file, replacing the old file of the same name. Remember that you are always working on acopy of your file in the buffer, and that your edits will not affect your original file until you save the buffer. Saving your edits is also called “writing the buffer,” or more commonly, “writing your file.”

Opening a File

vi is the Unix command that invokes the vi editor for an existing file or for a brand new file. The syntax for the vi command is:

$vi [filename]

The brackets shown on the above command line indicate that the filename is optional. The brackets should not be typed. The $ is the Unix prompt. If the filename is omitted, vi will open an unnamed buffer. You can assign the name when you write the buffer into a file. For right now, though, let’s stick to naming the file on the command line.

A filename must be unique inside its directory. A filename can include any 8-bit character except a slash (/), which is reserved as the separator between files and directories in a pathname, and ASCII NUL, the character with all zero bits. You can even include spaces in a filename by typing a backslash (\) before the space. In practice, though, filenames generally consist of any combination of uppercase and lowercase letters, numbers, and the characters dot (.) and underscore (_). Remember that Unix is case-sensitive: lowercase letters are distinct from uppercase letters. Also remember that you must press ENTER to tell Unix that you are finished issuing your command.

When you want to open a new file in a directory, give a new filename with the vi command. For example, if you want to open a new file called practice in the current directory, you would enter:

$vi practice

Since this is a new file, the buffer is empty and the screen appears as follows:

"practice" [New file]

The tildes (~) down the lefthand column of the screen indicate that there is no text in the file, not even blank lines. The prompt line (also called the status line) at the bottom of the screen echoes the name and status of the file.

You can also edit any existing text file in a directory by specifying its filename. Suppose that there is a Unix file with the pathname /home/john/letter. If you are already in the /home/john directory, use the relative pathname. For example:

$vi letter

brings a copy of the file letter to the screen.

If you are in another directory, give the full pathname to begin editing:

$vi /home/john/letter

Problems Opening Files

  • When you invoke vi , the message [open mode] appears.

    Your terminal type is probably incorrectly identified. Quit the editing session immediately by typing :q. Check the environment variable $TERM. It should be set to the name of your terminal. Or ask your system administrator to provide an adequate terminal type setting.

  • You see one of the following messages:

    Visual needs addressable cursor or upline capability
    Bad termcap entry
    Termcap entry too longterminal:  Unknown terminal type
    Block device required
    Not a typewriter

    Your terminal type is either undefined, or there’s probably something wrong with your terminfo or termcap entry. Enter :q to quit. Check your $TERM environment variable, or ask your system administrator to select a terminal type for your environment.

  • A [new file] message appears when you think a file already exists.

    Check that you have used correct case in the filename (Unix filenames are case-sensitive). If you have, then you are probably in the wrong directory. Enter :q to quit. Then check to see that you are in the correct directory for that file (enter pwd at the Unix prompt). If you are in the right directory, check the list of files in the directory (with ls) to see whether the file exists under a slightly different name.

  • You invoke vi , but you get a colon prompt (indicating that you’re in ex line-editing mode).

    You probably typed an interrupt before vi could draw the screen. Enter vi by typing vi at the ex prompt (:).

  • One of the following messages appears:

    [Read only]
    File is read only
    Permission denied

    “Read only” means that you can only look at the file; you cannot save any changes you make. You may have invoked vi in view mode (with view or vi -R), or you do not have write permission for the file. See the section Problems Saving Files.

  • One of the following messages appears:

    Bad file number
    Block special file
    Character special file
    Non-ascii filefile non-ASCII

    The file you’ve called up to edit is not a regular text file. Type :q! to quit, then check the file you wish to edit, perhaps with the file command.

  • When you type :q because of one of the previously mentioned difficulties, this message appears:

      No write since last change (:quit! overrides).

    You have modified the file without realizing it. Type :q! to leave vi. Your changes from this session will not be saved in the file.

Modus Operandi

As mentioned earlier, the concept of the current “mode” is fundamental to the way vi works. There are two modes, command mode and insert mode. You start out in command mode, where every keystroke represents a command. In insert mode, everything you type becomes text in your file.

Sometimes, you can accidentally enter insert mode, or conversely, leave insert mode accidentally. In either case, what you type will likely affect your files in ways you did not intend.

Press the ESC key to force vi to enter command mode. If you are already in command mode, vi will beep at you when you press the ESC key. (Command mode is thus sometimes referred to as “beep mode.”)

Once you are safely in command mode, you can proceed to repair any accidental changes, and then continue editing your text.

Saving and Quitting a File

You can quit working on a file at any time, save your edits, and return to the Unix prompt. The vi command to quit and save edits is ZZ. Note that ZZ is capitalized.

Let’s assume that you do create a file called practice to practice vi commands, and that you type in six lines of text. To save the file, first check that you are in command mode by pressing ESC, and then enter ZZ.

 "practice" [New file] 6 lines, 320 characters

Give the write and save command, ZZ. Your file is saved as a regular Unix file.

 ch01         ch02         practice

Listing the files in the directory shows the new file practice that you created.

You can also save your edits with ex commands. Type :w to save (write) your file but not quit vi; type :q to quit if you haven’t made any edits; and type :wq to both save your edits and quit. (:wq is equivalent to ZZ.) We’ll explain fully how to use ex commands in Chapter 5; for now, you should just memorize a few commands for writing and saving files.

The best content for your career. Discover unlimited learning on demand for around $1/day.