免费注册 查看新帖 |

Chinaunix

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

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

论坛徽章:
0
51 [报告]
发表于 2008-06-11 13:51 |只看该作者
might want to call in a lengthy file to practice vi movements, or you
might want to scroll through a command file or program). You can
enter a file in read-only mode and use all the vi movement
commands, but you won't be able to change the file.

To look at a file in read-only mode, enter either:

$ vi -R file

or:

$ view file

(The view command, like the vi command, can use any of the
command-line options for advancing to a specific place in the file.)[3]
If you do decide to make some edits to the file, you can override
read-only mode by adding an exclamation point to the write
command:

[3] Typically view is just a link to vi.
:w!

or:

:wq!

If you have a problem writing out the file, see the problem
checklists summarized in Appendix D.

4.2.3 Recovering a Buffer
Occasionally there is a system failure while you are editing a file.
Ordinarily, any edits made after your last write (save) are lost.
However, there is an option, -r, which lets you recover the edited
buffer at the time of a system crash.

When you first log on after the system is running again, you will
receive a mail message stating that your buffer has been saved. In
addition, if you type the command:

$ ex -r

or:

$ vi -r

you will get a list of any files that the system has saved.

论坛徽章:
0
52 [报告]
发表于 2008-06-11 13:51 |只看该作者
Use the -r option with a file name to recover the edited buffer. For
example, to recover the edited buffer of the file practice after a
system crash, enter:

$ vi -r practice

It is wise to recover the file immediately, lest you inadvertently
make edits to the file, and then have to resolve a version skew
between the preserved buffer and the newly edited file.

You can force the system to preserve your buffer even when there
is not a crash by using the command :pre. You may find it useful if
you have made edits to a file, then discover that you can't save
your edits because you don't have write permission. (You could also
just write out a copy of the file under another name or into a
directory where you do have write permission. See Section 1.2.1.)


Recovery for the various clones may work
differently, and can change from version to version.
It is best to check your local documentation. vile
does not support any kind of recovery. The vile
documentation recommends the use of the
autowrite and autosave options. How to do this is
described in Section 7.1.

4.3 Making Use of Buffers
You have seen that while you are editing, your last deletion (d or x)
or yank (y) is saved in a buffer (a place in stored memory). You can
access the contents of that buffer and put the saved text back in
your file with the put command (p or P).

The last nine deletions are stored by vi in numbered buffers. You
can access any of these numbered buffers to restore any (or all) of
the last nine deletions. (Small deletions, of only parts of lines, are
not saved in numbered buffers, however. These deletions can only
be recovered by using the p or P command immediately after you've
made the deletion.)

vi also allows you to place yanks (copied text) in buffers identified
by letters. You can fill up to 26 (a-z) buffers with yanked text and
restore that text with a put command at any time in your editing
session.

论坛徽章:
0
53 [报告]
发表于 2008-06-11 13:52 |只看该作者
4.3.1 Recovering Deletions
Being able to delete large blocks of text at a single bound is all very
well and good, but what if you mistakenly delete 53 lines that you
need? There is a way to recover any of your past nine deletions, for
they are saved in numbered buffers. The last delete is saved in
buffer 1, the second-to-last in buffer 2, and so on.

To recover a deletion, type " (double quote), identify the buffered
text by number, then give the put command. To recover your
second-to-last deletion from buffer 2:

"2p

The deletion in buffer 2 is placed after the cursor.

If you're not sure which buffer contains the deletion you want to
restore, you don't have to keep typing "np over and over again. If
you use the repeat command (.) with p after u, it automatically
increments the buffer number. As a result, you can search through
the numbered buffers as follows:

"1pu.u.u

etc.

to put the contents of each succeeding buffer in the file one after
the other. Each time you type u, the restored text is removed; when
you type a dot (.), the contents of the next buffer is restored to
your file. Keep typing u and . until you've recovered the text you're
looking for.

4.3.2 Yanking to Named Buffers
You have seen that you must put (p or P) the contents of the
unnamed buffer before you make any other edit, or the buffer will
be overwritten. You can also use y and d with a set of 26 named
buffers (a-z) which are specifically available for copying and moving
text. If you name a buffer to store the yanked text, you can retrieve
the contents of the named buffer at any time during your editing
session.

To yank into a named buffer, precede the yank command with a
double quote (") and the character for the name of the buffer you
want to load. For example:

"dyy

Yank current line into buffer d.

论坛徽章:
0
54 [报告]
发表于 2008-06-11 13:53 |只看该作者
"a7yy

Yank next seven lines into buffer a.

After loading the named buffers and moving to the new position,
use p or P to put the text back:

"dP

Put the contents of buffer d before cursor.

"ap

Put the contents of buffer a after cursor.

There is no way to put part of a buffer into the text鈥攊t is all or
nothing.

In the next chapter, you'll learn to edit multiple files. Once you
know how to travel between files without leaving vi, you can use
named buffers to selectively transfer text between files.

You can also delete text into named buffers using much the same
procedure:

"a5dd

Delete five lines into buffer a.

If you specify a buffer name with a capital letter, your yanked or
deleted text will be appended to the current contents of that buffer.
This allows you to be selective in what you move or copy. For
example:

"zd)

Delete from cursor to end of current sentence and save in
buffer z.

2)

Move two sentences further on.

"Zy)

Add the next sentence to buffer z. You can continue adding
more text to a named buffer for as long as you like鈥攂ut be
warned: if you once forget, and yank or delete to the buffer
without specifying its name in capitalized form, you'll
overwrite the buffer, losing whatever you had accumulated in
it.

论坛徽章:
0
55 [报告]
发表于 2008-06-11 13:53 |只看该作者
4.4 Marking Your Place
During a vi session, you can mark your place in the file with an
invisible "bookmark," perform edits elsewhere, then return to your
marked place. In command mode:

m x

Marks the current position with x (x can be any letter).

' x

(apostrophe) Moves the cursor to the first character of the
line marked by x.

` x

(backquote) Moves the cursor to the character marked by x.

``

(backquotes) Returns to the exact position of the previous
mark or context after a move.

''

(apostrophes) Returns to the beginning of the line of the
previous mark or context.

Place markers are set only during the current vi
session; they are not stored in the file.
4.5 Other Advanced Edits
There are other advanced edits that you can execute with vi, but to
use them you must first learn a bit more about the ex editor by
reading the next chapter.

4.6 Review of vi Buffer and Marking Commands
Table 4.2 summarizes the command-line options common to all
versions of vi. Table 4.3 and Table 4.4 summarize the buffer and
marking commands.

论坛徽章:
0
56 [报告]
发表于 2008-06-11 13:54 |只看该作者
Table 4.2. Command-Line Options
Option Meaning
+n file Open file at line number n.
+file Open file at last line.
+/pattern
file Open file at first occurrence of pattern.
-c command
file
Run command after opening file; usually a line number or search
(POSIX version of +).
-R Operate in read-only mode (same as using view instead of vi).
-r Recover files after a crash.
Table 4.3. Buffer Names
Buffer
Names Buffer Use
1-9 The last nine deletions, from most to least recent.
a-z
Named buffers for you to use as needed. Uppercase letters append to
the buffer.
Table 4.4. Buffer and Marking Commands
Command Meaning
"bcommand Do command with buffer b.
mx Mark current position with x.
'x Move cursor to first character of line marked by x.
`x Move cursor to character marked by x.
`` Return to exact position of previous mark or context.
'' Return to beginning of the line of previous mark or context.

论坛徽章:
0
57 [报告]
发表于 2008-06-11 13:54 |只看该作者
Chapter 5. Introducing the ex Editor

If this is a handbook on vi, why would we include a chapter on
another editor? ex is not really another editor. vi is the visual mode
of the more general, underlying line editor, ex. Some ex commands
can be useful to you while you are working in vi, since they can
save you a lot of editing time. Most of these commands can be used

[1]
without ever leaving vi.

[1] vile is different from the other clones. Many of the more advanced ex commands simply don't work.
Instead of noting each one, more details are provided in Chapter 12.
You already know how to think of files as a sequence of numbered
lines. ex gives you editing commands with greater mobility and
scope. With ex you can move easily between files and transfer text
from one file to another in a variety of ways. You can quickly edit
blocks of text larger than a single screen. And with global
replacement you can make substitutions throughout a file for a
given pattern.

This chapter introduces ex and its commands. You will learn how to:

论坛徽章:
0
58 [报告]
发表于 2008-06-11 13:55 |只看该作者
Many of the commands we'll see in this chapter
have filename arguments. Although it's possible, it
is usually a very bad idea to have spaces in your
files' names. ex will be confused to no end, and you
will go to more trouble than it's worth trying to get
the filenames to be accepted. Use underscores,
dashes, or periods to separate the components of
your file names, and you'll be much happier.

Before you start off simply memorizing ex commands (or worse,
ignoring them), let's first take some of the mystery out of line
editors. Seeing how ex works when it is invoked directly will help
make sense of the sometimes obscure command syntax.

Open a file that is familiar to you and try a few ex commands. Just
as you can invoke the vi editor on a file, you can invoke the ex line
editor on a file. If you invoke ex, you will see a message about the
total number of lines in the file, and a colon command prompt.

For example:

$ ex practice"practice" 6 lines, 320 characters:

You won't see any lines in the file unless you give an ex command
that causes one or more lines to be displayed.

ex commands consist of a line address (which can simply be a line
number) plus a command; they are finished with a carriage return.
One of the most basic commands is p for print (to the screen). So,
for example, if you type 1p at the prompt, you will see the first line
of the file:

:1p

With a screen editor you can

:

In fact, you can leave off the p, because a line number by itself is
equivalent to a print command for that line. To print more than one
line, you can specify a range of line numbers (for example, 1,3鈥

论坛徽章:
0
59 [报告]
发表于 2008-06-11 13:55 |只看该作者
scroll the page, move the cursor,

delete lines, insert characters, and more,

A command without a line number is assumed to affect the current
line. So, for example, the substitute command (s), which allows you
to substitute one word for another, could be entered like this:

:1

With a screen editor you can

:s/screen/line/

With a line editor you can

Notice that the changed line is reprinted after the command is
issued. You could also make the same change like this:

:1s/screen/line/
With a line editor you can

Even though you will be invoking ex commands from vi and will not
be using them directly, it is worthwhile to spend a few minutes in ex
itself. You will get a feel for how you need to tell the editor which
line (or lines) to work on, as well as which command to execute.

After you have given a few ex commands on your practice file, you
should invoke vi on that same file, so that you can see it in the
more familiar visual mode. The command :vi will get you from ex
to vi.

To invoke an ex command from vi, you must type the special
bottom line character : (colon). Then type the command and press
RETURN to execute it. So, for example, in the ex editor you move to
a line simply by typing the number of the line at the colon prompt.
To move to line 6 of a file using this command from within vi, enter:

:6

Press RETURN.

Following the exercise, we will discuss ex commands only as they
are executed from vi.

5.1.1 Exercise: The ex Editor
At the UNIX prompt, invoke ex editor on a file
called practice: ex practice
A message appears: "practice" 6 lines, 320characters
Go to and print (display) first line: :1
Print (display) lines 1 through 3: :1,3

论坛徽章:
0
60 [报告]
发表于 2008-06-11 13:56 |只看该作者
Substitute screen for line on line 1: :1s/screen/line
Invoke vi editor on file: :vi
Go to first line: :1

5.1.2 Problem Checklist
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP