免费注册 查看新帖 |

Chinaunix

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

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

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

论坛徽章:
0
62 [报告]
发表于 2008-06-11 13:57 |只看该作者
:1,10#

would display the line numbers from line one to line ten.

As described in Chapter 3, you can also use the CTRL-G command
to display the current line number. You can thus identify the line
numbers corresponding to the start and end of a block of text by
moving to the start of the block, typing CTRL-G, then moving to the
end of the block and typing CTRL-G again.

Yet another way to identify line numbers is with the ex = command:

:=

Print the total number of lines.

:.=

Print the line number of the current line.

:/ pattern/=

Print the line number of the first line that matches pattern.

5.2.3 Line Addressing Symbols
You can also use symbols for line addresses. A dot (.) stands for
the current line; $ stands for the last line of the file. % stands for
every line in the file; it's the same as the combination 1,$. These
symbols can also be combined with absolute line addresses. For
example:

:.,$d

Delete from current line to end of file.

:20,.m$

Move from line 20 through the current line to the end of the
file.

:%d

Delete all the lines in a file.

:%t$

论坛徽章:
0
63 [报告]
发表于 2008-06-11 13:58 |只看该作者
Copy all lines and place them at the end of the file (making a
consecutive duplicate).

In addition to an absolute line address, you can specify an address
relative to the current line. The symbols + and -work like
arithmetic operators. When placed before a number, these symbols
add or subtract the value that follows. For example:

:.,.+20d

Delete from current line through the next 20 lines.

:226,$m.-2

Move lines 226 through the end of the file to two lines above
the current line.

:.,+20#

Display line numbers from the current line to 20 lines further
on in the file.

In fact, you don't need to type the dot (.) when you use + or -,
because the current line is the assumed starting position.

Without a number following them, + and -are equivalent to +1 and
-1, respectively.[2] Similarly, ++ and --each extend the range by an
additional line, and so on. The + and - can also be used with search
patterns, as shown in the next section.

[2] In a relative address, you shouldn't separate the plus or minus symbol from the number that follows it.
For example, +10 means "10 lines following," but +10 means "11 lines following (1+10)," which is probably
not what you mean (or want).
The number 0 stands for the top of the file (imaginary line 0). 0 is
equivalent to 1-, and both allow you to move or copy lines to the
very start of a file, before the first line of existing text. For example:

:-,+t0

Copy three lines (the line above the cursor through the line
below the cursor) and put them at the top of the file.

5.2.4 Search Patterns
Another way that ex can address lines is by using search patterns.
For example:

论坛徽章:
0
64 [报告]
发表于 2008-06-11 13:58 |只看该作者
:/ pattern/d

Delete the next line containing pattern.

: /pattern/+d

Delete the line below the next line containing pattern. (You
could also use +1 instead of + alone.)

: /pattern1/,/ pattern2/d

Delete from the first line containing pattern1 through the first
line containing pattern2.

:.,/ pattern/m23

Take the text from the current line (.) through the first line
containing pattern and put it after line 23.

Note that patterns are delimited by a slash both before and after.

If you make deletions by pattern with vi and ex, there is a
difference in the way the two editors operate. Suppose your file
practice contains the lines:


Keystrokes Results
d/while
The vi delete to pattern command deletes from the cursor up to the
word while, but leaves the remainder of both lines.
:.,/while/d
The ex command deletes the entire range of addressed lines; in this
case both the current line and the line containing the pattern. All
lines are deleted in their entirety.

5.2.5 Redefining the Current Line Position
Sometimes, using a relative line address in a command can give
you unexpected results. For example, suppose the cursor is on line

论坛徽章:
0
65 [报告]
发表于 2008-06-11 13:59 |只看该作者
1, and you want to print line 100 plus the five lines below it. If you
type:

:100,+5 p

you'll get an error message saying, "First address exceeds second."
The reason the command fails is that the second address is
calculated relative to the current cursor position (line 1), so your
command is really saying this:

:100,6 p

What you need is some way to tell the command to think of line 100
as the "current line," even though the cursor is on line 1.

ex provides such a way. When you use a semicolon instead of a
comma, the first line address is recalculated as the current line. For
example, the command:

:100;+5 p

prints the desired lines. The +5 is now calculated relative to line

100. A semicolon is useful with search patterns as well as absolute
addresses. For example, to print the next line containing pattern,
plus the 10 lines that follow it, enter the command:
:/pattern/;+10 p

5.2.6 Global Searches
You already know how to use / (slash) in vi to search for patterns of
characters in your files. ex has a global command, g, that lets you
search for a pattern and display all lines containing the pattern
when it finds them. The command :g! does the opposite of :g. Use
:g! (or its synonym :v) to search for all lines that do not contain
pattern.

You can use the global command on all lines in the file, or you can
use line addresses to limit a global search to specified lines or to a
range of lines.

:g/ pattern

Finds (moves to) the last occurrence of pattern in the file.

:g/ pattern/p

Finds and displays all lines in the file containing pattern.

论坛徽章:
0
66 [报告]
发表于 2008-06-11 14:00 |只看该作者
:g!/ pattern/nu

Finds and displays all lines in the file that don't contain
pattern; also displays the line number for each line found.

:60,124g/ pattern/p

Finds and displays any lines between lines 60 and 124
containing pattern.

As you might expect, g can also be used for global replacements.
We'll talk about that in Chapter 6.

5.2.7 Combining ex Commands
You don't always need to type a colon to begin a new ex command.
In ex, the vertical bar (|) is a command separator, allowing you to
combine multiple commands from the same ex prompt (in much the
same way that a semicolon separates multiple commands at the
UNIX shell prompt). When you use the |, keep track of the line
addresses you specify. If one command affects the order of lines in
the file, the next command does its work using the new line
positions. For example:

:1,3d | s/thier/their/

Delete lines 1 through 3 (leaving you now on the top line of
the file); then make a substitution on the current line (which
was line 4 before you invoked the ex prompt).

:1,5 m 10 | g/pattern/nu

Move lines 1 through 5 after line 10, and then display all lines
(with numbers) containing pattern.

Note the use of spaces to make the commands easier to read.

5.3 Saving and Exiting Files
You have learned the vi command ZZ to quit and write (save) your
file. But you will frequently want to exit a file using ex commands,
because these commands give you greater control. We've already
mentioned some of these commands in passing. Now let's take a
more formal look.

:w

论坛徽章:
0
67 [报告]
发表于 2008-06-11 14:00 |只看该作者
Writes (saves) the buffer to the file but does not exit. You can
(and should) use :w throughout your editing session to
protect your edits against system failure or a major editing
error.

:q

Quits the editor (and returns to the UNIX prompt).

:wq

Both writes the file and quits the editor. The write happens
unconditionally, even if the file was not changed.



Both writes the file and quits (exits) the editor. The file is
written only if it has been modified.[3]

[3] The difference between :wq and is important when editing source code and using make, which
performs actions based upon file modification times.
vi protects existing files and your edits in the buffer. For example, if
you want to write your buffer to an existing file, vi gives you a
warning. Likewise, if you have invoked vi on a file, made edits, and
want to quit without saving the edits, vi gives you an error message
such as:

No write since last change.

These warnings can prevent costly mistakes, but sometimes you
want to proceed with the command anyway. An exclamation point
(!) after your command overrides the warning:

:w!
:q!

:w! can also be used to save edits in a file that was opened in read-
only mode with vi-R or view (assuming you have write permission
for the file).

:q! is an essential editing command that allows you to quit without
affecting the original file, regardless of any changes you made in
this session. The contents of the buffer are discarded.

论坛徽章:
0
68 [报告]
发表于 2008-06-11 14:31 |只看该作者
5.3.1 Renaming the Buffer
You can also use :w to save the entire buffer (the copy of the file
you are editing) under a new filename.

Suppose you have a file practice, which contains 600 lines. You
open the file and make extensive edits. You want to quit but save
both the old version of practice and your new edits for comparison.
To save the edited buffer in a file called practice.new, give the
command:

:w practice.new

Your old version, in the file practice, remains unchanged (provided
that you didn't previously use :w). You can now quit editing the new
version by typing :q.

5.3.2 Saving Part of a File
While editing, you will sometimes want to save just part of your file
as a separate, new file. For example, you might have entered
formatting codes and text that you want to use as a header for
several files.

You can combine ex line addressing with the write command, w, to
save part of a file. For example, if you are in the file practice and
want to save part of practice as the file newfile, you could enter:

:230,$w newfile

Saves from line 230 to end of file in newfile.

:.,600w newfile

Saves from the current line to line 600 in newfile.

5.3.3 Appending to a Saved File
You can use the UNIX redirect and append operator (>>) with w to
append all or part of the contents of the buffer to an existing file.
For example, if you entered:

:1,10w newfile

then:

:340,$w >>newfile

论坛徽章:
0
69 [报告]
发表于 2008-06-11 14:32 |只看该作者
newfile would contain lines 1-10 and from line 340 to the end of the
buffer.

5.4 Copying a File into Another File
Sometimes you want to copy text or data already entered on the
system into the file you are editing. In vi you can read in the
contents of another file with the ex command:

:read filename

or its abbreviation:

:r filename

This command inserts the contents of filename starting on the line
after the cursor position in the file. If you want to specify a line
other than the one the cursor's on, simply type the line number (or
other line address) you want before the read or r command.

Let's suppose you are editing the file practice and want to read in a
file called data from another directory called /home/tim. Position
the cursor one line above the line where you want the new data
inserted, and enter:

:r /home/tim/data

The entire contents of /home/tim/data are read into practice,
beginning below the line with the cursor.

To read in the same file and place it after line 185, you would enter:

:185r /home/tim/data

Here are other ways to read in a file:

r /home/tim/data

Place the read-in file at the end of the current file.

:0r /home/tim/data

Place the read-in file at the very beginning of the current file.

:/ pattern/r /home/tim/data

Place the read-in file in the current file, after the line
containing pattern.

论坛徽章:
0
70 [报告]
发表于 2008-06-11 14:33 |只看该作者
5.5 Editing Multiple Files
ex commands enable you to switch between multiple files. The
advantage to editing multiple files is speed. If you are sharing the
system with other users, it takes time to exit and reenter vi for each
file you want to edit. Staying in the same editing session and
traveling between files is not only faster for access, but you also
save abbreviations and command sequences that you have defined
(see Chapter 7), and you keep yank buffers so that you can copy
text from one file to another.

5.5.1 Invoking vi on Multiple Files
When you first invoke vi, you can name more than one file to edit,
and then use ex commands to travel between the files. For
example:

$ vi file1 file2

edits file1 first. After you have finished editing the first file, the ex
command :w writes (saves) file1 and :n calls in the next file (file2).

Suppose you want to edit two files, practice and note.

Keystrokes Results
vi
practicenote
Open the two files practice and note. The first-named file, practice,
appears on your screen. Perform any edits.
:w
Save the edited file practice with the ex command w. Press RETURN.
:n
Call in the next file, note, with the ex command n. Press RETURN.
Perform any edits.

Save the second file, note, and quit the editing session.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP