免费注册 查看新帖 |

Chinaunix

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

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

论坛徽章:
0
41 [报告]
发表于 2008-06-11 13:45 |只看该作者
non-blank character on a line. If you have left only a single space
following a period, or if the sentence ends with a quotation mark, vi
won't recognize the sentence.

A paragraph is defined as text up to the next blank line, or up to
one of the default paragraph macros (.IP, .PP, .LP, or .QP) from the
troff MS macro package. Similarly, a section is defined as text up to
the next default section macro (.NH, .SH, .H 1, .HU). The macros
that are recognized as paragraph or section separators can be
customized with the :set command, as described in Chapter 7.

Remember that you can combine numbers with movement. For
example, 3) moves ahead three sentences. Also remember that you
can edit using movement commands: d) deletes to the end of the
current sentence, 2y} copies (yanks) two paragraphs ahead.

3.3 Movement by Searches
One of the most useful ways to move around in a large file
quickly is by searching for text, or more properly, a pattern of
characters. Sometimes a search can be performed to find a
misspelled word or to find each occurrence of a variable in a
program.

The search command is the special character / (slash). When you
enter a slash, it appears on the bottom line of the screen; you then
type in the pattern that you want to find: /pattern.

A pattern can be a whole word or any other sequence of characters
(called a "character string"). For example, if you search for the
characters red, you will match red as a whole word, but you'll also
match occurred. If you include a space before or after pattern, the
spaces will be treated as part of the word. As with all bottom-line
commands, press RETURN to finish. vi, like all other UNIX editors,
has a special pattern-matching language that allows you to look for
variable text patterns; for example, any word beginning with a
capital letter, or the word The at the beginning of a line.

We'll talk about this more powerful pattern-matching syntax in
Chapter 6. For right now, think of pattern simply as a word or
phrase.

vi begins the search at the cursor and searches forward, wrapping
around to the start of the file if necessary. The cursor will move to
the first occurrence of the pattern. If there is no match, the
message "Pattern not found" will be shown on the status line.[1]

论坛徽章:
0
42 [报告]
发表于 2008-06-11 13:46 |只看该作者
[1] The exact messages will vary with different vi clones, but their meanings will be the same. In general, we
won't bother noting everywhere that the text of a message may be different; in all cases the information
conveyed will be the same.
Using the file practice, here's how to move the cursor by searches:

Keystrokes Results
/edits
Search for the pattern edits. Press RETURN to enter. The cursor moves
directly to that pattern.
/scr
Search for the pattern scr. Press RETURN to enter. Note that there is no
space after scr.

The search wraps around to the front of the file. Note that you can
give any combination of characters; a search does not have to be
for a complete word.

To search backward, type a ? instead of a /:

?pattern

In both cases, the search wraps around to the beginning or end of
the file, if necessary.

3.3.1 Repeating Searches
The last pattern that you searched for stays available
throughout your editing session. After a search, instead of repeating
your original keystrokes, you can use a command to search again
for the last pattern.

n Repeat search in same direction.
N Repeat search in opposite direction.
/ RETURN Repeat search forward.
?RETURN Repeat search backward.

Since the last pattern stays available, you can search for a pattern,
do some work, and then search again for the same pattern without
retyping it by using n, N, / or ?. The direction of your search (/ is

论坛徽章:
0
43 [报告]
发表于 2008-06-11 13:46 |只看该作者
forward, ? is backward) is displayed at the bottom left of the
screen.[2]

[2] nvi 1.79 does not show the direction for the n and N commands. vim 5.x puts the search text into the
command line too.
To continue with the example above, since the pattern scr is still
available for search, you can:

Keystrokes Results
n
Move to the next instance of the pattern scr (from screen to scroll) with
the n (next) command.
?you
Search backward with ? from the cursor to the first occurrence of you.
You need to press RETURN after typing the pattern.
N
Repeat previous search for you but in the opposite direction (forward).

Sometimes you want to find a word only if it is further ahead; you
don't want the search to wrap around earlier in the file. vi has an
option, wrapscan, that controls whether searches wrap. You can
disable wrapping like this:

:set nowrapscan

When nowrapscan is set and a forward search fails, the status line
displays the message:

Address search hit BOTTOM without matching pattern

When nowrapscan is set and a backward search fails, the message
displays "TOP" instead of "BOTTOM".

This section has given only the barest introduction to searching for
patterns. Chapter 6 will teach you more about pattern matching and
its use in making global changes to a file.

论坛徽章:
0
44 [报告]
发表于 2008-06-11 13:47 |只看该作者
3.3.1.1 Changing through searching
You can combine the / and ? search operators with the commands
that change text, such as c and d. Continuing with the previous
example:

Keystrokes

Results

d?move


Delete from before the cursor up to and through the word move.

Note how the deletion occurs on a character basis, whole lines are
not deleted.

3.3.2 Current Line Searches
There are also miniature versions of the search commands that
operate within the current line. The command fx moves the cursor
to the next instance of the character x (where x stands for any
character). The command tx moves the cursor to the character
before the next instance of x. Semicolons can then be used
repeatedly to "find" your way along.

The in-line search commands are summarized below. None of these
commands will move the cursor to the next line.

fx Find (move cursor to) next occurrence of x in the line, where x stands for any
character.
Fx Find (move cursor to) previous occurrence of x in the line.
tx Find (move cursor to) character before next occurrence of x in the line.
Tx Find (move cursor to) character after previous occurrence of x in the line.
; Repeat previous find command in same direction.
, Repeat previous find command in opposite direction.

With any of these commands, a numeric prefix n will locate the nth
occurrence. Suppose you are editing in practice, on this line:


Keystrokes Results
fo
Find the first occurrence of o in your current line with f.
;

论坛徽章:
0
45 [报告]
发表于 2008-06-11 13:47 |只看该作者
Move to the next occurrence of o with the ; command (find next o).
dfx deletes up to and including the named character x. This
command is useful in deleting or yanking partial lines. You might
need to use dfx instead of dw if there were symbols or punctuation
within the line that made counting words difficult. The t command
works just like f, except that it positions the cursor before the
character searched for. For example, the command ct. could be
used to change text up to the end of a sentence, leaving the period.

3.4 Movement by Line Number
Lines in a file are numbered sequentially, and you can move
through a file by specifying line numbers.

Line numbers are useful for identifying the beginning and end of
large blocks of text you want to edit. Line numbers are also useful
for programmers, since compiler error messages refer to line
numbers. Line numbers are also used by ex commands, which you
will learn in the next chapters.

If you are going to move by line numbers, you must have a way to
identify them. Line numbers can be displayed on the screen using
the :setnu option described in Chapter 7. In vi, you can also
display the current line number on the bottom of the screen.

The command CTRL-G causes the following to be displayed at the
bottom of your screen: the current line number, the total number of
lines in the file, and what percentage of the total the present line
number represents. For example, for the file practice, CTRL-G might
display:

"practice" line 3 of 6 --50%--

CTRL-G is useful either for displaying the line number to use in a
command or for orienting yourself if you have been distracted from
your editing session.

Depending upon the implementation of vi you're using, you may see
additional information, such as what column the cursor is on, and
an indication as to whether or not the file has been modified but not
yet written out. The exact format of the message will vary as well.

论坛徽章:
0
46 [报告]
发表于 2008-06-11 13:48 |只看该作者
3.4.1 The G (Go To) Command
You can use line numbers to move the cursor through a file. The
G (go to) command uses a line number as a numeric argument and
moves directly to that line. For instance, 44G moves the cursor to
the beginning of line 44. G without a line number moves the cursor
to the last line of the file.

Typing two backquotes (` `) returns you to your original position
(the position where you issued the last G command), unless you
have done some edits in the meantime. If you have made an edit,
and then moved the cursor using some command other than G, ` `
will return the cursor to the site of your last edit. If you have issued
a search command (/ or ?), ` ` will return the cursor to its position
when you started the search. A pair of apostrophes (' ') works
much like two backquotes, except that it returns the cursor to the
beginning of the line instead of the exact position on that line where
your cursor had been.

The total number of lines shown with CTRL-G can be used to give
yourself a rough idea of how many lines to move. If you are on line
10 of a 1,000 line file:

"practice" line 10 of 1000 --1%--

and know that you want to begin editing near the end of that file,
you could give an approximation of your destination with 800G.

Movement by line number is a tool that can move you quickly from
place to place through a large file.

3.5 Review of vi Motion Commands
Table 3.1 summarizes the commands covered in this chapter.

Table 3.1. Movement Commands
Movement Command
Scroll forward one screen. ^F
Scroll backward one screen. ^B
Scroll forward half screen. ^D
Scroll backward half screen. ^U
Scroll forward one line. ^E
Scroll backward one line. ^Y
Move current line to top of screen and scroll. z RETURN
Move current line to center of screen and scroll. z.

论坛徽章:
0
47 [报告]
发表于 2008-06-11 13:49 |只看该作者
Move current line to bottom of screen and scroll. z-
Redraw the screen. ^L
Move to home鈥攖op line of screen. H
Move to middle line of screen. M
Move to bottom line of screen. L
Move to first character of next line. RETURN
Move to first character of next line. +
Move to first character of previous line. -
Move to first non-blank character of current line. ^
Move to column n of current line. n|
Move to end of word. e
Move to end of word (ignore punctuation). E
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. ]]
Search forward for pattern. /pattern
Search backward for pattern. ?pattern
Repeat last search. n
Repeat last search in opposite direction. N
Repeat last search forward. /
Repeat last search backward. ?
Move to next occurrence of x in current line. fx
Move to previous occurrence of x in current line. Fx
Move to just before next occurrence of x in current line. tx
Move to just after previous occurrence of x in current line. Tx
Repeat previous find command in same direction. ;
Repeat previous find command in opposite direction. ,
Go to given line n. nG
Go to end of file. G
Return to previous mark or context. ` `
Return to beginning of line containing previous mark. ' '
Show current line (not a movement command). ^G

论坛徽章:
0
48 [报告]
发表于 2008-06-11 13:49 |只看该作者
Chapter 4. Beyond the Basics

You have already been introduced to the basic vi editing commands,
i, a, c, d, and y. This chapter expands on what you already know
about editing. It covers:

论坛徽章:
0
49 [报告]
发表于 2008-06-11 13:50 |只看该作者
4.2 Options When Starting vi
In this handbook, you have invoked the vi editor with the
command:

$ vi file

There are other options to the vi command that can be helpful. You
can open a file directly to a specific line number or pattern. You can
also open a file in read-only mode. Another option recovers all
changes to a file that you were editing when the system crashed.

4.2.1 Advancing to a Specific Place
When you begin editing an existing file, you can call the file in and
then move to the first occurrence of a pattern or to a specific line
number. You can also specify your first movement by search or by
line number right on the command line:[1]

[1] According to the POSIX standard, vi should use -c command instead of +command as shown here. Typically,
for backwards compatibility, both versions are accepted.
$

vi +

n file

Opens file at line number n.

$

vi +

file

Opens file at last line.

$

vi +/

pattern file

Opens file at the first occurrence of pattern.

In the file practice, to open the file and advance directly to the line
containing the word Screen, enter:

Keystrokes Results

论坛徽章:
0
50 [报告]
发表于 2008-06-11 13:50 |只看该作者
vi +/Screen practice
Give the vi command with the option +/pattern to go
directly to the line containing Screen.
As you see in the example above, your search pattern will not
necessarily be positioned at the top of the screen. If you include
spaces in the pattern, you must enclose the whole pattern within
single or double quotes:[2]

[2] It is the shell that imposes the quoting requirement, not vi.
+/"you make"

or escape the space with a backslash:

+/you\ make

In addition, if you want to use the general pattern-matching syntax
described in Chapter 6, you may need to protect one or more
special characters from interpretation by the shell with either single
quotes or backslashes.

Using +/pattern is helpful if you have to leave an editing session in
the middle. You can mark your place by inserting a pattern such as
ZZZ or HERE. Then when you return to the file, all you have to
remember is /ZZZ or /HERE.

Normally, when you're editing in vi, the wrapscanoption is enabled. If you've customized your
environment so that wrapscan is always disabled
(see Section 3.3.1), you might not be able to use
+/pattern. If you try to open a file this way, vi
opens the file at the last line and displays the
message "Address search hit BOTTOM without
matching pattern."

4.2.2 Read-only Mode
There will be times when you want to look at a file but want to
protect that file from inadvertent keystrokes and changes. (You
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP