Learning the vi and Vim Editors, 7th Edition

Book description

There's nothing that hard-core Unix and Linux users are more fanatical about than their text editor. Editors are the subject of adoration and worship, or of scorn and ridicule, depending upon whether the topic of discussion is your editor or someone else's.

vi has been the standard editor for close to 30 years. Popular on Unix and Linux, it has a growing following on Windows systems, too. Most experienced system administrators cite vi as their tool of choice. And since 1986, this book has been the guide for vi.

However, Unix systems are not what they were 30 years ago, and neither is this book. While retaining all the valuable features of previous editions, the 7th edition of Learning the vi and vim Editors has been expanded to include detailed information on vim, the leading vi clone. vim is the default version of vi on most Linux systems and on Mac OS X, and is available for many other operating systems too.

With this guide, you learn text editing basics and advanced tools for both editors, such as multi-window editing, how to write both interactive macros and scripts to extend the editor, and power tools for programmers -- all in the easy-to-follow style that has made this book a classic.

Learning the vi and vim Editors includes:

A complete introduction to text editing with vi:

  • How to move around vi in a hurry
  • Beyond the basics, such as using buffers
  • vi's global search and replacement
  • Advanced editing, including customizing vi and executing Unix commands


  • How to make full use of vim:
  • Extended text objects and more powerful regular expressions
  • Multi-window editing and powerful vim scripts
  • How to make full use of the GUI version of vim, called gvim
  • vim's enhancements for programmers, such as syntax highlighting, folding and extended tags


  • Coverage of three other popular vi clones -- nvi, elvis, and vile -- is also included. You'll find several valuable appendixes, including an alphabetical quick reference to both vi and ex mode commands for regular vi and for vim, plus an updated appendix on vi and the Internet.

    Learning either vi or vim is required knowledge if you use Linux or Unix, and in either case, reading this book is essential. After reading this book, the choice of editor will be obvious for you too.

    Publisher resources

    View/Submit Errata

    Table of contents

    1. Dedication
    2. 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
    3. I. Basic and Advanced vi
      1. 1. The vi Text Editor
        1. A Brief Historical Perspective
        2. Opening and Closing Files
          1. Opening a File
          2. Problems Opening Files
          3. Modus Operandi
          4. Saving and Quitting a File
        3. Quitting Without Saving Edits
          1. Problems Saving Files
          2. Exercises
      2. 2. Simple Editing
        1. vi Commands
        2. Moving the Cursor
          1. Single Movements
          2. Numeric Arguments
          3. Movement Within a Line
          4. Movement by Text Blocks
        3. Simple Edits
          1. Inserting New Text
          2. Appending Text
          3. Changing Text
            1. Words
            2. Lines
            3. Characters
            4. Substituting text
          4. Changing Case
          5. Deleting Text
            1. Words
            2. Lines
            3. Characters
            4. Problems with deletions
          6. Moving Text
            1. Transposing two letters
          7. Copying Text
          8. Repeating or Undoing Your Last Command
            1. Repeat
            2. Undo
        4. More Ways to Insert Text
          1. Numeric Arguments for Insert Commands
        5. Joining Two Lines with J
          1. Problem Checklist
        6. Review of Basic vi Commands
      3. 3. Moving Around in a Hurry
        1. Movement by Screens
          1. Scrolling the Screen
          2. Repositioning the Screen with z
          3. Redrawing the Screen
          4. Movement Within a Screen
          5. Movement by Line
            1. Movement on the current line
        2. Movement by Text Blocks
        3. Movement by Searches
          1. Repeating Searches
            1. Changing through searching
          2. Current Line Searches
        4. Movement by Line Number
          1. The G (Go To) Command
        5. Review of vi Motion Commands
      4. 4. Beyond the Basics
        1. More Command Combinations
        2. Options When Starting vi
          1. Advancing to a Specific Place
          2. Read-Only Mode
          3. Recovering a Buffer
        3. Making Use of Buffers
          1. Recovering Deletions
          2. Yanking to Named 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
          1. Exercise: The ex Editor
          2. Problem Checklist
        2. Editing with ex
          1. Line Addresses
          2. Defining a Range of Lines
          3. Line Addressing Symbols
          4. Search Patterns
          5. Redefining the Current Line Position
          6. Global Searches
          7. Combining ex Commands
        3. Saving and Exiting Files
          1. Renaming the Buffer
          2. Saving Part of a File
          3. Appending to a Saved File
        4. Copying a File into Another File
        5. Editing Multiple Files
          1. Invoking vi on Multiple Files
          2. Using the Argument List
          3. Calling in New Files
          4. Switching Files from vi
          5. Edits Between Files
      6. 6. Global Replacement
        1. Confirming Substitutions
        2. Context-Sensitive Replacement
        3. Pattern-Matching Rules
          1. Metacharacters Used in Search Patterns
          2. POSIX Bracket Expressions
          3. Metacharacters Used in Replacement Strings
          4. More Substitution Tricks
        4. Pattern-Matching Examples
          1. Search for General Class of Words
          2. Block Move by Patterns
          3. More Examples
        5. A Final Look at Pattern Matching
          1. Deleting an Unknown Block of Text
          2. Switching Items in a Textual Database
          3. Using :g to Repeat a Command
          4. Collecting Lines
      7. 7. Advanced Editing
        1. Customizing vi
          1. The :set Command
          2. The .exrc File
          3. Alternate Environments
          4. Some Useful Options
        2. Executing Unix Commands
          1. Filtering Text Through a Command
            1. Filtering text with ex
            2. Filtering text with vi
        3. Saving Commands
          1. Word Abbreviation
          2. Using the map Command
          3. Protecting Keys from Interpretation by ex
          4. A Complex Mapping Example
          5. More Examples of Mapping Keys
          6. Mapping Keys for Insert Mode
          7. Mapping Function Keys
          8. Mapping Other Special Keys
          9. Mapping Multiple Input Keys
          10. @-Functions
          11. Executing Buffers from ex
        4. Using ex Scripts
          1. Looping in a Shell Script
          2. Here Documents
          3. Sorting Text Blocks: A Sample ex Script
          4. Comments in ex Scripts
          5. Beyond ex
        5. Editing Program Source Code
          1. Indentation Control
          2. A Special Search Command
          3. Using Tags
      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
          1. Exuberant ctags
          2. The New tags Format
          3. Tag Stacks
            1. Solaris vi
            2. Exuberant ctags and Vim
        6. Improved Facilities
          1. Command-Line History and Completion
          2. Arbitrary Length Lines and Binary Data
          3. Infinite Undo
          4. Incremental Searching
          5. Left-Right Scrolling
          6. Visual Mode
          7. Mode Indicators
        7. Programming Assistance
          1. Edit-Compile Speedup
          2. Syntax Highlighting
        8. Editor Comparison Summary
        9. Nothing Like the Original
        10. A Look Ahead
    4. II. Vim
      1. 9. Vim (vi Improved): An Introduction
        1. Overview
          1. Author and History
          2. Why Vim?
          3. Compare and Contrast with vi
          4. Categories of Features
          5. Philosophy
        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
          1. Command-Line Options
          2. Behaviors Associated to Command Name
          3. System and User Configuration Files
          4. Environment Variables
            1. How to set environment variables
            2. Environment variables relevant to Vim
        3. New Motion Commands
          1. Visual Mode Motion
        4. Extended Regular Expressions
        5. Customizing the Executable
      3. 11. Multiple Windows in Vim
        1. Initiating Multiwindow Editing
          1. Multiwindow Initiation from the Command Line (Shell)
          2. Multiwindow Editing Inside Vim
        2. Opening Windows
          1. New Windows
          2. Options During Splits
          3. Conditional Split Commands
          4. Window Command Summary
        3. Moving Around Windows (Getting Your Cursor from Here to There)
        4. Moving Windows Around
          1. Moving Windows (Rotate or Exchange)
          2. Moving Windows and Changing Their Layout
          3. Window Move Commands: Synopsis
        5. Resizing Windows
          1. Window Resize Commands
          2. Window Sizing Options
          3. Resizing Command Synopsis
        6. Buffers and Their Interaction with Windows
          1. Vim’s Special Buffers
          2. Hidden Buffers
          3. Buffer Commands
          4. Buffer Command Synopsis
        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)?
          1. Conditional Execution
            1. Using the strftime() function
          2. Variables
          3. The execute Command
          4. Defining Functions
          5. A Nice Vim Piggybacking Trick
          6. Tuning a Vim Script with Global Variables
          7. Arrays
        2. Dynamic File Type Configuration Through Scripting
          1. Autocommands
          2. Checking Options
          3. Buffer Variables
          4. The exists() Function
          5. Autocommands and Groups
          6. Deleting Autocommands
        3. Some Additional Thoughts About Vim Scripting
          1. A Useful Vim Script Example
          2. More About Variables
          3. Expressions
          4. Extensions
          5. A Few More Comments About autocmd
          6. Internal Functions
        4. Resources
      5. 13. Graphical Vim (gvim)
        1. General Introduction to gvim
          1. Starting gvim
          2. Using the Mouse
          3. Useful Menus
            1. gvim’s Window menu
            2. gvim’s right-click pop-up menu
        2. Customizing Scrollbars, Menus, and Toolbars
          1. Scrollbars
          2. Menus
            1. Basic menu customization
            2. More menu customization
            3. Putting it all together
          3. Toolbars
          4. Tooltips
        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)
          1. The Fold Commands
          2. Manual Folding
          3. Outlining
          4. A Few Words About the Other Fold Methods
        2. Auto and Smart Indenting
          1. Vim autoindent Extensions to vi’s autoindent
          2. smartindent
          3. cindent
            1. The cinkeys option
            2. The cinwords option
            3. The cinoptions option
          4. indentexpr
          5. A Final Word on Indentation
        3. Keyword and Dictionary Word Completion
          1. Insertion Completion Commands
            1. Completing whole lines
            2. Completion by keyword in file
            3. Completion by dictionary
            4. Completion by thesaurus
            5. Completion by keyword in current file and included files
            6. Completion by tag
            7. Completion by filename
            8. Completion by macro and definition names
            9. Completion method with Vim commands
            10. Completion by user functions
            11. Completion by omni function
            12. Completion for spelling correction
            13. Completion with the complete option
          2. Some Final Comments on Vim Autocompletion
        4. Tag Stacking
        5. Syntax Highlighting
          1. Getting Started
          2. Customization
            1. Syntax groups
            2. The colorscheme command
            3. Setting the background option
            4. The highlight command
            5. Overriding syntax files
          3. Rolling Your Own
        6. Compiling and Checking Errors with Vim
          1. More Uses for the Quickfix List Window
        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?
          1. The viminfo Option
          2. The mksession Command
        10. What’s My Line (Size)?
        11. Abbreviations of Vim Commands and Options
        12. A Few Quickies (Not Necessarily Vim-Specific)
        13. More Resources
    5. 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
          1. Command-Line History and Completion
          2. Tag Stacks
          3. Infinite Undo
          4. Arbitrary Length Lines and Binary Data
          5. Incremental Searching
          6. Left-Right Scrolling
        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
          1. The Session File
          2. Initialization Steps
        5. Multiwindow Editing
        6. GUI Interfaces
          1. The Basic Window
          2. Mouse Behavior
          3. The Toolbar
          4. Options
        7. Extended Regular Expressions
        8. Improved Editing Facilities
          1. Command-Line History and Completion
          2. Tag Stacks
          3. Infinite Undo
          4. Arbitrary Length Lines and Binary Data
          5. Left-Right Scrolling
          6. Visual Mode
        9. Programming Assistance
          1. Edit-Compile Speedup
          2. Syntax Highlighting
        10. Interesting Features
          1. Display Modes
          2. Pre- and Post-Operation Control Files
        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
          1. Building xvile
          2. xvile Basic Appearance and Functionality
            1. Scrollbars
            2. Setting the cursor position and mouse motions
            3. Selections
            4. Clipboard
            5. Resources
            6. Adding menus
          3. Building winvile
          4. winvile Basic Appearance and Functionality
        7. Extended Regular Expressions
        8. Improved Editing Facilities
          1. Command-Line History and Completion
          2. Tag Stacks
          3. Infinite Undo
          4. Arbitrary Length Lines and Binary Data
            1. Locale support
            2. File formats
          5. Incremental Searching
          6. Left-Right Scrolling
          7. Visual Mode
        9. Programming Assistance
          1. Edit-Compile Speedup
          2. Syntax Highlighting
        10. Interesting Features
          1. The vile Editing Model
          2. Major Modes
          3. The Procedure Language
          4. Miscellaneous Small Features
        11. Sources and Supported Operating Systems
    6. IV. Appendixes
      1. A. The vi, ex, and Vim Editors
        1. Command-Line Syntax
          1. Command-Line Options
        2. Review of vi Operations
          1. Command Mode
          2. Insert Mode
          3. Syntax of vi Commands
            1. Examples
            2. Visual mode (Vim only)
          4. Status-Line Commands
        3. vi Commands
          1. Movement Commands
            1. Character
            2. Text
            3. Lines
            4. Screens
            5. Searches
            6. Line numbering
            7. Marks
          2. Insert Commands
          3. Edit Commands
            1. Changing and deleting text
            2. Copying and moving
          4. Saving and Exiting
          5. Accessing Multiple Files
          6. Window Commands (Vim)
          7. Interacting with the System
          8. Macros
          9. Miscellaneous Commands
        4. vi Configuration
          1. The :set Command
          2. Example .exrc File
        5. ex Basics
          1. Syntax of ex Commands
          2. Addresses
          3. Address Symbols
          4. Options
        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
          1. The vi Lover’s Home Page
          2. The Vi Pages
          3. vi Powered!
          4. vi for Java Lovers
          5. Online vi Tutorial
        3. A Different vi Clone
        4. Amaze Your Friends!
        5. Tastes Great, Less Filling
        6. vi Quotes
    7. Index
    8. About the Authors
    9. Colophon
    10. Copyright

    Product information

    • Title: Learning the vi and Vim Editors, 7th Edition
    • Author(s): Arnold Robbins, Elbert Hannah, Linda Lamb
    • Release date: July 2008
    • Publisher(s): O'Reilly Media, Inc.
    • ISBN: 9780596529833