免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 46333 | 回复: 300
打印 上一主题 下一主题

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

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-06-11 13:08 |只看该作者 |倒序浏览
Chapter 1. The vi Text Editor
UNIX has a number of editors that can process the contents of text
files, whether those files contain data, source code, or sentences.
There are line editors, such as ed and ex, which display a line of the
file on the screen; and there are screen editors, such as vi and
emacs, which display a part of the file on your terminal screen. Text
editors based on the X Window System are also commonly
available, and are becoming increasing popular. Both GNU emacs
and its derivative xemacs provide multiple X windows; an
interesting alternative is the sam editor from Bell Labs. All but one
of the vi clones described in Part II of this book also provide X-
based interfaces.

vi is the most useful standard text editor on your system. (vi is
short for visual editor and is pronounced "vee-eye.") Unlike emacs,
it is available in nearly identical form on almost every UNIX system,

[1]
thus providing a kind of text-editing lingua franca. The same might
be said of ed and ex, but screen editors are generally much easier
to use. With a screen editor, you can scroll the page, move the
cursor, delete lines, insert characters, and more, while seeing the
results of your edits as you make them. Screen editors are very
popular, since they allow you to make changes as you read through
a file, like you would edit a printed copy, only faster.

[1] Actually, these days, GNU emacs is pretty much the universal version of emacs; the only problem is it
doesn't come standard with most commercial UNIX systems; you must retrieve and install it yourself.
To many beginners, vi looks unintuitive and cumbersome鈥攊nstead
of using special control keys for word processing functions and just
letting you type normally, it uses all of the regular keyboard keys
for issuing commands. When the keyboard keys are issuing
commands, vi is said to be in command mode. You must be in a
special insert mode before you can type actual text on the screen.
In addition, there seem to be so many commands.

Once you start learning, however, you realize that vi is well
designed. You need only a few keystrokes to tell vi to do complex
tasks. As you learn vi, you learn shortcuts that transfer more and
more of the editing work to the computer鈥攚here it belongs.

vi (like any text editor) is not a "what you see is what you get" word
processor. If you want to produce formatted documents, you must
type in codes that are used by another formatting program to
control the appearance of the printed copy. If you want to indent
several paragraphs, for instance, you put a code where the indent
begins and ends. Formatting codes allow you to experiment with or

[ 本帖最后由 projl 于 2008-6-12 16:59 编辑 ]

vi.pdf

1.9 MB, 下载次数: 6437

论坛徽章:
0
2 [报告]
发表于 2008-06-11 13:10 |只看该作者
change the appearance of your printed files, and in many ways,
give you much more control over the appearance of your
documents than a word processor. UNIX supports the troff
formatting package.[2] The and formatters are popular, commonly
available alternatives.

[2] troff is for laser printers and typesetters. Its "twin brother" is nroff, for line printers and terminals. Both
accept the same input language. Following common UNIX convention, we refer to both with the name troff.
(vi does support some simple formatting mechanisms. For example,
you can tell it to automatically wrap when you come to the end of a
line, or to automatically indent new lines.)

As with any skill, the more editing you do, the easier the basics
become, and the more you can accomplish. Once you are used to all
the powers you have while editing with vi, you may never want to
return to any "simpler" editor.

What are the components of editing? First, you want to insert text
(a forgotten word or a missing sentence), and you want to delete
text (a stray character or an entire paragraph). You also need to
change letters and words (to correct misspellings or to reflect a
change of mind about a term). You might want to move text from
one place to another part of your file. And, on occasion, you want to
copy text to duplicate it in another part of your file.

Unlike many word processors, vi's command mode is the initial or
"default" mode. Complex, interactive edits can be performed with
only a few keystrokes. (And to insert raw text, you simply give any
of the several "insert" commands and then type away.)

One or two characters are used for the basic commands. For
example:

insert

cw

change word

Using letters as commands, you can edit a file with great speed.
You don't have to memorize banks of function keys or stretch your
fingers to reach awkward combinations of keys. Most of the
commands can be remembered by the letter that performs them,
and nearly all commands follow similar patterns and are related to
each other.

论坛徽章:
0
3 [报告]
发表于 2008-06-11 13:11 |只看该作者
In general, vi commands:


  * Are case-sensitive (uppercase and lowercase keystrokes mean
different things; I is different from i).

  * Are not shown (or "echoed") on the screen when you type
them.

  *Do not require a RETURN after the command.

There is also a group of commands that echo on the bottom line of
the screen. Bottom-line commands are preceded by different
symbols. The slash (/) and the question mark (?) begin search
commands, and are discussed in Chapter 3. A colon (:) begins all
ex commands. ex commands are those that are used by the ex line
editor. The ex editor is available to you when you use vi, because
ex is the underlying editor, and vi is really just its "visual" mode. ex
commands and concepts are discussed fully in Chapter 5, but this
chapter introduces you to the ex commands to quit a file without
saving edits.

1.1 Opening and Closing Files
You can use vi to edit any text file. vi copies the file to be edited
into a buffer (an area temporarily set aside in memory), displays
the buffer (though you can see only one screenful at a time), and
lets you add, delete, and change text. When you save your edits, vi
copies the edited buffer back into a permanent file, replacing the old
file of the same name. Remember that you are always working on a
copy of your file in the buffer, and that your edits will not affect
your original file until you save the buffer. Saving your edits is also
called "writing the buffer," or more commonly, "writing your file."

1.1.1 Opening a File
vi is the UNIX command that invokes the vi editor for an
existing file or for a brand new file. The syntax for the vi command
is:

$ vi [filename ]

The brackets shown on the above command line indicate that the
filename is optional. The brackets should not be typed. The $ is the
UNIX prompt. If the filename is omitted, vi will open an unnamed
buffer. You can assign the name when you write the buffer into a
file. For right now, though, let's stick to naming the file on the
command line.

[[i] 本帖最后由 projl 于 2008-6-11 13:28 编辑 [/i]]

论坛徽章:
0
4 [报告]
发表于 2008-06-11 13:11 |只看该作者
A filename must be unique inside its directory. On older System V
UNIX systems, it cannot exceed 14 characters in length (most
common UNIX systems allow much longer names). A filename can
include any 8-bit character except a slash (/), which is reserved as
the separator between files and directories in a pathname, and
ASCII NUL, the character with all zero bits. You can even include
spaces in a filename by typing a backslash (\) before the space. In
practice, though, filenames generally consist of any combination of
uppercase and lowercase letters, numbers, and the characters dot
(.) and underscore (_). Remember that UNIX is case-sensitive:
lowercase letters are distinct from uppercase letters. Also remember
that you must press RETURN to tell UNIX that you are finished
issuing your command.

When you want to open a new file in a directory, give a new
filename with the vi command. For example, if you want to open a
new file called practice in the current directory, you would enter:

$ vi practice

Since this is a new file, the buffer is empty and the screen appears
as follows:

~
~
~
"practice" [New file].

The tildes (~) down the left-hand column of the screen indicate that
there is no text in the file, not even blank lines. The prompt line
(also called the status line) at the bottom of the screen echoes the
name and status of the file.

You can also edit any existing text file in a directory by specifying
its filename. Suppose that there is a UNIX file with the pathname
/home/john/letter. If you are already in the /home/john directory,
use the relative pathname. For example:

$ vi letter

brings a copy of the file letter to the screen.

If you are in another directory, give the full pathname to begin
editing:

$ vi /home/john/letter

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

* When you invoke vi, the message [open mode] appears.

Your terminal type is probably incorrectly identified. Quit the
editing session immediately by typing :q. Check the
environment variable $TERM. It should be set to the name of
your terminal. Or ask your system administrator to provide an
adequate terminal type setting.

* You see one of the following messages:

* Visual needs addressable cursor or uplinecapability

* Bad termcap entry

* Termcap entry too long

* terminal: Unknown terminal type

*Block device requiredNot a typewriter

Your terminal type is either undefined, or there's probably
something wrong with your terminfo or termcap entry. Enter
:q to quit. Check your $TERM environment variable, or ask
your system administrator to select a terminal type for your
environment.

*A [new file] message appears when you think a file already
exists.

You are probably in the wrong directory. Enter :q to quit.
Then check to see that you are in the correct directory for
that file (enter pwd at the UNIX prompt). If you are in the
right directory, check the list of files in the directory (with ls)
to see whether the file exists under a slightly different name.

* You invoke vi, but you get a colon prompt (indicating that
you're in ex line-editing mode).

You probably typed an interrupt before vi could draw the
screen. Enter vi by typing vi at the ex prompt (:).

* One of the following messages appears:

* [Read only]

* File is read onlyPermission denied
"Read only" means that you can only look at the file; you
cannot save any changes you make. You may have invoked vi

[ 本帖最后由 projl 于 2008-6-11 13:30 编辑 ]

论坛徽章:
0
6 [报告]
发表于 2008-06-11 13:13 |只看该作者
in view mode (with view or vi -R), or you do not have write
permission for the file. See Section 1.2.1 below.


One of the following messages appears:
Bad file number

Block special fileCharacter special fileDirectoryExecutable

Non-ascii file

file non-ASCII

The file you've called up to edit is not a regular text file. Type
:q! to quit, then check the file you wish to edit, perhaps with
the file command.


When you type :q because of one of the above difficulties, the
message appears:
No write since last change (:quit! overrides).

You have modified the file without realizing it. Type :q! to

leave vi. Your changes from this session will not be saved in

the file.

1.1.3 Modus Operandi
As mentioned earlier, the concept of the current "mode" is
fundamental to the way vi works. There are two modes, command
mode and insert mode. You start out in command mode, where
every keystroke represents a command. In insert mode, everything
you type becomes text in your file.

Sometimes, you can accidentally enter insert mode, or conversely,
leave insert mode accidentally. In either case, what you type will
likely affect your files in ways you did not intend.

Press the ESC key to force vi to enter command mode. If you are
already in command mode, vi will beep at you when you press the
ESC key. (Command mode is thus sometimes referred to as "beep
mode.")

Once you are safely in command mode, you can proceed to repair
any accidental changes, and then continue editing your text.

[ 本帖最后由 projl 于 2008-6-11 13:33 编辑 ]

论坛徽章:
0
7 [报告]
发表于 2008-06-11 13:13 |只看该作者
1.1.4 Saving and Quitting a File
You can quit working on a file at any time, save your edits and
return to the UNIX prompt. The vi command to quit and save edits
is ZZ. Note that ZZ is capitalized.

Let's assume that you do create a file called practice to practice vi
commands, and that you type in six lines of text. To save the file,
first check that you are in command mode by pressing ESC and
then enter ZZ.

Keystrokes Results
ZZ
Give the write and save command, ZZ. Your file is saved as a regular
UNIX file.
ls
Listing the files in the directory shows the new file practice that you
created.

You can also save your edits with ex commands. Type :w to save
your file but not quit vi; type :q to quit if you haven't made any
edits; and type :wq to both save your edits and quit. (:wq is
equivalent to ZZ.) We'll explain fully how to use commands in
Chapter 5, Introducing the ex Editor; for now, you should just
memorize a few commands for writing and saving files.

1.2 Quitting Without Saving Edits
When you are first learning vi, especially if you are an intrepid
experimenter, there are two other ex commands that are handy for
getting out of any mess that you might create.

What if you want to wipe out all of the edits you have made in a
session and then return to the original file? The command:

:e! RETURN

returns you to the last saved version of the file, so you can start
over.

Suppose, however, that you want to wipe out your edits and then
just quit vi? The command:

:q! RETURN

论坛徽章:
0
8 [报告]
发表于 2008-06-11 13:14 |只看该作者
quits the file you're editing and returns you to the UNIX prompt.
With both of these commands, you lose all edits made in the buffer
since the last time you saved the file. vi normally won't let you
throw away your edits. The exclamation point added to the :e or :qcommand causes vi to override this prohibition, performing the
operation even though the buffer has been modified.

1.2.1 Problems Saving Files

You try to write your file, but you get one of the following
messages:

File exists

File file exists - use w!

[Existing file]
File is read only
Type :w! file to overwrite the existing file, or type :w
newfile to save the edited version in a new file.

You want to write a file, but you don't have write permission
for it. You get the message "Permission denied."
Use :w newfile to write out the buffer into a new file. If you
have write permission for the directory, you can use mv to
replace the original version with your copy of it. If you don't
have write permission for the directory, type :w
pathname/file to write out the buffer to a directory in which
you do have write permission (such as your home directory,
or /tmp).


You try to write your file, but you get a message telling you
that the file system is full.
Type :!rm junkfile to delete a (large) unneeded file and free
some space. (Starting an ex command with an exclamation
point gives you access to UNIX.)

Or type :!df to see whether there's any space on another file
system. If there is, choose a directory on that file system and
write your file to it with :w pathname. (df is the UNIX
command to check a disk's free space.)


The system puts you into open mode and tells you that the
file system is full.
The disk with vi's temporary files is filled up. Type :!ls /tmpto see whether there are any files you can remove to gain

[ 本帖最后由 projl 于 2008-6-11 13:31 编辑 ]

论坛徽章:
0
9 [报告]
发表于 2008-06-11 13:14 |只看该作者
some disk space.[3] If there are, create a temporary UNIX shell
from which you can remove files or issue other UNIX
commands. You can create a shell by typing :sh; type CTRL-D
or exit to terminate the shell and return to vi. (On most UNIX
systems, when using a job-control shell, you can simply type
CTRL-Z to suspend vi and return to the UNIX prompt; type fgto return to vi.) Once you've freed up some space, write your
file with :w!.

[3] Your vi may keep its temporary files in /usr/tmp, /var/tmp, or your current directory; you may
need to poke around a bit to figure out where exactly you've run out of room.

You try to write your file, but you get a message telling you
that your disk quota has been reached.
Try to force the system to save your buffer with the ex
command :pre (short for :preserve). If that doesn't work,
look for some files to remove. Use :sh (or CTRL-Z if you are
using a job-control system) to move out of vi and remove
files. Use CTRL-D (or fg) to return to vi when you're done.
Then write your file with :w!.

1.2.2 Exercises
The only way to learn vi is to practice. You now know enough to
create a new file and to return to the UNIX prompt. Create a file
called practice, insert some text, and then save and quit the file.

Open a file called practice in the current directory: vi practice
Insert text: i any text you like
Return to command mode: ESC
Quit vi, saving edits: ZZ

[ 本帖最后由 projl 于 2008-6-11 13:31 编辑 ]

论坛徽章:
0
10 [报告]
发表于 2008-06-11 13:15 |只看该作者
Chapter 2. Simple Editing

This chapter introduces you to editing with vi, and it is set up to be
read as a tutorial. In it you will learn how to move the cursor and
how to make some simple edits. If you've never worked with vi, you
should read the entire chapter.

Later chapters show you how to expand your skills to perform faster
and more powerful edits. One of the biggest advantages for an
adept user of vi is that there are so many options to choose from.
(One of the biggest disadvantages for a newcomer to vi is that there
are so many different editor commands.)

You can't learn vi by memorizing every single vi command. Start
out by learning the basic commands introduced in this chapter. Note
the patterns of use that the commands have in common.

As you learn vi, be on the lookout for more tasks that you can
delegate to the editor, and then find the command that
accomplishes it. In later chapters you will learn more advanced
features of vi, but before you can handle the advanced, you must
master the simple.

This chapter covers:

Moving the cursor

Adding and changing text

Deleting, moving, and copying text

More ways to enter insert mode

2.1 vi Commands
vi has two modes: command mode and insert mode. As soon as you
enter a file, you are in command mode, and the editor is waiting for
you to enter a command. Commands enable you to move anywhere
in the file, to perform edits, or to enter insert mode to add new
text. Commands can also be given to exit the file (saving or
ignoring your edits) in order to return to the UNIX prompt.

You can think of the different modes as representing two different
keyboards. In insert mode, your keyboard functions like a
typewriter. In command mode, each key has a new meaning or
initiates some instruction.


There are several ways to tell vi that you want to begin insert
mode. One of the most common is to press i. The i doesn't appear

[ 本帖最后由 projl 于 2008-6-11 13:32 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP