[ Team LiB ] Previous Section Next Section

1.9 vi Editor

The vi program is a popular text editor on all Unix systems, and Vim is a popular vi clone with expanded regular expression support. Both use a DFA match engine. For an explanation of the rules behind a DFA engine, see Section 1.2.

1.9.1 Supported Metacharacters

Table 1-36 through Table 1-40 list the metacharacters and metasequences supported by vi. For expanded definitions of each metacharacter, see Section 1.2.1.

Table 1-36. Character representation

Sequence

Meaning

Vim only

 

\b

Backspace, x08.

\e

ESC character, x1B.

\n

Newline, x0A.

\r

Carriage return, x0D.

\t

Horizontal tab, x09.

Table 1-37. Character classes and class-like constructs

Class

Meaning

[...]

Any character listed or contained within a listed range.

[^...]

Any character that is not listed or contained within a listed range.

[:class:]

POSIX-style character class valid only within a character class.

.

Any character except newline (unless /s mode).

Vim only

 

\w

Word character, [a-zA-z0-9_].

\W

Non-word character, [^a-zA-z0-9_].

\a

Letter character, [a-zA-z].

\A

Non-letter character, [^a-zA-z].

\h

Head of word character, [a-zA-z_].

\H

Not the head of a word character, [^a-zA-z_].

\d

Digit character, [0-9].

\D

Non-digit character, [^0-9].

\s

Whitespace character, [ \t].

\S

Non-whitespace character, [^ \t].

\x

Hex digit, [a-fA-F0-9].

\X

Non-hex digit, [^a-fA-F0-9].

\o

Octal digit, [0-7].

\O

Non-octal digit, [^0-7].

\l

Lowercase letter, [a-z].

\L

Non-lowercase letter, [^a-z].

\u

Uppercase letter, [A-Z].

\U

Non-uppercase letter, [^A-Z].

\i

Lowercase letter, [a-z].

\L

Non-lowercase letter, [^a-z].

\u

Uppercase letter, [A-Z].

\U

Non-uppercase letter, [^A-Z].

\i

Identifier character defined by isident.

\I

Any non-digit identifier character.

\k

Keyword character defined by iskeyword, often set by language modes.

\K

Any non-digit keyword character.

\f

Filename character defined by isfname. Operating system dependent.

\F

Any non-digit filename character.

\p

Printable character defined by isprint, usually x20-x7E.

\P

Any non-digit printable character.

Table 1-38. Anchors and zero-width tests

Sequence

Meaning

^

Start of a line when appearing first in a regular expression; otherwise, it matches itself.

$

End of a line when appearing last in a regular expression; otherwise, it matches itself.

\<

Beginning of word boundary, (i.e., a position between a punctuation or space character and a word character).

\>

End of word boundary.

Table 1-39. Mode modifiers

Modifier

Meaning

:set ic

Turns on case-insensitive mode for all searching and substitution.

:set noic

Turns off case-insensitive mode.

\u

Force next character in a replacement string to uppercase.

\l

Force next character in a replacement string to lowercase.

\U

Force all following characters in a replacement string to uppercase.

\L

Force all following characters in a replacement string to lowercase.

\E or \e

Ends a span started with \U or \L.

Table 1-40. Grouping, capturing, conditional, and control

Sequence

Meaning

\(...\)

Group subpattern and capture submatch into \1,\2,...

\n

Contains the results of the nth earlier submatch. Valid in both a regex pattern or a replacement string.

&

Evaluates to the matched text when used in a replacement string.

*

Match 0 or more times.

Vim only

 

\+

Match 1 or more times.

\=

Match 1 or 0 times.

\{n}

Match exactly n times.

\{n,}

Match at least n times.

\{,n}

Match at most n times.

\{x,y}

Match at least x times, but no more than y times.

1.9.2 Pattern Matching

Searching

/pattern
?pattern

Moves to the start of the next position in the file matched by pattern. A ?pattern searches backwards. A search can be repeated with the n (search forward) or N (search backwards) commands.

Substitution

:[addr1[,addr2]]s/pattern/replacement/[cgp]

Replace the text matched by pattern with replacement on every line in the address range. If no address range is given, the current line is used. Each address may be either a line number or a regular expression. If addr1 is supplied, substitution will begin on that line number (or the first matching line) and continue until either the end of the file or the line indicated (or matched) by addr2. There are also a number of address shortcuts, which are described in the following tables.

Substitution options

Option

Meaning

c

Prompt before each substitution.

g

Replace all matches on a line.

p

Print line after substitution.

Address shortcuts

Address

Meaning

.

Current line.

$

Last line in file.

%

Entire file.

't

Position "t".

/...[/]

Next line matched by pattern.

?...[?]

Next previous line matched by pattern.

\/

Next line matched by the last search.

\?

Next previous line matched by the last search.

\&

Next line where the last substitution pattern matched.

1.9.3 Examples

Example 1-23. Simple search in vi
Find spider-man, Spider-Man, Spider Man
/[Ss]pider[- ][Mm]an
Example 1-24. Simple search in Vim
Find spider-man, Spider-Man, Spider Man, spiderman, SPIDER-MAN, etc.
:set ic
/spider[- ]\=man
Example 1-25. Simple substitution in vi
Globally convert <br> to <br /> for XHTML compliance.
:set ic
: % s/<br>/<br \/>/
Example 1-26. Simple substitution in Vim
Globally convert <br> to <br /> for XHTML compliance.
: % s/<br>/<br \/>/i
Example 1-27. Harder substitution in Vim
Urlify: Turn URLs into HTML links
: % s/\(https\=:\/\/[a-z_.\\w\/\\#~:?+=&;%@!-]*\)/< a href="\1">\1<\/a>/ic

1.9.4 Other Resources

  • Learning the vi Editor, by Linda Lamb and Arnold Robbins (O'Reilly), is a guide to the vi editor and popular vi clones.

  • http://www.geocities.com/volontir/, by Oleg Raisky, is an overview of Vim regular expression syntax.

    [ Team LiB ] Previous Section Next Section