免费注册 查看新帖 |

Chinaunix

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

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

论坛徽章:
0
241 [报告]
发表于 2008-06-11 16:11 |只看该作者
12.8.1 Command-Line History and Completion
vile stores all your ex commands in a buffer named [History]. This feature is
controlled with the history option, which is true by default. Turning it off
disables the history feature and removes the [History] buffer. The command
show-history will split the screen and display the [History] buffer in a new
window.

Starting with vile 7.4, the colon command line is really a minibuffer. You can use
it to recall lines from the [History] buffer and edit them.

You use the and keys to scroll backward and forward in the history, and
and to move around within the line. Your current delete character (usually
BACKSPACE) can be used to delete characters. Any other characters you type will
be inserted at the current cursor postion.


You can toggle the minibuffer into vi mode by typing the mini-edit character (by
default, ^G). When you do this, vile will highlight the minibuffer using the
mechanism specified by the mini-hilite option. The default is reverse, for
reverse video. In vi mode, you can use vi style commands for positioning. In
Version 8.0, you can also use the i, I, a, and A vi commands.

An interesting feature is that vile will use the history to show you previous data
that corresponds to the command you're entering. For instance, after typing :set
followed by a space, vile will prompt you with Global value:. At that point, you

can use to see previous global variables that you've set, should you wish to
change one of them.

The ex command line provides completion of various sorts. As you type the name
of a command, you can hit the TAB key at any point. vile will fill out the rest of
the command name as much as possible. If you type a TAB a second time, vile
will create a new window showing you all the possible completions.

Completion applies to built-in and user-defined vile commands, tags, filenames,
modes (described later in this chapter), variables, and to the terminal characters
(the character settings such as backspace, suspend, and so on, derived from your
stty settings).

As a side point, this leads to an interesting phenomenon. In vi-style editors,
commands may have long names, but they tend to be unique in the first few
characters, since abbreviations are accepted. In emacs-style editors, command
names often are not unique in the first several characters, but command
completion still allows you to get away with less typing.

12.8.2 Tag Stacks
Tag stacking is described in Section 8.5.3. In vile, tag stacking is available and
straightforward. It is somewhat different than the other clones, most notably in
the vi mode commands that are used for tag searching and popping the tag
stack. Table 12.2 shows the vile tag commands.

论坛徽章:
0
242 [报告]
发表于 2008-06-11 16:12 |只看该作者
Table 12.2. vile Tag Commands

Command

Function

Edit the file containing tagstring as defined in the tags file. The !

ta[g][!]

forces vile to switch to the new file if the current buffer has been

[tagstring]

modified but not saved.
Pops a cursor position off the stack, restoring the cursor to its


pop[!]

previous position.

next-tag

Continues searching through the tags file for more matches.
Creates a new window that displays the tag stack. The display

show-tagstack

changes as tags are pushed onto or popped off of the stack.

The vi mode commands are described in Table 12.3.

Table 12.3. vile Command Mode Tag Commands

Command

Function

Look up the location of the identifier under the cursor in the tags file,

^]

and move to that location. The current location is automatically
pushed onto the tag stack.

^T ^X

Return to the previous location in the tag stack, i.e., pop off one

^]

element.

^A ^]

Same as the :next-tag command.

As in the other editors, options control how vile manages the tag related
commands, as shown in Table 12.4.

Table 12.4. vile Options for Tag Management
Option Function
taglength
Controls the number of significant characters in a tag that is to
be looked up. The default value of zero indicates that all
characters are significant.
tagignorecase Makes tag searches ignore case. By default this option is false.
tagrelative
When using a tags file in another directory, filenames in that tags
file are considered to be relative to the directory where the tags
file is.
tags
Can be set to a whitespace separated list of tags files to use for
looking up tags. vile loads all tags files into separate buffers that
are hidden by default, but that can be edited if you wish. You can
place environment variables and shell wildcards into tags.
tagword
Uses the whole word under the cursor for the tag lookup, not just
the sub-word starting at the current cursor position. This option
is disabled by default, which keeps vile compatible with vi.
12.8.3 Infinite Undo
vile is similar in principle but different in practice from the other editors. Like elvis
and vim, there is an undo limit you can set, but like nvi, the . command will do

论坛徽章:
0
243 [报告]
发表于 2008-06-11 16:12 |只看该作者
the next undo or redo, as appropriate it. Separate vi mode commands implement
successive undo and redo.

vile uses the undolimit option to control how many changes it will store. The
default is 10, meaning that you can undo up to the 10 most recent changes.
Setting it to zero allows true "infinite undo," but this may consume a lot of
memory.

To start an undo, first use either the u or ^X u commands. Then each successive
. command will do another undo. Like vi, two u commands just toggle the state
of the change; however, each ^X u command does another undo.

The ^X r command does a redo. Typing . after the first ^X r will do successive
redos. You can provide a count to the ^X u and ^X r commands, in which case
vile will perform the requested number of undos or redos.

12.8.4 Arbitrary Length Lines and Binary Data
vile can edit files with arbitrary length lines, and with an arbitrary number of
lines.

vile automatically handles binary data. No special command lines or options are
required. To enter 8-bit text, type ^V followed by an x and two hexadecimal
digits, or a 0 and three octal digits, or three decimal digits.

12.8.5 Incremental Searching
As mentioned in Section 8.6.4, you perform incremental searching in vile using
the ^X S and ^X R commands. It is not necessary to set an option to enable
incremental searching.

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 backwards.

You may wish to add these commands (described below) to your .vilerc file to
make the more familiar / and ? search commands work incrementally:

bind-key incremental-search /
bind-key reverse-incremental-search ?

Also of interest is the "visual match" facility, which will highlight all occurrences of
the matched expression. For a .vilerc file:

set visual-matches reverse

This command directs vile to use reverse video for visual matching. Since the
highlighting can sometimes be visually distracting, the = command will turn off
any current highlighting until you enter a new search pattern.

论坛徽章:
0
244 [报告]
发表于 2008-06-11 16:13 |只看该作者
12.8.6 Left-Right Scrolling
As mentioned in Section 8.6.5 in Chapter 8, you enable left-right scrolling in vile
using :set nolinewrap. Unlike the other editors, left-right scrolling is the
default. Long lines are marked at the left and right edges with < and >. The value
of sideways controls the number of characters by which vile shifts the screen
when scrolling left to right. With sideways set to zero, each scroll moves the
screen by one third. Otherwise the screen scrolls by the desired number of
characters.

12.8.7 Visual Mode
vile is different from elvis and vim in the way you highlight the text you want to
operate on. It uses the "quoted motion" command, q.

You enter q at the beginning of the region, any other vi motions to get to the
opposite end of the region, and then another q to end the quoted motion. vile
highlights the marked text.

Arguments to the q command determine what kind of highlighting it will do. 1q(same as q) does an exact highlighting, 2q does line-at-a-time highlighting, and
3q does rectangular highlighting.

Typically, you use a quoted motion in conjunction with an operator, such as d or

y. Thus, d3qjjwq deletes the rectangle indicated by the motions. When used
without an operator, the region is left highlighted. It can be referred to later using
^S. Thus, d ^S will delete the highlighted region.
In addition, rectangular regions can be indicated through the use of marks.[4] As
you know, a mark can be used to refer to either a specific character (when
referred to with `) or a specific line (when referred to with '). In addition,
referring to the mark (say a mark set with mb) with `b instead of 'b can change
the nature of the operation being done鈥攄'b will delete a set of lines, and d`b will
delete two partial lines and the lines in between. Using the ` form of mark
reference gives a more "exact" region than the ' form of mark reference.

[4] Thanks to Paul Fox for this explanation.
vile adds a third form of mark reference. The \ command can be used as another
way of referring to a mark. By itself, it behaves just like ` and moves the cursor
to the character at which the mark was set. When combined with an operator,
however, the behavior is quite different. The mark reference becomes
"rectangular," such that the action d\b will delete the rectangle of characters
whose corners are marked by the cursor and the character which holds mark b.

Keystrokes
ma

论坛徽章:
0
245 [报告]
发表于 2008-06-11 16:13 |只看该作者
Results
Set mark a at the b in book.
Keystrokes
3jfr
Results
Move the cursor to the r in number to mark the opposite corner.
Keystrokes
^A ~\a
Results
Toggle the case of rectangle bounded with mark a.
The commands which define arbitrary regions and operate upon them are
summarized in Table 12.5.

Table 12.5. vile Block Mode Operations
Command Operation
q Start and end a quoted motion.
^A r Open up a rectangle.
> Shift text to the right. Same as ^A r when the region is rectangular.
< Shift text to the left. Same as d when the region is rectangular.
y Yank the whole region. vile remembers that it was rectangular.
c
Change the region. For a non-rectangular region, delete all the text
between the end points and enter insert mode. For a rectangular
region, prompt for the text to fill the lines.
^A u Change the case of the region to all uppercase.
^A l Change the case of the region to all lowercase.
^A ~ Toggle the case of all alphabetic characters in the region.
^A SPACE Fill the region with spaces.
p, P Put the text back. vile does a rectangular put if the original text was
rectangular.
^A p, ^A
P
Force previously yanked text to be put back as if it were rectangular.
The width of the longest yanked line is used for the rectangle's width.
12.9 Programming Assistance
vile's programming assistance capabilities are discussed in this section.

论坛徽章:
0
246 [报告]
发表于 2008-06-11 16:14 |只看该作者
12.9.1 Edit-Compile Speedup
vile uses two straightforward vi mode commands to manage program
development, shown in Table 12.6.

Table 12.6. vile Program Development vi Mode Commands

Command Function
^X Run command, saving the output in a buffer named
!commandRETURN [Output].
^X ^X Find the next error. vile parses the output and moves to the
location of each successive error.

vile understands the Entering directory XXX and Leaving directory XXX messages
that GNU make generates, allowing it to find the correct file, even if it's in a
different directory.

The error messages are parsed using regular expressions in the buffer [ErrorExpressions]. vile automatically creates this buffer, and then it uses the buffer
when you use ^X ^X. You can add expressions to it as needed, and it has an
extended syntax that allows you to specify where filenames, line numbers,
columns and so on appear in the error messages. Full details are provided in the
online help, but you probably won't need to make any changes, as it works pretty
well "out of the box."

vile's error finder also compensates for changes in the file, keeping track of
additions and deletions as you progress to each error.

The error finder applies to the most recent buffer created by reading from a shell
command. For example, ^X!command produces a buffer named [Output], and :e
!command produces a buffer named [!command]. The error finder will be set
appropriately.

You can point the error finder at an arbitrary buffer (not just the output of shell
commands) using the :error-buffer command. This lets you use the error
finder on the output of previous compiler or egrep runs.

12.9.2 Syntax Highlighting
vile relies on help from an external program to provide syntax coloring. In fact,
there are three programs: one for C programs, one for Pascal programs, and one
for UNIX man pages. The vile documentation provides this sample macro for use
in a .vilerc file:

30 store-macro

write-message "[Attaching C/C++ attributes...]"

set-variable %savcol $curcol

set-variable %savline $curline

set-variable %modified $modified

goto-beginning-of-file

filter-til end-of-file "vile-c-filt"

goto-beginning-of-file

attribute-cntl_a-sequences-til end-of-file

论坛徽章:
0
247 [报告]
发表于 2008-06-11 16:15 |只看该作者
~if &not %modified

unmark-buffer
~endif
%savline goto-line
%savcol goto-column
write-message "[Attaching C/C++ attributes...done ]"

~endm
bind-key execute-macro-30 ^X-q

This runs vile-c-filt over the C source code. This program in turn relies upon the
contents of $HOME/.vile.keywords, which specifies the attributes to provide to
different text. (B for bold, U for underlined, I for italic, and C for one of 16
different colors.) This is Kevin Buettner's version:

Comments:C2
Literal:U
Cpp:CBif:B
else:B
for:B
return:B
while:B
switch:B
case:B
do:B
goto:Bbreak:B

Syntax coloring works on the X11 interface with both Versions 7.4 and 8.0 of vile.
Getting it to work on a Linux console is a bit more complicated. It depends upon
which screen handling interface it was compiled with.

The ncurses library

Configure vile with - -with-screen=ncurses and rebuild. This will then
work out of the box.

The termcap library

This is the default way that vile is configured. Using this version requires
you to have a correct /etc/termcap entry for the Linux console. The
following termcap entry works:[5]

console|linux|con80x25|dumb:\
:do=^J:co#80:li#25:cl=\E[H\E[J:sf=\ED:sb=\EM:\
:le=^H:bs:am:cm=\E[%i%d;%dH:nd=\E[C:up=\E[A:\
:ce=\E[K:cd=\E[J:so=\E[7m:se=\E[27m:us=\E[4m:ue=\E[24m:\
:md=\E[1m:mr=\E[7m:mb=\E[5m:me=\E[m:is=\E[1;25r\E[25;1H:\
:ll=\E[1;25r\E[25;1H:al=\E[L:dc=\E[P:dl=\E[M:\
:it#8:ku=\E[A:kd=\E[B:kr=\E[C:kl=\E[D:kb=^H:ti=\E[r\E[H:\
:ho=\E[H:kP=\E[5~:kN=\E[6~:kH=\E[4~:kh=\E[1~:kD=\E[3~:kI=\E[2

~:\
:k1=\E[[A:k2=\E[[B:k3=\E[[C:k4=\E[[D:k5=\E[[E:k6=\E[17~:\
:k7=\E[18~:k8=\E[19~:k9=\E[20~:k0=\E[21~:K1=\E[1~:K2=\E[5~:\
:K4=\E[4~:K5=\E[6~:\
:pt:sr=\EM:vt#3n:km:bl=^G:vi=\E[?25l:ve=\E[?25h:vs=\E[?25h:

\
:sc=\E7:rc=\E8:cs=\E[%i%d;%dr:
\

论坛徽章:
0
248 [报告]
发表于 2008-06-11 16:15 |只看该作者
:r1=\Ec:r2=\Ec:r3=\Ec:
\
:vb=\E[?5h\E[?5l:
\
:ut:
\
:Co#8:
\
:AF=\E[%a+c\036%dm:
\
:AB=\E[%a+c\050%dm:


[5] This entry courtesy of Kevin Buettner. Note that Linux distributions will vary. This was tested
under Redhat Linux 4.2; you may not need to change your /etc/termcap file.
On the one hand, because syntax highlighting is accomplished with an external
program, it should be possible to write any number of highlighters for different
languages. On the other hand, because the facilities are rather low-level, doing so
is not for non-programmers. The online help describes how the highlight filters
should work.

The directory ftp://ftp.clark.net/pub/dickey/vile/utilities contains user-contributed
filters for coloring makefiles, input, Perl, HTML, and troff. It even contains a
macro that will color the lines in RCS files according to their age!

12.10 Interesting Features
vile has a number of interesting features that are the topic of this section.

The vile editing model

vile's editing model is somewhat different from vi's. Based on concepts
from emacs, it provides key rebinding and a more dynamic command line.

Major modes

vile supports editing "modes." These are groups of option settings that
make it convenient for editing different kinds of files.

The procedure language

vile's procedure language allows you to define functions and macros that
make the editor more programmable and flexible.

Miscellaneous small features

A number of smaller features make day-to-day editing easier.

12.10.1 The vile Editing Model
In vi and the other clones, editing functionality is "hardwired" into the editor. The
association between command characters and what they do is built into the code.
For example, the x key deletes characters, and the i key enters insert mode.
Without resorting to severe trickery, you cannot switch the functionality of the
two keys (if it can even be done at all).

vile's editing model, derived from emacs through MicroEMACS, is different. The
editor has defined, named functions, each of which performs a single editing task,
such as delete-next-character or delete-previous-character. Many of the

论坛徽章:
0
249 [报告]
发表于 2008-06-11 16:16 |只看该作者
functions are then bound to keystrokes, such as binding delete-next-character
to x.

Changing bindings is very easy to do. You use the :bind-key command. As
arguments, you give it the name of the function, and then the key sequence to
bind the function to. You might put the following commands into your .vilerc file:

bind-key incremental-search /
bind-key reverse-incremental-search ?

These commands change the / and ? search commands to do incremental
searching.

In addition to pre-defined functions, vile contains a simple programming language
that allows you to write procedures. You may then bind the command for
executing a procedure to a keystroke sequence. GNU emacs uses a variant of Lisp
for its language, which is extremely powerful. vile has a somewhat simpler, less
general-purpose language.

Also, as in emacs, the vile command line is very interactive. Many commands
display a default value for their operand, which you can edit if not appropriate, or
select by hitting RETURN. As you type vi mode editing commands, such as those
that change or delete characters, you will see feedback about the operation in the
status line.

The "amazing" ex mode that Paul referred to earlier is best reflected in the
behavior of the :s (substitute) command. It prompts for each part of the
command: the search pattern, the replacement text, and any flags.

As an example, let's assume you wish to change all instances of perl to awk
everywhere in your file. In the other editors, you'd simply type
:1,$s/perl/awk/gRETURN, and that's what would appear on the command line.
The following set of screens describes what you see on the vile colon command
line as you type:

Keystrokes Results
:1,$s The first part of the substitute command.
/
substitute pattern: _
vile prompts you for the pattern to search for. Any previous pattern is
placed there for you to re-use.
perl/
replacement string: _
At the next / delimiter, vile prompts you for the replacement text. Any
previous text is placed there for you to re-use.
awk/
(g)lobally, ([1-9])th occurrence on line,
(c)onfirm, and/or (p)rint result: _
At the final delimiter, vile prompts for the optional flags. Enter any
desired flags, then RETURN.

The last prompt line is broken for readability. vile prints it all on one line.

论坛徽章:
0
250 [报告]
发表于 2008-06-11 16:16 |只看该作者
vile follows through with this style of behavior on all appropriate ex commands.
For example, the read command (:r) will prompt you with the name of the last
file you read. To read that file again, just hit RETURN.

Finally, vile's ex command parser is weaker than in the other editors. For
example, you cannot use search patterns to specify line ranges
(:/now/,/forever/s/perl/awk/g), and the move command (m) is not
implemented. In practice, what's not implemented does not seem to hinder you
very much.

12.10.2 Major Modes
A major mode is a collection of option settings that apply when editing a certain
class of file. Many of these options apply on a per-buffer basis, such as the tab-
stop settings. The major mode concept was first introduced in vile 7.2, and is
more fully developed in 7.4 and 8.0.

vile has one pre-defined major mode, cmode, for editing C and C++ programs.
With cmode, you can use % to match C preprocessor conditionals (#if, #else, and
#endif). vile will do automatic source code indentation based on the placement
of braces ({ and }). And it will do smart formatting of C comments. The tabstopand shiftwidth options are set on a per-major-mode basis as well.

Using major modes, you can apply the same features to programs written in
other languages. This example, courtesy of Tom Dickey, defines a new major
mode, shmode, for editing Bourne shell scripts. (This is useful for any Bourne-
style shell, such as ksh, bash, or zsh.)

define-mode sh
set shsuf "\.sh$
"
set shpre "^#!\\s*\/.*sh\\>$
"
define-submode sh comment-prefix "^\\s*/[:#]
"
define-submode sh comments "^\\s*/\\?[:#]\\s+/\\?\\s*$
"
define-submode sh fence-if "^\\s*\\<if\\>
"
define-submode sh fence-elif "^\\s*\\<elif\\>
"
define-submode sh fence-else "^\\s*\\<else\\>
"
define-submode sh fence-fi "^\\s*\\<fi\\>
"


The shsuf (shell suffix) variable describes the file name suffix that indicates a file
is a shell script. The shpre (shell preamble) variable describes a first line of the
file that indicates that the file contains shell code. The define-submode
commands then add options that apply only to buffers where the corresponding
major mode is set. The examples here set up the smart comment formatting and
the smart % command matching for shell programs.

12.10.3 The Procedure Language
vile's procedure language is almost unchanged from that of MicroEMACS.
Comments begin with a semi-colon or a double quote character. Environment
variable names (editor options) start with a $, user variable names start with %. A
number of built-in functions exist for doing comparisons and testing conditions;
their names all begin with &. Flow control commands and certain others begin
with ~. An @ with a string prompts the user for input, and the user's answer is
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP