免费注册 查看新帖 |

Chinaunix

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

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

论坛徽章:
0
221 [报告]
发表于 2008-06-11 16:00 |只看该作者
syntax onsource ~/.exrc

The nocp option turns off strict vi compatibility. The incsearch option turns on
incremental searching. The settings for cinoptions, cinwords, and
formatoptions differ from the defaults; the result is to produce a fairly strict
"K&R" C formatting style. Finally, syntax coloring is turned on, and then the rest
of the vi options are read in from the user's .exrc file.

We recommend that you start up vim, set these options as shown, and then
spend some time working on a C or C++ program. Five minutes of playing with
this facility will give you a better feel for it than whatever static examples we
could present on the printed page. We think you'll find the facility really enjoyable
to use.

11.10.2.2 Include file searching
Often, when working with large C programs, it is helpful to be able to see where a
particular type name, function, variable or macro is defined. The tag facility can
help with this, but doing a tag lookup actually moves you to the found location,
which may be more than you need.

vim has a number of commands that search through the current file and through
included files to find other occurrences of a keyword. We summarize them here.

The vi and ex commands fall into four categories: those that display the first
occurrence of a particular object (in the status line), those that display all
occurrences of a particular object, those that jump to the location of the first
occurrence, and those that open a new window and jump to the first occurrence.
Commands that do all four exist to look for keywords, usually the identifier under
the cursor, and to look for macro definitions of the identifier under the cursor.

These commands use the smart syntax facilities (the comments variable described
earlier) to ignore occurrences of the searched-for identifier inside comments. With
a preceding count, they go to the countth occurrence. The search for the
identifier starts at the beginning of the file, unless otherwise noted.

See Table 11.15 for a list of the vim identifier searching commands.

Table 11.15. vim Identifier Search Commands
Command Function
[i Display the first line that contains the keyword under the cursor.
]i
Display the first line that contains the keyword under the cursor, but
start the search at the current position in the file. This command is
most effective when given a count.
[I Display all lines that contain the keyword under the cursor. Filenames
and line numbers are displayed.
]I Display all lines that contain the keyword under the cursor, but start
from the current position in the file.
[ ^I Jump to the first occurrence of the keyword under the cursor. (Note
that ^I is a TAB.)
] ^I Jump to the first occurrence of the keyword under the cursor, but start

论坛徽章:
0
222 [报告]
发表于 2008-06-11 16:00 |只看该作者
the search from the current position.
^W i Open a new window showing the location of the first (or countth)
occurrence of the identifier under the cursor. ^W ^I
[d Display the first macro definition for the identifier under the cursor.
]d Display the first macro definition for the identifier under the cursor, but
start the search from the current position.
[D Display all macro definitions for the identifier under the cursor.
Filenames and line numbers are displayed.
]D Display all macro definitions for the identifier under the cursor, but start
the search from the current position.
[ ^D Jump to the first macro definition for the identifier under the cursor.
] ^D Jump to the first macro definition for the identifier under the cursor, but
start the search from the current position.
^W d Open a new window showing the location of the first (or countth) macro
definition of the identifier under the cursor. ^W ^D

Two options, define and include, describe the source code lines that define
macros and include source files. They have default values appropriate for C, but
can be changed to suit your programming language (e.g., the value
^\(#\s*define\|[a-z]*\s*const\s*[a-z]*\) for define could be used to also
look for definitions of C++ named constants).

The same facilities are also available as ex commands, shown in Table 11.16.

Table 11.16. vim Identifier Search Commands from ex Mode
Command Function
[range]is[earch][!]
[count] [/]pattern[/]
Like [i and ]i, but searches in range lines. The default
is the whole file. The !, if supplied, forces comments to
be searched also. Without the /'s, a word search is
done. With them, a regular expression search is done.
[range]il[ist][!]
[/]pattern[/]
Like [I and ]I, but searches in range lines. The default
is the whole file.
[range]ij[ump][!]
[count] [/]pattern[/]
Like [ ^I and ] ^I, but searches in range lines. The
default is the whole file.
[range]isp[lit][!]
[count] [/]pattern[/]
Like ^W i and ^W ^I, but searches in range lines. The
default is the whole file.
[range]ds[earch][!]
[count] [/]pattern[/]
Like [d and ]d, but searches in range lines. The default
is the whole file.
[range]dl[ist][!]
[/]pattern[/]
Like [D and ]D, but searches in range lines. The default
is the whole file.
[range]dj[ump][!]
[count] [/]pattern[/]
Like [ ^D and ] ^D, but searches in range lines. The
default is the whole file.
[range]dsp[lit][!]
[count] [/]pattern[/]
Like ^W d and ^W ^D, but searches in range lines. The
default is the whole file.
che[ckpath][!]
List all the included files that could not be found. With
the !, list all the included files.

论坛徽章:
0
223 [报告]
发表于 2008-06-11 16:01 |只看该作者
The path option is used to search for included files that do not have an absolute
pathname. Its default value is .,/usr/include,,, which looks in the directory
where the edited file resides, in /usr/include, and in the current directory.

11.10.2.3 Cursor motion commands for programming
A number of enhanced and new cursor motion commands make it easier to find
the opposite ends of matching constructs, as well as to find unmatched constructs
that should be matched, for example, #if statements that do not have a
corresponding #endif. Most of these commands may be preceded by a count,
which defaults to one if not given.

See Table 11.17 for a list of the extending matching commands.

Table 11.17. vim Extended Matching Commands
Command Function
%
Extended to match the /* and */ of C comments, nd also the C
preprocessor conditionals, #if, #ifdef, #ifndef, #elif, #else, and
#endif.
[( Move to the countth previous unmatched (.
[) Move to the countth next unmatched ).
[{ Move to the countth previous unmatched {.
[} Move to the countth next unmatched }.
[# Move to the countth previous unmatched #if or #else.
]# Move to the countth next unmatched #else or #endif.
[*, [/ Move to the countth previous unmatched start of a C comment, /*.
]*, ]/ Move to the countth next unmatched end of a C comment, */.
11.10.3 Autocommands
vim allows you to specify actions that should be executed when a particular event
occurs. This facility gives you a great deal of flexibility and control. As always
though, with power comes responsibility; the vim documentation warns that you
should be careful with the autocommand facility so that you don't accidentally
destroy your text!

The facility is complicated and detailed. In this section we outline its general
capabilities, and provide an example to give you a sense of its flavor.

The autocommand command is named :autocmd. The general syntax is:

:au event filepat command

The event is the kind of event to which this command applies, for example,
before and after reading a file (FileReadPre and FileReadPost), before and after
writing a file (FileWritePre and FileWritePost), and upon entering or leaving a
window (WinEnter and Winleave). There are more defined events, and case in
the event name does not matter.

论坛徽章:
0
224 [报告]
发表于 2008-06-11 16:01 |只看该作者
The filepat is a shell-style wildcard pattern that vim applies to filenames. If they
match, then the autocommand will be applied for this file.

The command is any ex mode command. vim has a special syntax for retrieving
the different parts of filenames, such as the file's extension, or the name without
the extension. These can be used in any ex command, but are very useful with
autocommands.

Multiple autocommands for the same events and file patterns add commands
onto the list. Autocommands can be removed for a particular combination of
events and file patterns by appending ! to the :autocmd command.

A particularly elegant example allows you to edit files compressed with the gzip
program. The file is automatically decompressed when editing starts, and then
recompressed when the file is written out (the fourth line is broken for
readability):

:autocmd! BufReadPre,FileReadPre *.gz set bin:autocmd! BufReadPost,FileReadPost *.gz '[,']!gunzip:autocmd BufReadPost,FileReadPost *.gz set nobin:autocmd BufReadPost,FileReadPost *.gz \

execute ":doautocmd BufReadPost " . expand("%:r")

:autocmd! BufWritePost,FileWritePost *.gz !mv <afile> <afile>:r:autocmd BufWritePost,FileWritePost *.gz !gzip <afile>:r

:autocmd! FileAppendPre *.gz !gunzip <afile>
:autocmd FileAppendPre *.gz !mv <afile>:r <afile>

:autocmd! FileAppendPost *.gz !mv <afile> <afile>:r:autocmd FileAppendPost *.gz !gzip <afile>:r

The first four commands are for reading compressed files. The first two in this set
use ! to remove any previously defined autocommands for compressed files
(*.gz). The compressed file is read into the buffer as a binary file, so the first
command turns on the bin (short for binary) option.

vim sets the marks '[ and '] to the first and last lines of the just read text. The
second command uses this to uncompress the just read file in the buffer.

The next two lines unset the binary option, and then apply any autocommands
that apply to the uncompressed version of the file (e.g., syntax highlighting). The
%:r is the current filename without the extension.

The next two lines are for writing the compressed file. The first one in this set
first removes any previously defined autocommands for compressed files (*.gz),
with these events. The commands invoke a shell to rename the file to not have
the .gz extension, and then run gzip to compress the file. The <afile>:r is the
filename without the extension. (The use of <afile>:r is restricted to
autocommands.) vim writes the uncompressed buffer to the file with the .gz
extension, thus the need for the renaming.

The second line in this set runs gzip to compress the file. gzip automatically
renames the file, adding the .gz extension.

论坛徽章:
0
225 [报告]
发表于 2008-06-11 16:02 |只看该作者
The last four lines handle the case of appending to a compressed file. The first
two of these lines uncompress the file and rename it before appending the
contents to the file.

Finally, the last two lines recompress the file after writing to it, so that the
uncompressed file is not left laying around.

This section just touches the tip of the iceberg of autocommands. For example,
autocommands can be placed into groups, so that they can all be executed or
removed together. All of the syntax coloring commands described in Section

11.9.2 are placed into the highlight group. An autocommand then executes all
of them together when an appropriate file is read.
As an example, instead of having your .vimrc file always execute set cindent for
smart C indenting, you might use an autocommand to do it just for C source
code, like this:

autocmd BufReadPre,FileReadPre *.[chy] set cindent

11.11 Sources and Supported
Operating Systems
vim has its own Internet domain. The best thing to do is start from the home
page at http://www.vim.org/ . There is a FAQ (Frequently Asked Questions) for
vim, at http://www.vim.org/faq/ . Of particular interest are several vim-related
mailing lists; start with http://www.vim.org/mail.html .

Instead of just one or two distribution points, there are a number of ftp sites that
mirror the main vim distribution site. These are all available as
ftp.country.vim.org. Replace country with a two-letter code from Table 11.18.
More details, including other mirror sites, are available via links on the web page,
and in the file ftp://ftp.nl.vim.org/pub/vim/MIRRORS . The other sites are all
mirrors of ftp.nl.vim.org . When retrieving files via ftp, try to use the one that is
closest to you.

Table 11.18. vim Distribution Site Country Codes
Code Country
au Australia
ca Canada
gr Greece
hu Hungary
jp Japan
kr Korea
nl The Netherlands
pl Poland
us United States
The source code for vim is freely distributable. Distribution is permitted in source
and binary form, but if you modify vim and distribute it, you must make your

论坛徽章:
0
226 [报告]
发表于 2008-06-11 16:02 |只看该作者
changes available to the maintainer for possible inclusion in a subsequent release.
vim is also "charityware." This was discussed earlier in this chapter.

vim has been ported to the following systems:

论坛徽章:
0
227 [报告]
发表于 2008-06-11 16:03 |只看该作者
Chapter 12. vile鈥攙i Like Emacs

vile stands for "vi Like Emacs." It started out as a copy of Version

3.9 of MicroEMACS that was modified to have the "finger feel" of vi.
There are currently three maintainers: Paul Fox, Tom Dickey, and
Kevin Buettner. The current version is 8.0; it is essentially the same
as 7.4, but with bug fixes. This chapter was written using vile.
12.1 Authors and History
Paul Fox describes the early vile history this way:

vile's design goal has always been a little different than that of the other clones.
vile has never really attempted to be a "clone" at all, though most people find it
close enough. I started it because in 1990 I wanted to to be able to edit multiple
files in multiple windows, I had been using vi for 10 years already, and the
sources to Micro-EMACS came floating past my newsreader at a job where I had
too much time on my hands. I started by changing the existing keymaps in the
obvious way, and ran full-tilt into the "Hey! Where's `insert' mode?" problem. So
I hacked a little more, and hacked a little more, and eventually released in '91 or
'92. (Starting soon thereafter, major version numbers tracked the year of
release: 7.3 was the third release in '97.)
But my goal has always been to preserve finger-feel (as opposed to the display
visuals), and, selfishly, to preserve finger-feel most for the commands I use.
vile has quite an amazing ex mode, that works very well鈥攊t just looks really odd,
and a couple of commands which are beyond the scope of the current parser are
missing. For the same reasons, vile also won't fully parse existing .exrc files,
since I don't really think that's so important鈥攊t does simple ones, but more
sophisticated ones need some tweaking. But when you toss in vile's built-in
command/macro language, you quickly forget you ever cared about .exrc.

Tom Dickey started working on vile in December of 1992, initially just
contributing patches, and later doing more significant features and extensions,
such as line numbering, name completion, and animating the buffer list window.
Tom states that "Integrating features together is more important to my design
goals than implementing a large number of features."

In February of 1994, Kevin Buettner started working on vile. Initially, he supplied
bug fixes for the X11 version, xvile, and then improvements, such as scrollbars.
This evolved into support for the Motif, OpenLook, and Athena widget sets.
Because, surprisingly, the Athena widgets were not "universally available in a
bugfree form," he wrote a version that used the raw Xt toolkit. This version ended
up providing superior functionality to the Athena version. Kevin also contributed
the initial support in vile for GNU Autoconf.

Currently, vile maintenance is done "by committee," with Tom Dickey being the
primary maintainer. Paul manages the mailing lists.

For the near term, future work will focus on improving the Perl integration, and
enhancing the major mode concept (discussed below).

论坛徽章:
0
228 [报告]
发表于 2008-06-11 16:04 |只看该作者
12.2 Important Command-Line Arguments
Although vile does not expect to be invoked as either vi or ex, it can be invoked
as view, in which case it will treat each file as read-only. Unlike the other clones,
it does not have a line-editor mode.

Here are the important vile command-line arguments:

-?

vile prints a short usage summary and then exits.

-g N

vile will begin editing on the first file at the specified line number. This can
also be given as +N.

-s pattern

In the first file, vile will execute an initial search for the given pattern. This
can also be given as +/pattern.

-t tag

Start editing at the specified tag. The -T option is equivalent, and can be
used when X11 option parsing eats the -t.

-h

Invokes vile on the help file.

-R

Invokes vile in "readonly" mode, no writes are permitted while in this
mode. (This will also be true if vile is invoked as view, or if readonly mode
is set in the startup file.)

Invokes vile in "view" mode, no changes are permitted to any buffer while
in this mode.

@ cmdfile

vile will run the specified file as its startup file, and will bypass any normal
startup file (i.e., .vilerc) or environment variable (i.e., VILEINIT).

12.3 Online Help and Other Documentation
vile currently comes with a single (rather large) ASCII text file, vile.hlp. The
:help command (which can be abbreviated to :h) will open a new window on
that file. You can then search for information on a particular topic, using standard

论坛徽章:
0
229 [报告]
发表于 2008-06-11 16:04 |只看该作者
vi search techniques. Because it is a flat ASCII file, it is also easy to print out and
read through.

In addition to the help file, vile has a number of built-in commands for displaying
information about the facilities and state of the editor. Some of the most useful
commands are:

:show-commands

Creates a new window that shows a complete list of all vile commands,
with a brief description of each one. The information is placed in its own
buffer that can be treated just like any other vile buffer. In particular, it is
easy to write it out to a file for later printing.

:apropos

Shows all commands whose names contain a given substring. This is
easier than just randomly searching through the help file to find
information on a particular topic.

:describe-key

Prompts you for a key or key sequence, and then shows the description of
that command. For instance, the x key will implement the delete-nextcharacter
function.

:describe-function

Prompts you for a function name, and then shows the description of that
function. For instance, the delete-next-character function deletes a
given number of characters to the right of the current cursor position.

The :apropos, :describe-function, and :describe-key commands all give the
descriptive information, plus all other synonyms (since a function may have more
than one name, for convenience), all other keys that are bound to it (since many
key sequences may be bound to the same function), and whether the command
is a "motion" or an "operator." A good example of this is the output of
:describe-function next-line:

"next-line" ^J j #-B
or "down-arrow"
or "down-line"
or "forward-line"
(motion: move down CNT lines )

This shows all four of its names and its three key-bindings. (The sequence #-B is
vile's terminal-independent representation of the up-arrow鈥攁 complete list of
those names is in the help file.)

The VILE_STARTUP_PATH environment variable can be set to a colon-separated
search path for the help file.[1] The VILE_HELP_FILE environment variable can be
used to override the name of the help file (typically vile.hlp).

论坛徽章:
0
230 [报告]
发表于 2008-06-11 16:05 |只看该作者
[1] Although the help file says that this path is also used when searching for the startup file, the version 7.4
source code disagrees. It is actually the search path used for the :source command. In version 8.0, this is
fixed鈥攖he startup file and :source command use the same mechanism.
The combination of online searchable help, built-in command and key
descriptions, and command completion makes the help facility straightforward to
use.

12.4 Initialization
vile and xvile perform the following initializations:

1.
(xvile only) Use the value of the XVILE_MENU environment variable for the
name of the menu description file, if provided. Otherwise, it uses
.vilemenu. The purpose of this file is to set the default menus for the X11
interface. You can then add to or override any of these menus in the other
startup files.
2.
Execute the file named on the command line with @cmdfile, if any. Bypass
any other initialization steps that would otherwise be done.
3.
If the VILEINIT environment variable exists, execute its value. Otherwise,
look for an initialization file.
4.
If the VILE_STARTUP_FILE environment variable exists, use that as the
name of the startup file. If not, on UNIX use .vilerc, on other systems use
vile.rc.
5.
Look for the startup file in the current directory, and then in the user's
home directory. Use whichever one is found first.
As for nvi and vim, you can place common initialization actions into your .exrc file
(i.e., options and commands for UNIX vi, and/or the other clones), and have your
.vilerc file execute :source .exrc before or after the vile-specific initializations.

12.5 Multiwindow Editing
vile is somewhat different from the other clones. It started life as a version of
Micro-Emacs, and then was modified into an editor with the "finger-feel" of vi.

One of the things that versions of emacs have always done is handle multiple
windows and multiple files; as such, vile was the first vi-like program to provide
multiple windows and editing buffers.

As in elvis and vim, the :split command[2] will create a new window, and then
you can use the ex command :e filename to edit a new file in the new window.
After that, things become different, in particular the vi command mode keys to
switch among windows are very different.

[2] That this works is an artifact of the fact that vile allows you to abbreviate commands. The actual
command name is split-current-window.
<preface id="VI6-CH-0"
>
<title>Preface </title>


<para>
Text editing is one of the most common uses of any computer system,
and
<command>vi</command> is one of the most useful standard text
editors>
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP