You are previewing vi and Vim Editors Pocket Reference, 2nd Edition.

vi and Vim Editors Pocket Reference, 2nd Edition

Cover of vi and Vim Editors Pocket Reference, 2nd Edition by Arnold Robbins Published by O'Reilly Media, Inc.
  1. vi and Vim Editors Pocket Reference
  2. 1. vi and Vim Editors Pocket Reference
    1. Introduction
    2. Conventions
    3. Acknowledgments
    4. Command-Line Options
    5. vi Commands
      1. Movement Commands
      2. Editing Commands
      3. Exit Commands
      4. Solaris vi Command-Mode Tag Commands
      5. Buffer Names
      6. Buffer and Marking Commands
    6. Input Mode Shortcuts
      1. Word Abbreviation
      2. Command and Input Mode Maps
      3. Executable Buffers
      4. Automatic Indentation
    7. Substitution and Regular Expressions
      1. The Substitute Command
      2. vi Regular Expressions
      3. POSIX Bracket Expressions
      4. Metacharacters Used in Replacement Strings
      5. More Substitution Tricks
    8. ex Commands
      1. Command Syntax
      2. Address Symbols
      3. Command Option Symbols
      4. Alphabetical List of Commands
    9. Initialization
    10. Recovery
    11. vi set Options
    12. Nothing like the Original
    13. Enhanced Tags and Tag Stacks
      1. Exuberant ctags
      2. Solaris vi Tag Stacking
    14. Vim—vi Improved
      1. Important Command-Line Options
      2. Vim Window Management
      3. Tabbed Editing
      4. Vim Extended Regular Expressions
      5. Command-Line History and Completion
      6. Tag Stacks
      7. Edit-Compile Speedup
      8. Programming Assistance
      9. Folding and Unfolding Text
      10. Insertion Completion Facilities
      11. Diff Mode
      12. Vim Scripting
      13. Vim set Options
    15. nvi—New vi
      1. Important Command-Line Options
      2. nvi Window Management Commands
      3. nvi Extended Regular Expressions
      4. Command-Line History and Completion Options
      5. Tag Stacks
      6. nvi 1.79 set Options
    16. elvis
      1. Important Command-Line Options
      2. elvis Window Management
      3. elvis Extended Regular Expressions
      4. Command-Line History and Completion Movement Keys
      5. Tag Stacks
      6. Edit-Compile Speedup
      7. elvis 2.2 set Options
    17. vile—vi like Emacs
      1. Important Command-Line Options
      2. vile Window Management Commands
      3. vile Extended Regular Expressions
      4. Command-Line History and Completion
      5. Tag Stacks
      6. Edit-Compile Speedup
      7. vile 9.8 set Options
    18. Internet Resources for vi
    19. Program Source and Contact Information
  3. Index
  4. About the Author
  5. Copyright
O'Reilly logo

Substitution and Regular Expressions

Regular expressions, and their use with the substitute command, are what give vi most of its significant editing power.

The Substitute Command

The general form of the substitute command is:

:[addr1[,addr2]]s/old/new/[flags]

Omitting the search pattern (:s//replacement/) uses the last search or substitution regular expression.

An empty replacement part (:s/pattern//) “replaces” the matched text with nothing, effectively deleting it from the line.

Substitution flags

Flag

Meaning

c

Confirm each substitution

g

Change all occurrences of old to new on each line (globally)

p

Print the line after the change is made

It’s often useful to combine the substitute command with the ex global command, :g:

:g/Object Oriented/s//Buzzword compliant/g

vi Regular Expressions

.

(period) Matches any single character except a newline. Remember that spaces are treated as characters.

*

Matches zero or more (as many as there are) of the single character that immediately precedes it.

The * can follow a metacharacter, such as ., or a range in brackets.

^

When used at the start of a regular expression, ^ requires that the following regular expression be found at the beginning of the line. When not at the beginning of a regular expression, ^ stands for itself.

$

When used at the end of a regular expression, $ requires that the preceding regular expression be found at the end of the line. When not at the end of a regular expression, $ stands for itself.

\

Treats the following special character ...

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