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

Chapter 1. The vi Text Editor

Unix[4] has a number of editors that can process the contents of text files, whether those files contain data, source code, or sentences. There are line editors, such as ed and ex, which display a line of the file on the screen; and there are screen editors, such as vi and Emacs, which display a part of the file on your terminal screen. Text editors based on the X Window System are also commonly available and are becoming increasing popular. Both GNU Emacs and its derivative, XEmacs, provide multiple X windows; two interesting alternatives are the sam and Acme editors from Bell Labs. Vim also provides an X-based interface.

vi is the most useful standard text editor on your system. (vi is short for visual editor and is pronounced “vee-eye.” This is illustrated graphically in Figure 1-1.) Unlike Emacs, it is available in nearly identical form on every modern Unix system, thus providing a kind of text-editing lingua franca.[5] The same might be said of ed and ex, but screen editors are generally much easier to use. (So much so, in fact, that line editors have generally fallen into disuse.) With a screen editor, you can scroll the page, move the cursor, delete lines, insert characters, and more, 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 through a file, like you would edit a printed copy, only faster.

Correct pronunciation of vi
Figure 1-1. Correct pronunciation of vi

To many beginners, vi looks unintuitive and cumbersome—instead of using special control keys for word processing functions and just letting you type normally, it uses all of the regular keyboard keys for issuing commands. When the keyboard keys are issuing commands, vi is said to be in command mode. You must be in a special insert mode before you can type actual text on the screen. In addition, there seem to be so many commands.

Once you start learning, however, you realize that vi is well designed. You need only a few keystrokes to tell vi to do complex tasks. As you learn vi, you learn shortcuts that transfer more and more of the editing work to the computer—where it belongs.

vi (like any text editor) is not a “what you see is what you get” word processor. If you want to produce formatted documents, you must type in codes that are used by another formatting program to control the appearance of the printed copy. If you want to indent several paragraphs, for instance, you put a code where the indent begins and ends. Formatting codes allow you to experiment with or change the appearance of your printed files, and, in many ways, they give you much more control over the appearance of your documents than a word processor. Unix supports the troff formatting package.[6] The and formatters are popular, commonly available alternatives.[7]

(vi does support some simple formatting mechanisms. For example, you can tell it to automatically wrap when you come to the end of a line, or to automatically indent new lines. In addition, Vim version 7 provides automatic spellchecking.)

As with any skill, the more editing you do, the easier the basics become, and the more you can accomplish. Once you are used to all the powers you have while editing with vi, you may never want to return to any “simpler” editor.

What are the components of editing? First, you want to insert text (a forgotten word or a new or missing sentence), and you want to delete text (a stray character or an entire paragraph). You also need to change letters and words (to correct misspellings or to reflect a change of mind about a term). You might want to move text from one place to another part of your file. And, on occasion, you want to copy text to duplicate it in another part of your file.

Unlike many word processors, vi’s command mode is the initial or “default” mode. Complex, interactive edits can be performed with only a few keystrokes. (And to insert raw text, you simply give any of the several “insert” commands and then type away.)

One or two characters are used for the basicIp commands. For example:




Change word

Using letters as commands, you can edit a file with great speed. You don’t have to memorize banks of function keys or stretch your fingers to reach awkward combinations of keys. You never have to remove your hands from the keyboard, or mess around with multiple levels of menus! Most of the commands can be remembered by the letters that perform them, and nearly all commands follow similar patterns and are related to each other.

In general, vi commands:

  • Are case-sensitive (uppercase and lowercase keystrokes mean different things; I is different from i).

  • Are not shown (or “echoed”) on the screen when you type them.

  • Do not require an ENTER after the command.

There is also a group of commands that echo on the bottom line of the screen. Bottom-line commands are preceded by different symbols. The slash (/) and the question mark (?) begin search commands, and are discussed in Chapter 3. A colon (:) begins all ex commands. ex commands are those used by the ex line editor. The ex editor is available to you when you use vi, because ex is the underlying editor and vi is really just its “visual” mode. ex commands and concepts are discussed fully in Chapter 5, but this chapter introduces you to the ex commands to quit a file without saving edits.

[4] These days, the term “Unix” includes both commercial systems derived from the original Unix code base, and Unix work-alikes whose source code is available. Solaris, AIX, and HP-UX are examples of the former, and GNU/Linux and the various BSD-derived systems are examples of the latter. Unless otherwise noted, everything in this book applies across the board to all those systems.

[5] GNU Emacs has become the universal version of Emacs. The only problem is that it doesn’t come standard with most commercial Unix systems; you must retrieve and install it yourself.

[6] troff is for laser printers and typesetters. Its “twin brother” is nroff, for line printers and terminals. Both accept the same input language. Following common Unix convention, we refer to both with the name troff. Today, anyone using troff uses the GNU version, groff. See for more information.

[7] See and for information on and , respectively.

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