免费注册 查看新帖 |

Chinaunix

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

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

论坛徽章:
0
11 [报告]
发表于 2008-06-11 13:16 |只看该作者
on the screen, but after you press it, whatever you type will appear
on the screen and will be entered into the buffer. The cursor marks
the current insertion point. To tell vi that you want to stop inserting
text, press ESC. Pressing ESC moves the cursor back one space (so
that it is on the last character you typed) and returns vi to
command mode.

For example, suppose you have opened a new file and want to
insert the word "introduction". If you type the keystrokes
iintroduction, what appears on the screen is:

introduction

When you open a new file, vi starts in command mode and
interprets the first keystroke (i) as the insert command. All
keystrokes made after the insert command are considered text until
you press ESC. If you need to correct a mistake while in insert
mode, backspace and type over the error. Depending on the type of
terminal you are using, backspacing may erase what you've
previously typed or may just back up over it. In either case,
whatever you back up over will be deleted. Note that you can't use
the backspace key to back up beyond the point where you entered
insert mode.

vi has an option that lets you define a right margin and provides a
carriage return automatically when you reach it. For right now,
while you are inserting text, press RETURN to break the lines.

Sometimes you don't know whether you are in insert mode or
command mode. Whenever vi does not respond as you expect,
press ESC once or twice to check which mode you are in. When you
hear the beep, you are in command mode.

2.2 Moving the Cursor
You may spend only a small amount of time in an editing session
adding new text in insert mode; much of the time you will be
making edits to existing text.

In command mode you can position the cursor anywhere in the file.
Since you begin all basic edits (changing, deleting, and copying
text) by placing the cursor at the text that you want to change, you
want to be able to move the cursor to that place as quickly as
possible.

There are vi commands to move the cursor:

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

论坛徽章:
0
13 [报告]
发表于 2008-06-11 13:17 |只看该作者
Before you move the cursor, press ESC to make sure that you are in
command mode. Use h, j, k, and l to move forward or backward in
the file from the current cursor position. When you have gone as far
as possible in one direction, you hear a beep and the cursor stops.
For example, once you're at the beginning or end of a line, you
cannot use h or l to wrap around to the previous or next line; you

[1]
have to use j or k. Similarly, you cannot move the cursor past a
tilde (~) representing a line without text, nor can you move the
cursor above the first line of text.

[1] vim version 4.x, and vim version 5.x with nocompatible set, allow you to "space past" the end of the line
to the next one with l or the spacebar.
2.2.2 Numeric Arguments
You can precede movement commands with numbers. Figure 2.2
shows how the command 4l moves the cursor four spaces to the
right, just as if you had typed l four times (llll).

Figure 2.2. Multiplying commands by numbers


The ability to multiply commands gives you more options and power
for each command you learn. Keep it in mind as you are introduced
to additional commands.

2.2.3 Movement Within a Line
When you saved the file practice, vi displayed a message telling you
how many lines are in that file. A line is not necessarily the same
length as the visible line (often limited to 80 characters) that
appears on the screen. A line is any text entered between newlines.
(A newline character is inserted into the file when you press the
RETURN key in insert mode.) If you type 200 characters before
pressing RETURN, vi regards all 200 characters as a single line
(even though those 200 characters visibly take up several lines on
the screen).

As we mentioned, vi has an option that allows you to set a distance
from the right margin at which vi will automatically insert a newline
character. This option is wrapmargin (its abbreviation is wm). You
can set a wrapmargin at 10 characters:

:set wm=10

论坛徽章:
0
14 [报告]
发表于 2008-06-11 13:17 |只看该作者
This command doesn't affect lines that you've already typed. We'll
talk more about setting options in Chapter 7. (This one really
couldn't wait!)

If you do not use vi's automatic wrapmargin option, you should
break lines with carriage returns to keep the lines of manageable
length.


Two useful commands that involve movement within a line are:

Move to beginning of line.

$

Move to end of line.

In the example below, line numbers are displayed. (Line numbers
can be displayed in vi by using the number option, which is enabled
by typing :setnu in command mode. This operation is described in
Chapter 7.)


The number of logical lines (3) does not correspond to the
number of visible lines (6) that you see on the screen. If the cursor
were positioned on the d in the word delete, and you entered $, the
cursor would move to the period following the word them. If you
entered 0, the cursor would move back to the letter m in the word
move, at the beginning of line two.

论坛徽章:
0
15 [报告]
发表于 2008-06-11 13:18 |只看该作者
2.2.4 Movement by Text Blocks
You can also move the cursor by blocks of text: words,
sentences, paragraphs, etc. The w command moves the cursor
forward one word at a time, counting symbols and punctuation as
equivalent to words. The line below shows cursor movement by w:

cursor, delete lines, insert characters,

You can also move by word, not counting symbols and punctuation,
using the W command. (You can think of this as a "large" or "capital"
Word.)

Cursor movement using W looks like this:

cursor, delete lines, insert characters,

To move backward by word, use the b command. Capital B allows
you to move backward by word, not counting punctuation.

As mentioned previously, movement commands take numeric
arguments; so, with either the w or b commands you can multiply
the movement with numbers. 2w moves forward two words; 5B
moves back five words, not counting punctuation.

We'll discuss movement by sentences and by paragraphs in Chapter

3. For now, practice using the cursor movement commands that you
know, combining them with numeric multipliers.
2.3 Simple Edits
When you enter text in your file, it is rarely perfect. You find typos
or want to improve on a phrase; sometimes your program has a
bug. Once you enter text, you have to be able to change it, delete
it, move it, or copy it. Figure 2.3 shows the kinds of edits you might
want to make to a file. The edits are indicated by proofreading
marks.

Figure 2.3. Proofreading edits

论坛徽章:
0
16 [报告]
发表于 2008-06-11 13:18 |只看该作者
In vi you can perform any of these edits with a few basic
keystrokes: i for insert (which you've already seen); a for append;
c for change; and d for delete. To move or copy text, you use a pair
of commands. You move text with a d for delete, then a p for put;
you copy text with a y for "yank," then a p for put. Each type of edit
is described in this section. Figure 2.4 shows the vi commands you
use to make the edits marked in Figure 2.3.

Figure 2.4. Edits with vi commands


2.3.1 Inserting New Text
You have already seen the insert command used to enter text into a
new file. You also use the insert command while editing existing
text to add missing characters, words, and sentences. In the file
practice, suppose you have the sentence:

论坛徽章:
0
17 [报告]
发表于 2008-06-11 13:19 |只看该作者
with the cursor positioned as shown. To insert With a screen editor
at the beginning of the sentence, enter the following:

Keystrokes Results
2k
Move the cursor up two lines with the k command, to the line
where you want to make the insertion.
iWith a
Press i to enter insert mode and begin inserting text.
screen
editorESC
Finish inserting text, and press ESC to end the insert and return to
command mode.

On the screen shown in the example above, vi pushes existing text
to the right as the new text is inserted. That is because we are
assuming that you are using vi on an "intelligent" terminal that can
rewrite the screen with each character you type. An insert on a
"dumb" terminal (such as an adm3a) will look different. The
terminal itself cannot handle the overhead of updating the screen
for each character typed (without a tremendous sacrifice of speed),
so vi doesn't rewrite the screen until after you press ESC. On a
dumb terminal, the same insert would appear:

Keystrokes Result
iWith a
Press i to enter insert mode and begin inserting text. The dumb
terminal appears to overwrite the existing text on the line.
screen
editor
The insertion appears to have overwritten existing text.
ESC

论坛徽章:
0
18 [报告]
发表于 2008-06-11 13:20 |只看该作者
After you have finished inserting text, press ESC to end the insert and
return to command mode. vi now rewrites the line, so that you see all
existing text.

2.3.2 Appending Text
You can append text at any place in your file with the append
command a. a works in almost the same way as i, except that text
is inserted after the cursor rather than before the cursor. You may
have noticed that when you press i to enter insert mode, the cursor
doesn't move until after you enter some text. On the other hand,
when you press a to enter insert mode, the cursor moves one space
to the right. When you enter text, it appears after the original
cursor position.

2.3.3 Changing Text
You can replace any text in your file with the change command,

c. In order to tell c how much text to change, you combine c with a
movement command. In this way, a movement command serves as
a text object for the c command to affect. For example, c can be
used to change text from the cursor:
cw

to the end of a word.

c2b

back two words

c$

to the end of line

c0

to the beginning of line.

After issuing a change command, you can replace the identified text
with any amount of new text, with no characters at all, with one
word, or with hundreds of lines. c, like i and a, leaves you in insert
mode until you press the ESC key.

论坛徽章:
0
19 [报告]
发表于 2008-06-11 13:20 |只看该作者
When the change only affects the current line, vi marks the end of
the text that will be changed with a $, so that you can see what part
of the line is affected. (See the example for cw, below.)

2.3.3.1 Words
To change a word, combine the c (change) command with w
for word. You can replace a word (cw) with a longer or shorter word
(or any amount of text). cw can be thought of as "delete the word
marked and insert new text until ESC is pressed."

Suppose you have the following line in your file practice:

With an editor you can scroll the page,

and want to change an to a screen. You need to change only one
word:

Keystrokes Results
w
Move with w to the place you want the edit to begin.
cw
Give the change word command. The end of the text to be changed will
be marked with a $ (dollar sign).
a
screen Type in the replacement text, and then press ESC to return to
command mode.

cw also works on a portion of a word. For example, to change
spelling to spelled, you can position the cursor on the i, type cw,
then type ed, and finish with ESC.

General Form of vi Commands

In the change commands we've mentioned up to this point,
you may have noticed the following pattern:
(command)(text object)
command is the change command c, and text object is
a

论坛徽章:
0
20 [报告]
发表于 2008-06-11 13:21 |只看该作者
movement command (you don't type the parentheses). But c
is not the only command that requires a text object. The d
command (delete) and the y command (yank) follow this
pattern as well.

Remember also that movement commands take numeric
arguments, so numbers can be added to the text objects of
c, d, and y commands. For example, d2w and 2dw are
commands to delete two words. With this in mind, you can
see that most vi commands follow a general pattern:

(command)(number)(text object)

or the equivalent form:

(number)(command)(text object)

Here's how this works. number and command are optional.
Without them, you simply have a movement command. If
you add a number, you have a multiple movement. On the
other hand, combine a command (c, d, or y) with a text
object to get an editing command.

When you realize how many combinations are possible in this
way, vi becomes a powerful editor indeed!

2.3.3.2 Lines
To replace the entire current line, there is the special change
command, cc. cc changes an entire line, replacing that line with
any amount of text entered before pressing ESC. It doesn't matter
where the cursor is located on the line; cc replaces the entire line of
text.

A command like cw works differently from a command like cc. In
using cw, the old text remains until you type over it, and any old
text that is left over (up to the $) goes away when you press ESC.
In using cc, though, the old text is wiped out first, leaving you a
blank line on which to insert text.

The "type over" approach happens with any change command that
affects less than a whole line, whereas the "blank line" approach
happens with any change command that affects one or more lines.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP