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

Options When Starting vi

So far, you have invoked the vi editor with the command:


There are other options to the vi command that can be helpful. You can open a file directly to a specific line number or pattern. You can also open a file in read-only mode. Another option recovers all changes to a file that you were editing when the system crashed.

Advancing to a Specific Place

When you begin editing an existing file, you can call the file in and then move to the first occurrence of a pattern or to a specific line number. You can also specify your first movement by search or by line number right on the command line:[15]

$ vi +n file

Opens file at line number n.

$ vi + file

Opens file at last line.

$ vi +/pattern file

Opens file at the first occurrence of pattern.

In the file practice, to open the file and advance directly to the line containing the word Screen, enter:

vi +/Screen practice
 With a screen editor you can scroll
 the page, move the cursor, delete
 lines, and insert characters, while
 seeing the results of your edits as
 you make them.Screen editors are
 very popular, since they allow you
 to make changes as you read

Give the vi command with the option +/ pattern to go directly to the line containing Screen.

As you see in this example, your search pattern will not necessarily be positioned at the top of the screen. If you include spaces in the pattern, you must enclose the whole pattern within single or double quotes:[16]

+/"you make"

or escape the space with a backslash:

+/you\ make

In addition, if you want to use the general pattern-matching syntax described in Chapter 6, you may need to protect one or more special characters from interpretation by the shell with either single quotes or backslashes.

Using +/pattern is helpful if you have to leave an editing session before you’re finished. You can mark your place by inserting a pattern such as ZZZ or HERE. Then, when you return to the file, all you have to remember is /ZZZ or /HERE.


Normally, when you’re editing in vi, the wrapscan option is enabled. If you’ve customized your environment so that wrapscan is always disabled (see Repeating Searches), you might not be able to use +/pattern. If you try to open a file this way, vi opens the file at the last line and displays the message, “Address search hit BOTTOM without matching pattern.”

Read-Only Mode

There will be times when you want to look at a file but want to protect that file from inadvertent keystrokes and changes. (You might want to call in a lengthy file to practice vi movements, or you might want to scroll through a command file or program.) You can enter a file in read-only mode and use all the vi movement commands, but you won’t be able to change the file.

To look at a file in read-only mode, enter either:

$vi -Rfile



(The view command, like the vi command, can use any of the command-line options for advancing to a specific place in the file.[17]) If you do decide to make some edits to the file, you can override read-only mode by adding an exclamation point to the write command:




If you have a problem writing out the file, see the problem checklists summarized in Appendix C.

Recovering a Buffer

Occasionally a system failure may happen while you are editing a file. Ordinarily, any edits made after your last write (save) are lost. However, there is an option, -r, which lets you recover the edited buffer at the time of a system crash.

On a traditional Unix system with the original vi, when you first log on after the system is running again, you will receive a mail message stating that your buffer has been saved. In addition, if you type the command:

$ex -r


$vi -r

you will get a list of any files that the system has saved.

Use the -r option with a filename to recover the edited buffer. For example, to recover the edited buffer of the file practice after a system crash, enter:

$vi -r practice

It is wise to recover the file immediately, lest you inadvertently make edits to the file and then have to resolve a version skew between the preserved buffer and the newly edited file.

You can force the system to preserve your buffer even when there is not a crash by using the command :pre (short for :preserve). You may find it useful if you have made edits to a file and then discover that you can’t save your edits because you don’t have write permission. (You could also just write out a copy of the file under another name or into a directory where you do have write permission. See Problems Saving Files.)


Recovery may work differently for the various clones and can change from version to version. It is best to check your local documentation. vile does not support any kind of recovery. The vile documentation recommends the use of the autowrite and autosave options. How to do this is described in Customizing vi.

[15] According to the POSIX standard, vi should use -c command instead of +command as shown here. Typically, for backward compatibility, both versions are accepted.

[16] It is the shell that imposes the quoting requirement, not vi.

[17] Typically view is just a link to vi.

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