免费注册 查看新帖 |

Chinaunix

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

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

论坛徽章:
0
151 [报告]
发表于 2008-06-11 15:19 |只看该作者
:tags
The :tags command in vim displays the tag stack.
Typing ^T three times would move you back to main.c, where you started. The
tag facilities make it easy to move around as you edit source code.

8.6 Improved Facilities
The four clones all provide additional features that make simple text editing
easier and more powerful.

Editing the ex command line

The ability to edit ex mode commands as you type them, possibly
including a saved history of ex commands. Also, the ability to complete
filenames and possibly other things, such as commands and options.

No line length limit

The ability to edit lines of essentially arbitrary length. Also, the ability to
edit files containing any 8-bit character.

Infinite undo

The ability to successively undo all of the changes you've made to a file.

Incremental searching

The ability to search for text while you are typing the search pattern.

Left/right scrolling

The ability to let long lines trail off the edge of screen instead of wrapping.

Visual mode

The ability to select arbitrary contiguous chunks of texts upon which some
operation will be done.

Mode indicators

A visible indication of insert mode versus command mode, as well as
indicators of the current line and column.

8.6.1 Command-Line History and Completion
Users of the csh, tcsh, ksh, and bash shells have known for years that being able
to recall previous commands, edit them slightly, and resubmit them makes them
more productive.

论坛徽章:
0
152 [报告]
发表于 2008-06-11 15:20 |只看该作者
This is no less true for editor users than it is for shell users; unfortunately, UNIX
vi does not have any facility to save and recall ex commands.

This lack is remedied in each of the clones. Although each one provides a
different way of saving and recalling the command history, each one's mechanism
is usable and useful.

In addition to a command history, all of the editors can do some kind of
completion. This is where you type the beginning of, for example, a filename. You
then type a special character (such as tab), and the editor completes the filename
for you. All of the editors can do filename completion, some of them can complete
other things as well. Details are provided in each editor's chapter.

8.6.2 Arbitrary Length Lines and Binary Data
All four clones can handle lines of any length.[7] Historic versions of vi often had
limits of around 1,000 characters per line; longer lines would be truncated.

[7] Well, up to the maximum value of a C long, 2,147,483,647.
All four are also 8-bit clean, meaning that they can edit files containing any 8-bit
character. It is even possible to edit binary and/or executable files, if necessary.
This can be really useful, at times. You may or may not have to tell each editor
that a file is binary.

nvi

Automatically handles binary data. No special command-line or ex options
are required.

elvis

Under UNIX, does not treat a binary file differently from any other file. On
other systems, it uses the elvis.brf file to set the binary option, to avoid
newline translation issues. (The elvis.brf file and hex display modes are
described in Section 10.10.)

vim

Does not have a limit on the length of a line. When binary is not set, vim
is like nvi, and automatically handles binary data. However, when editing a
binary file, you should either use the -b command-line option or :set
binary. These set several other vim options that make it easier to edit
binary files.

vile

Automatically handles binary data. No special command-line or ex options
are required.

Finally, there is one tricky detail. Traditional vi always writes the file with a final
newline appended. When editing a binary file, this might add one character to the
file and cause problems. nvi and vim are compatible with vi by default, and add

论坛徽章:
0
153 [报告]
发表于 2008-06-11 15:20 |只看该作者
that newline. In vim you can set the binary option, so this doesn't happen. elvis
and vile never append the extra newline.

8.6.3 Infinite Undo
UNIX vi allows you to undo only your last change, or to restore the current line to
the state it was in before you started making any changes. All of the clones
provide "infinite undo," the ability to keep undoing your changes, all the way back
to the state the file was in before you started any editing.

8.6.4 Incremental Searching
When incremental searching is used, the editor moves the cursor through the file,
matching text as you type the search pattern. When you finally type RETURN, the
search is finished.[8] If you've never seen it before, it is rather disconcerting at
first, but after a while you get used to it.

[8] emacs has always had incremental searching.
elvis does not support incremental searching. nvi and vim enable incremental
searching with an option, and vile uses two special vi mode commands. vile can
be compiled with incremental searching disabled, but it is enabled by default.
Table 8.6 shows the options each editor provides.

Table 8.6. Incremental Searching
Editor Option Command Action
nvi searchincr
The cursor moves through the file as you type,
always being placed on the first character of the text
that matches.
vim incsearch
The cursor moves through the file as you type. vim
highlights the text that matches what you've typed
so far.
vile ^X S, ^X
R
The cursor moves through the file as you type,
always being placed on the first character of the text
that matches. ^X S incrementally searches forward
through the file, while ^X R incrementally searches
backward.
8.6.5 Left-Right Scrolling
By default, vi and most of the clones wrap long lines around the screen. Thus, a
single logical line of the file may occupy multiple physical lines on your screen.

There are times when it might be preferable if a long line simply disappeared off
the right-hand edge of the screen, instead of wrapping. Moving onto that line and
then moving to the right would "scroll" the screen sideways. This feature is
available in all of the clones. Typically, a numeric option controls how much to
scroll the screen, and a Boolean option controls whether lines wrap or disappear
off the edge of the screen. vile also has command keys to perform sideways
scrolling of the entire screen. Table 8.7 shows how to use horizontal scrolling with
each editor.

论坛徽章:
0
154 [报告]
发表于 2008-06-11 15:21 |只看该作者
Table 8.7. Sideways Scrolling
Editor Scroll Amount Option Action
nvi sidescroll
= 16 leftright
Off by default. When set, long lines simply go off
the edge of the screen. The screen scrolls left or
right by 16 characters at a time.
elvis sidescroll
= 8 wrap
Off by default. When set, long lines simply go off
the edge of the screen. The screen scrolls left or
right by 8 characters at a time.
vim sidescroll
= 0 wrap
Off by default. When set, long lines simply go off
the edge of the screen. With sidescroll set to
zero, each scroll puts the cursor in the middle of
the screen. Otherwise the screen scrolls by the
desired number of characters.
vile sideways =
0 linewrap
Off by default. When set, long lines wrap. Thus,
the default is to have long lines go off the edge
of the screen. Long lines are marked at the left
and right edges with < and >. With sideways set
to zero, each scroll moves the screen by .
Otherwise the screen scrolls by the desired
number of characters.
horizscroll
On by default. When set, moving the cursor
along a long line off-screen shifts the whole
screen. When not set, only the current line
shifts; this may be desirable on slower displays.
vile has two additional commands, ^X ^R and ^X ^L. These two commands scroll
the screen right and left, respectively, leaving the cursor in its current location on
the line. You cannot scroll so far that the cursor position would go off the screen.

8.6.6 Visual Mode
Typically, operations in vi apply to units of text such lines, words, or characters,
or to sections of text from the current cursor position to a position specified by a
search command. For example, d/^} deletes up to the next line that starts with a
right brace. elvis, vim, and vile all provide a mechanism to explicitly select a
region of text to which an operation will apply. In particular, it is possible to
select a rectangular block of text and apply an operation to all the text within the
rectangle! See each editor's respective chapter for the details.

8.6.7 Mode Indicators
As you know by now, vi has two modes, command mode and insert mode.
Usually, you can't tell by looking at the screen which mode you're in.
Furthermore, often it's useful to know where in the file you are, without having to
use the ^G or ex := commands.

Two options address these issues, showmode and ruler. All four clones agree on
the option names and meanings, and even Solaris vi has the showmode option.

Table 8.8 lists the special features in each editor.

论坛徽章:
0
155 [报告]
发表于 2008-06-11 15:21 |只看该作者
Table 8.8. Position and Mode Indicators
Editor With ruler, displays With showmode, displays
nvi row and column insert, change, replace, and command mode indicators
elvis row and column input and command mode indicators
vim row and column insert, replace, and visual mode indicators
vile
row, column, and
percent of file
insert, replace, and overwrite mode indicators
vi N/A
separate mode indicators for open, input, insert,
append, change, replace, replace one character, and
substitute modes
The GUI versions of elvis and vim change the cursor shape depending upon the
current mode.

8.7 Programming Assistance
vi was developed primarily as a programmer's editor. It has features that make
things especially easy for the UNIX programmer鈥攕omeone writing C programs
and troff documentation. (Real Programmers write Real Documentation in troff.)
Several of the clones are proud bearers of this tradition, adding a number of
features that make them even more usable and capable for the "power user."[9]

[9] In contrast to the What You See Is What You Get (WYSIWYG) philosophy, UNIX is the You Asked For It,
You Got It operating system. (With thanks to Scott Lee.)
Two features (among many) most deserve discussion:

Edit-compile speedup

elvis, vim, and vile allow you to easily invoke make(1), capture the errors
from your compiler, and automatically move to the lines containing the
errors. You can then fix the errors and re-run make, all from within the
editor.

Syntax highlighting

elvis, vim, and vile have the ability to highlight and/or change the color of
different syntactic elements in different kinds of files.

8.7.1 Edit-Compile Speedup
Programming often consists of a "compile-test-debug" cycle. You make changes,
compile the new code, and then test and debug it. When learning a new
language, syntax errors are especially common, and it is frustrating to be
constantly stopping and restarting (or suspending and resuming) the editor in
between compiles.

elvis, vim, and vile all provide facilities that allow you to stay within the editor
while compiling your program. Furthermore, they capture the compiler's output
and use it to automatically go to each line that contains an error.[10] Consistent
use of this ability can save time and improve programmer productivity.

论坛徽章:
0
156 [报告]
发表于 2008-06-11 15:22 |只看该作者
[10] Yet another feature that emacs users are accustomed to comes to vi.
Here is an example, using elvis. You are beginning to learn C++, so you start out
with the obligatory first program:

Keystrokes Results
:w
hello.C
You enter the program, forgetting the closing quote, and then write
the program to hello.C.
:make
hello
You type the :make command to run make, which in turn runs the
C++ compiler. (In this case, g++.)
:errlist
The :errlist command moves to the line with the error and displays
the first compiler error message in the status line.

You can fix the error, resave the file, re-run :make and eventually compile your
program without errors.

All of the editors have similar facilities. They will all compensate for changes in
the file, correctly moving you to subsequent lines with errors. More details are
provided in each editor's chapter.

8.7.2 Syntax Highlighting
elvis, vim, and vile all provide some form of syntax highlighting. All three also
provide syntax coloring, changing the color of different parts of the file on
displays that can do so (such as under X11 or the Linux console). See each
editor's chapter for more information.

8.8 Editor Comparison Summary
Most of the clones support most or all of the features described above. Table 8.9
summarizes what each editor supports. Of course, the table does not tell the full
story; the details are provided in each one's individual chapter.

论坛徽章:
0
157 [报告]
发表于 2008-06-11 15:22 |只看该作者
Table 8.9. Feature Summary Chart
Feature nvi elvis vim vile
Multiwindow editing
GUI
Extended regular expressions
Enhanced tags
Tag stacks
Arbitrary length lines
8-bit data
Infinite undo
Incremental searching
Left-right scrolling
Mode indicators
Visual mode
Edit-Compile Speedup
Syntax Highlighting
Multiple OS support
8.9 A Look Ahead
The next four chapters cover nvi, elvis, vim, and vile, in that order. Each chapter
has the following outline:

1.
Who wrote the editor, and why.
2.
Important command-line arguments.
3.
Online help and other documentation.
4.
Initialization鈥攚hat files and environment variables the program reads,
and in what order.
5.
Multiwindow editing.
6.
GUI interface(s), if any.
7.
Extended regular expressions.
8.
Improved editing facilities (tag stacks, infinite undo, etc.).
9.
Programming assistance (edit-compile speedup, syntax highlighting).
10.Interesting features unique to the program.
11.Where to get the sources, and what operating systems the editor runs on.
All of the distributions are compressed with gzip, GNU zip. If you don't
already have it, you can get gzip from ftp://ftp.gnu.org/pub/gnu/gzip1.2.4.
tar The untar.c program available from the elvis ftp site is a very
portable, simple program for unpacking gzip'ed tar files on non-UNIX
systems.

Because each of these programs continues to undergo development, we have not
attempted an exhaustive treatment of each one's features. Such would quickly
become outdated. Instead, we have "hit the highlights," covering the features
that you are most likely to need to know about and that are least likely to change

论坛徽章:
0
158 [报告]
发表于 2008-06-11 15:23 |只看该作者
as the program evolves. You should supplement this book with each one's online
documentation if you need to know how to use every last feature of your editor.

论坛徽章:
0
159 [报告]
发表于 2008-06-11 15:24 |只看该作者
Chapter 9. nvi鈥擭ew vi

nvi is short for "new vi." It was developed initially at the University
of California at Berkeley (UCB), home of the famous BSD (Berkeley
Software Distribution) versions of UNIX. It was used for writing this
chapter.

9.1 Author and History
The original vi was developed at UCB in the late 1970s by Bill Joy, then a
computer science graduate student, and now a founder and vice president of Sun
Microsystems.

Bill Joy first built ex, starting with and heavily enhancing the Sixth Edition ed
editor. The first enhancement was open mode, done with Chuck Haley. Between
1976 and 1979 ex evolved into vi. Mark Horton then came to Berkeley, added
macros "and other features,"[1] and did much of the work on vi to make it work on
a large number of terminals and UNIX systems. By 4.1BSD (1981), the editor
already had essentially all of the features described in Part I of this book.

[1] From the nvi reference manual. Unfortunately, it does not say which features.
Despite all of the changes, vi's core was (and is) the original UNIX ed editor. As
such, it was code that could not be freely distributed. By the early 1990s, when
they were working on 4.4BSD, the BSD developers wanted a version of vi that
could be freely distributed in source code form.

Keith Bostic of UCB started with elvis 1.8,[2] which was a freely distributable vi
clone, and began turning it into a "bug for bug compatible" clone of vi. nvi also
complies with the POSIX Command Language and Utilities Standard (IEEE
P1003.2) where it makes sense to do so.

[2] Although little or no original elvis code is left.
Although no longer affiliated with UCB, Keith Bostic continues to maintain,
enhance, and distribute nvi. The version current at the time of this writing is nvi

1.79.
nvi is important because it is the "official" Berkeley version of vi. It is part of
4.4BSD-Lite II, and is the vi version used on the various popular BSD variants
such as NetBSD and FreeBSD.

9.2 Important Command-Line Arguments
In a pure BSD environment, nvi is installed under the names ex, vi, and view.
Typically they are all links to the same executable, and nvi looks at how it is
invoked to determine its behavior. (UNIX vi works this way too.) It allows the Q
command from vi mode to switch into ex mode. The view variant is like vi, except
that the readonly option is set initially.

nvi has a number of command-line options. The most useful are described here:

-c command

论坛徽章:
0
160 [报告]
发表于 2008-06-11 15:24 |只看该作者
Execute command upon startup. This is the POSIX version of the historical
+command syntax, but nvi is not limited to positioning commands. (The
old syntax is also accepted.)

-F

Don't copy the entire file when starting to edit. This may be faster, but
allows the possibility of someone else changing the file while you're
working on it.

-R

Start in read-only mode, setting the readonly option.

-r

Recover specified files, or if no files are listed on the command line, list all
the files that can be recovered.

-S

Run with the secure option set, disallowing access to external programs.[3]

[3] As with anything labelled "secure," blind trust is usually inappropriate. Keith Bostic says, though,
that you can trust nvi's secure option.
-s

Enter batch (script) mode. This is only for ex, and is intended for running
editing scripts. Prompts and non-error messages are disabled. This is the
POSIX version of the historic "-" argument; nvi supports both.

-t tag

Start editing at the specified tag.

-w size

Set the initial window size to size lines.

9.3 Online Help and Other Documentation
nvi comes with quite comprehensive printable documentation. In particular, it
comes with troff source, formatted ASCII, and formatted PostScript for the
following documents:

The vi Reference Manual

The reference manual for nvi. This manual describes all of the nvi
command line options, commands, options, and ex commands.

The vi Man Page
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP