免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: projl
打印 上一主题 下一主题

《Learning the vi editor》 [复制链接]

论坛徽章:
0
71 [报告]
发表于 2008-06-11 14:33 |只看该作者
5.5.2 Using the Argument List
ex actually lets you do more than just move to the next file in the
argument list with :n. The :args command (abbreviated :ar) lists
the files named on the command line, with the current file enclosed
in brackets.

Keystrokes Results
vi practicenote
Open the two files practice and note. The first-named file, practice,
appears on your screen.
:args
vi displays the argument list in the status line, with brackets
around the current filename.

The :rewind (:rew) command resets the current file to be the first
file named on the command line. elvis and vim provide a
corresponding :last command to move to the last file on the
command line.

5.5.3 Calling in New Files
You don't have to call in multiple files at the beginning of your
editing session. You can switch to another file at any time with the
ex command :e. If you want to edit another file within vi, you first
need to save your current file (:w), then give the command:

:e filename

Suppose you are editing the file practice and want to edit the file
letter, then return to practice.

Keystrokes Results
:w Save practice with w and press RETURN. practice is saved and remains
on the screen. You can now switch to another file, because your edits
are saved.
:e
letter
Call in the file letter with e and press RETURN. Perform any edits.

论坛徽章:
0
72 [报告]
发表于 2008-06-11 14:34 |只看该作者
vi "remembers" two filenames at a time as the current and alternate
filenames. These can be referred to by the symbols % (current
filename) and # (alternate filename). # is particularly useful with :e,
since it allows you to switch easily back and forth between two files.
In the example given just above, you could return to the first file,
practice, by typing the command :e #. You could also read the file
practice into the current file by typing :r #.

If you have not first saved the current file, vi will not allow you to
switch files with :e or :n unless you tell it imperatively to do so by
adding an exclamation point after the command.

For example, if after making some edits to letter, you wanted to
discard the edits and return to practice, you could type :e! #.

The following command is also useful. It discards your edits and
returns to the last saved version of the current file:

:e!

In contrast to the # symbol, % is useful mainly when writing out the
contents of the current buffer to a new file. For example, a few
pages earlier, in the section "Renaming the Buffer," we showed how
to save a second version of the file practice with the command:

:w practice.new

Since % stands for the current filename, that line could also have
been typed:

:w %.new

5.5.4 Switching Files from vi
Since switching back to the previous file is something that
tends to happen a lot, you don't have to move to the ex command
line to do it. The vi command ^^ (the "control" key with the caret
key) will do this for you. Using this command is the same as typing
:e #. As with the :e command, if the current buffer has not been
saved, vi will not let you switch back to the previous file.

5.5.5 Edits Between Files
When you give a yank buffer a one-letter name, you have a
convenient way to move text from one file to another. Named
buffers are not cleared when a new file is loaded into the vi buffer

论坛徽章:
0
73 [报告]
发表于 2008-06-11 14:34 |只看该作者
with the :e command. Thus, by yanking or deleting text from one
file (into multiple named buffers if necessary), calling in a new file
with :e, and putting the named buffer(s) into the new file, you can
transfer material between files.

The following example illustrates how to transfer text from one file
to another.

Keystrokes Results
"f4yy
Yank four lines into buffer f.
:w
Save the file.
:e
letter
Enter the file letter with :e. Move the cursor to where the copied text
will be placed.
"fp
Place yanked text from named buffer f below the cursor.

Another way to move text from one file to another is to use the ex
commands :ya (yank) and :pu (put). These commands work the
same way as the equivalent vi commands y and p, but they are
used with ex's line-addressing capability and named buffers.

For example:

:160,224ya a

would yank (copy) lines 160 through 224 into buffer a. Next you
would move with :e to the file where you want to put these lines.

论坛徽章:
0
74 [报告]
发表于 2008-06-11 14:35 |只看该作者
Place the cursor on the line where you want to put the yanked lines.
Then type:

:pu
a
to put the contents of buffer a after the current line.

论坛徽章:
0
75 [报告]
发表于 2008-06-11 14:35 |只看该作者
Chapter 6. Global Replacement

Sometimes, halfway through a document or at the end of a draft,
you may recognize inconsistencies in the way that you refer to
certain things. Or, in a manual, some product whose name appears
throughout your file is suddenly renamed (marketing!). Often
enough it happens that you have to go back and change what
you've already written, and you need to make the changes in
several places.

The way to make these changes is with a powerful change
command called global replacement. With one command you can
automatically replace a word (or a string of characters) wherever it
occurs in the file.

In a global replacement, the ex editor checks each line of a file for a
given pattern of characters. On all lines where the pattern is found,
ex replaces the pattern with a new string of characters. For right
now, we'll treat the search pattern as if it were a simple string; later
in the chapter we'll look at the powerful pattern-matching language
known as regular expressions.

Global replacement really uses two ex commands: :g (global) and
:s (substitute). Since the syntax of global replacement commands
can get fairly complex, let's look at it in stages.

The substitute command has the syntax:

:s/old/new/

This changes the first occurrence of the pattern old to new on the
current line. The / (slash) is the delimiter between the various parts
of the command. (The slash is optional when it is the last character
on the line.)

A substitute command with the syntax:

:s/old/new/g

changes every occurrence of old to new on the current line, not just
the first occurrence. The :s command allows options following the
substitution string. The g option in the syntax above stands for
global. (The g option affects each pattern on a line; don't confuse it
with the :g command, which affects each line of a file.)

论坛徽章:
0
76 [报告]
发表于 2008-06-11 14:36 |只看该作者
By prefixing the :s command with addresses, you can extend its
range to more than one line. For example, this line will change
every occurrence of old to new from line 50 to line 100:

:50,100s/old/new/g

This command will change every occurrence of old to new within the
entire file:

:1,$s/old/new/g

You can also use % instead of 1,$ to specify every line in a file. Thus
the last command could also be given like this:

:%s/old/new/g

Global replacement is much faster than finding each instance of a
string and replacing it individually. Because the command can be
used to make many different kinds of changes, and because it is so
powerful, we will first illustrate simple replacements and then build
up to complex, context-sensitive replacements.

6.1 Confirming Substitutions
It makes sense to be overly careful when using a search and
replace command. It sometimes happens that what you get is not
what you expect. You can undo any search and replacement
command by entering u, provided that the command was the most
recent edit you made. But you don't always catch undesired
changes until it is too late to undo them. Another way to protect
your edited file is to save the file with :w before performing a global
replacement. Then at least you can quit the file without saving your
edits and go back to where you were before the change was made.
You can also read the previous version of the buffer back in with
:e!.

It's wise to be cautious and know exactly what is going to be
changed in your file. If you'd like to see what the search turns up
and confirm each replacement before it is made, add the c option
(for confirm) at the end of the substitute command:

:1,30s/his/the/gc

It will display the entire line where the string has been located, and
the string will be marked by a series of carets (^^^^):

copyists at his school

^^^_

论坛徽章:
0
77 [报告]
发表于 2008-06-11 14:37 |只看该作者
If you want to make the replacement, you must enter y (for yes)
and press RETURN. If you don't want to make a change, simply
press RETURN.[1]

[1] elvis 2.0 doesn't support this feature. In the other clones, the actual appearance and prompt differ, but
the effect is still the same, allowing you to choose whether or not to do the substitution in each case.
this can be used for invitations, signs, and menus.
^^^_

The combination of the vi commands n (repeat last search) and dot
(.) (repeat last command) is also an extraordinarily useful and
quick way to page through a file and make repetitive changes that
you may not want to make globally. So, for example, if your editor
has told you that you're using which when you should be using that,
you can spot-check every occurrence of which, changing only those
that are incorrect:

/which Search for which.
cwthat ESC Change to that.
n Repeat search.
n Repeat search, skip a change.
. Repeat change (if appropriate).
.
.
.

6.2 Context-Sensitive Replacement
The simplest global replacements substitute one word (or a phrase)
for another. If you have typed a file with several misspellings
(editer for editor), you can do the global replacement:

:%s/editer/editor/g

This substitutes editor for every occurrence of editer throughout the
file.

There is a second, slightly more complex syntax for global
replacement. This syntax lets you search for a pattern, and then,
once you find the line with the pattern, make a substitution on a
string different from the pattern. You can think of this as context-
sensitive replacement.

The syntax is as follows:

:g/pattern/s/old/new/g

论坛徽章:
0
78 [报告]
发表于 2008-06-11 14:37 |只看该作者
The first g tells the command to operate on all lines of a file. pattern
identifies the lines on which a substitution is to take place. On those
lines containing pattern, ex is to substitute (s) for old the characters
in new. The last g indicates that the substitution is to occur globally
on that line.

For example, in this book, the SGML directives <keycap> and
</keycap> place a box around ESC to show the ESCAPE key. You
want ESC to be all in caps, but you don't want to change any
instances of Escape that might be in the text. To change instances
of Esc to ESC only when Esc is on a line that contains the <keycap>
directive, you could enter:

:g/<keycap>/s/Esc/ESC/g

If the pattern being used to find the line is the same as the one you
want to change, you don't have to repeat it. The command:

:g/string/s//new/g

would search for lines containing string and substitute for that same
string.

Note that:

:g/editer/s//editor/g

has the same effect as:

:%s/editer/editor/g

You can save some typing by using the second form. It is also
possible to combine the :g command with :d, :mo, :co and other ex
commands besides :s. As we'll show, you can thus make global
deletions, moves, and copies.

6.3 Pattern-Matching Rules
In making global replacements, UNIX editors such as vi allow you to
search not just for fixed strings of characters, but also for variable
patterns of words, referred to as regular expressions.

When you specify a literal string of characters, the search might
turn up other occurrences that you didn't want to match. The
problem with searching for words in a file is that a word can be used
in different ways. Regular expressions help you conduct a search for
words in context. Note that regular expressions can be used with

论坛徽章:
0
79 [报告]
发表于 2008-06-11 14:38 |只看该作者
the vi search commands / and ? as well as in the ex :g and :s
commands.

For the most part, the same regular expressions work with other

[2]
UNIX programs such as grep, sed, and awk.

[2] Much more information on regular expressions can be found in the two O'Reilly books sed & awk, by Dale
Dougherty and Arnold Robbins, and Mastering Regular Expressions, by Jeffrey E.F. Friedl.
Regular expressions are made up by combining normal characters

[3]
with a number of special characters called metacharacters. The

metacharacters and their uses are listed below.

[3] Technically speaking, we should probably call these metasequences, since sometimes two characters
together have special meaning, and not just single characters. Nevertheless, the term metacharacters is in
common use in UNIX literature, so we follow that convention here.
6.3.1 Metacharacters Used in Search Patterns
.

Matches any single character except a newline. Remember
that spaces are treated as characters. For example, p.pmatches character strings such as pep, pip, and pcp.

*

Matches zero or more (as many as there are) of the single
character that immediately precedes it. For example, bugs*
will match bugs (one s) or bug (no s's).

The * can follow a metacharacter. For example, since . (dot)
means any character, .* means "match any number of any
character."

Here's a specific example of this. The command
:s/End.*/End/ removes all characters after End (it replaces
the remainder of the line with nothing).

^

When used at the start of a regular expression, requires that
the following regular expression be found at the beginning of
the line; for example, ^Part matches Part when it occurs at
the beginning of a line, and ^... matches the first three
characters of a line. When not at the beginning of a regular
expression, ^ stands for itself.

$

论坛徽章:
0
80 [报告]
发表于 2008-06-11 14:38 |只看该作者
When used at the end of a regular expression, requires that
the preceding regular expression be found at the end of the
line; for example, here matches only when here: occurs at
the end of a line. When not at the end of a regular expression,
$ stands for itself.

\

Treats the following special character as an ordinary
character. For example, \. matches an actual period instead
of "any single character," and \* matches an actual asterisk
instead of "any number of a character." The \ (backslash)
prevents the interpretation of a special character. This
prevention is called "escaping the character." (Use \\ to get a
literal backslash.)

[ ]

Matches any one of the characters enclosed between the
brackets. For example, [AB] matches either A or B, and
p[aeiou]t matches pat, pet, pit, pot, or put. A range of
consecutive characters can be specified by separating the first
and last characters in the range with a hyphen. For example,
[A-Z] will match any uppercase letter from A to Z, and [0-9]
will match any digit from 0 to 9.

You can include more than one range inside brackets, and you
can specify a mix of ranges and separate characters. For
example, [:;A-Za-z()] will match four different punctuation
marks, plus all letters.

Most metacharacters lose their special meaning inside
brackets, so you don't need to escape them if you want to use
them as ordinary characters. Within brackets, the three
metacharacters you still need to escape are \-]. The hyphen
(-) acquires meaning as a range specifier; to use an actual
hyphen, you can also place it as the first character inside the
brackets.

A caret (^) has special meaning only when it is the first
character inside the brackets, but in this case the meaning
differs from that of the normal ^ metacharacter. As the first
character within brackets, a ^ reverses their sense: the
brackets will match any one character not in the list. For
example, [^a-z] matches any character that is not a
lowercase letter.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP