免费注册 查看新帖 |

Chinaunix

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

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

论坛徽章:
0
31 [报告]
发表于 2008-06-11 13:39 |只看该作者
You've also inserted text after the cursor with the a command.
There are other insert commands for inserting text at different
positions relative to the cursor:

A

Append text to end of current line.

Insert text at beginning of line.

o

Open blank line below cursor for text.

O

Open blank line above cursor for text.

s

Delete character at cursor and substitute text.

S

Delete line and substitute text.

R

Overstrike existing characters with new characters.

All of these commands place you in insert mode. After inserting
text, remember to press ESC to escape back to command mode.

A (append) and I (insert) save you from having to move your
cursor to the end or beginning of the line before invoking insert
mode. (The A command saves one keystroke over $a. Although one
keystroke might not seem like much of a saving, the more adept鈥

论坛徽章:
0
32 [报告]
发表于 2008-06-11 13:40 |只看该作者
as cc. One of the best uses for s is to change one character to
several characters.

R ("large" replace) is useful when you want to start changing text,
but you don't know exactly how much. For example, instead of
guessing whether to say 3cw or 4cw, just type R and then enter your
replacement text.

2.4.1 Numeric Arguments for Insert Commands
Except for o and O, the above insert commands (plus i and a) take
numeric prefixes. With numeric prefixes, you might use the
commands i, I, a, and A to insert a row of underlines or alternating
characters. For example, typing 50i*ESC inserts 50 asterisks, and
typing 25a*- ESC appends 50 characters (25 pairs of asterisk and
hyphen). It's better to repeat only a small string of characters.[4]

[4] Very old versions of vi have difficulty repeating the insertion of more than one line's worth of text.
With a numeric prefix, r replaces that many characters with a
repeated instance of a single character. For example, in C or C++
code, to change || to &&, you would place the cursor on the first
pipe character, and type 2r&.

You can use a numeric prefix with S to substitute several lines. It's
quicker and more flexible, though, to use c with a movement
command.

A good case for using the s command with a numeric prefix is when
you want to change a few characters in the middle of a word.
Typing r wouldn't be correct, but typing cw would change too much
text. Using s with a numeric prefix is usually the same as typing R.

There are other combinations of commands that work naturally
together. For example, ea is useful for appending new text to the
end of a word. It helps to train yourself to recognize such frequent
combinations so that they become automatic.

2.5 Joining Two Lines with J
Sometimes while editing a file you will end up with a series of
short lines that are difficult to scan. When you want to merge two
lines into one, position the cursor anywhere on the first line, and
press J to join the two lines.

Suppose your file practice reads:

论坛徽章:
0
33 [报告]
发表于 2008-06-11 13:40 |只看该作者
Keystrokes Results
J
J joins the line the cursor is on with the line below.
.
Repeat the last command (J) with the . to join the next line with the
current line.
Using a numeric argument with J joins that number of consecutive
lines. In the example above, you could have joined three lines by
using the command 3J.

2.5.1 Problem Checklist

论坛徽章:
0
34 [报告]
发表于 2008-06-11 13:41 |只看该作者
Table 2.1. Edit Commands
Text Object Change Delete Copy
1 word cw dw yw
2 words, not counting punctuation 2cW or c2W 2dW or d2W 2yW or y2W
3 words back 3cb or c3b 3db or d3b 3yb or y3b
1 line cc dd yy or Y
To end of line c$ or C d$ or D y$
To beginning of line c0 d0 y0
Single character r x or X yl or yh
Five characters 5s 5x 5yl
Table 2.2. Movement
Movement Commands
, , , h, j, k, l
To first character of next line +
To first character of previous line -
To end of word e or E
Forward by word w or W
Backward by word b or B
To end of line $
To beginning of line 0
Table 2.3. Other Operations
Operations Commands
Place text from buffer P or p
Start vi, open file if specified vi file
Save edits, quit file ZZ
No saving of edits, quit file :q!
Table 2.4. Text Creation and Manipulation Commands
Editing Action Command
Insert text at current position i
Insert text at beginning of line I
Append text at current position a
Append text at beginning of line A
Open new line below cursor for new text o
Open new line above cursor for new text O
Delete line and substitute text S
Overstrike existing characters with new text R
Join current and next line J
Toggle case ~
Repeat last action .
Undo last change u

论坛徽章:
0
35 [报告]
发表于 2008-06-11 13:41 |只看该作者
Restore line to original state U
You can get by in vi using only the commands listed in these tables.
However, in order to harness the real power of vi (and increase
your own productivity), you will need more tools. The following
chapters describe those tools.

论坛徽章:
0
36 [报告]
发表于 2008-06-11 13:42 |只看该作者
Chapter 3. Moving Around in a Hurry

You will not use vi just to create new files. You'll spend a lot of your
time in vi editing existing files. You rarely want to simply open to
the first line in the file and move through it line by line. You want to
get to a specific place in a file and start work.

All edits begin by moving the cursor to where you want to begin the
edit (or, with ex line editor commands, by identifying the line
numbers to be edited). This chapter shows you how to think about
movement in a variety of ways (by screens, by text, by patterns, or
by line numbers). There are many ways to move in vi, since editing
speed depends on getting to your destination with only a few
keystrokes.

This chapter covers:

论坛徽章:
0
37 [报告]
发表于 2008-06-11 13:43 |只看该作者
3.1.1 Scrolling the Screen
There are vi commands to scroll forward and backward
through the file by full and half screens:

^F

Scroll forward one screen.

^B

Scroll backward one screen.

^D

Scroll forward half screen (down).

^U

Scroll backward half screen (up).

(In the list of commands above, the ^ symbol represents the CTRL
key. ^F means to hold down the CTRL key and press the f key
simultaneously.)

There are also commands to scroll the screen up one line (^E) and
down one line (^Y). However, these two commands do not send the
cursor to the beginning of the line. The cursor remains at the same
point in the line as when the command was issued.

3.1.2 Repositioning the Screen with z
If you want to scroll the screen up or down, but you want the
cursor to remain on the line where you left it, use the z command.

zRETURN Move current line to top of screen and scroll.
z. Move current line to center of screen and scroll.
z-Move current line to bottom of screen and scroll.

With the z command, using a numeric prefix as a multiplier makes
no sense. (After all, you would need to reposition the cursor to the
top of the screen only once. Repeating the same z command
wouldn't move anything.) Instead, z understands a numeric prefix
as a line number that it will use in place of the current line. For

论坛徽章:
0
38 [报告]
发表于 2008-06-11 13:43 |只看该作者
example, z RETURN moves the current line to the top of the screen,
but 200z RETURN moves line 200 to the top of the screen.

3.1.3 Redrawing the Screen
Sometimes while you're editing, messages from your
computer system will display on your screen. These messages don't
become part of your editing buffer, but they do interfere with your
work. When system messages appear on your screen, you need to
redisplay, or redraw, the screen.

Whenever you scroll, you redraw part of (or all of) the screen, so
you can always get rid of unwanted messages by scrolling them off
the screen and then returning to your previous position. But you
can also redraw the screen without scrolling, by typing CTRL-L.

3.1.4 Movement Within a Screen
You can also keep your current screen, or view of the file, and
move around within the screen using:

H

Move to home鈥攖op line on screen.

M

Move to middle line on screen.

Move to last line on screen.

nH

Move to n lines below top line.

nL

Move to n lines above last line.

H moves the cursor from anywhere on the screen to the first, or
"home," line. M moves to the middle line, L to the last. To move to
the line below the first line, use 2H.

Keystrokes Results

论坛徽章:
0
39 [报告]
发表于 2008-06-11 13:44 |只看该作者
L
Move to the last line of the screen with the L command.
2H
Move to the second line of the screen with the 2H command. (H alone
moves to the top line of the screen.)
3.1.5 Movement by Line
Within the current screen there are also commands to
move by line. You've already seen j and k. You can also use:

RETURN Move to first character of next line.
+ Move to first character of next line.
-Move to first character of previous line.

The above three commands move down or up to the first character
of the line, ignoring any spaces or tabs. j and k, by contrast, move
the cursor down or up to the first position of a line, even if that
position is blank (and assuming that the cursor started at the first
position).

3.1.5.1 Movement on the current line
Don't forget that h and l move the cursor to the left and right and
that 0 and $ move the cursor to the beginning or end of the line.
You can also use:

^

Move to first non-blank character of current line.

n|

Move to column n of current line.

论坛徽章:
0
40 [报告]
发表于 2008-06-11 13:44 |只看该作者
As with the line movement commands above, ^ moves to the first
character of the line, ignoring any spaces or tabs. 0, by contrast,
moves to the first position of the line, even if that position is blank.

3.2 Movement by Text Blocks
Another way that you can think of moving through a vi file is by
text blocks鈥攚ords, sentences, paragraphs, or sections.

You have already learned to move forward and backward by word
(w, W, b or B). In addition, you can use these commands:

e

Move to end of word.

E

Move to end of word (ignore punctuation).

(

Move to beginning of current sentence.

)

Move to beginning of next sentence.

{

Move to beginning of current paragraph.

}

Move to beginning of next paragraph.

[[

Move to beginning of current section.

]]

Move to beginning of next section.

To find the end of a sentence, vi looks for one of the punctuation
marks ?.!. vi locates the end of a sentence when the punctuation
is followed by at least two spaces or when it appears as the last
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP