1. Trang chủ
  2. » Công Nghệ Thông Tin

vi and vim editors pocket reference second edition

94 412 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Vi and Vim Editors Pocket Reference
Tác giả Arnold Robbins
Thể loại Pocket reference
Năm xuất bản Second Edition
Thành phố Beijing
Định dạng
Số trang 94
Dung lượng 3,71 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Identifies optional text; the brackets are not typed.Command-Line Options Command Action vifile Invoke vi on file vifile1 file2 Invoke vi on files sequentially viewfile Invoke vi on file

Trang 3

vi and Vim Editors

Pocket Reference

Trang 6

vi and Vim Editors Pocket Reference, Second Edition

promo-booksonline.com) For more information, contact our corporate/institutional

sales department: (800) 998-9938 or corporate@oreilly.com

Editor: Andy Oram

Copyeditor: Amy Thomson

Production Editor: Adam Zaremba

Proofreader: Sada Preisch

Indexer: John Bickelhaupt

Cover Designer: Karen Montgomery

Interior Designer: David Futato

Illustrator: Robert Romano

Printing History:

Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are

registered trademarks of O’Reilly Media, Inc vi and Vim Editors Pocket Reference, the image of a tarsier, and related trade dress are trademarks of

O’Reilly Media, Inc

Many of the designations used by manufacturers and sellers to distinguishtheir products are claimed as trademarks Where those designations appear

in this book, and O’Reilly Media, Inc., was aware of a trademark claim, thedesignations have been printed in caps or initial caps

While every precaution has been taken in the preparation of this book, thepublisher and author assume no responsibility for errors or omissions, or fordamages resulting from the use of the information contained herein

ISBN: 978-1-449-39217-8

[TM]

1294430582

Trang 7

Substitution and Regular Expressions 11

Internet Resources for vi 80 Program Source and Contact Information 81

Trang 9

vi and Vim Editors Pocket

Reference

Introduction

This pocket reference is a companion to Learning the vi and Vim Editors, by Arnold Robbins et al It describes the vi com-mand-line options, command-mode commands, ex com-mands and options, regular expressions and the use of thesubstitute (s) command, and other pertinent information forusing vi

While retaining coverage of the vi clones, nvi, elvis, and

vile, this edition offers expanded coverage of the Vim editor,which has become the de facto standard version of vi in theGNU/Linux world

The Solaris version of vi served as the “reference” version ofthe original vi for this pocket reference

Trang 10

Identifies optional text; the brackets are not typed.

Command-Line Options

Command Action

vifile Invoke vi on file

vifile1 file2 Invoke vi on files sequentially

viewfile Invoke vi on file in read-only mode

vi -Rfile Invoke vi on file in read-only mode

vi -rfile Recover file and recent edits after a

vi +file Open file at last line

vi+n file Open file directly at line number n

Trang 11

Command Action

vi -ccommand file Open file, execute command, which is

usually a search command or line ber (POSIX)

num-vi+/pattern file Open file directly at pattern

exfile Invoke ex on file

ex -file<script Invoke ex on file, taking commands

from script; suppress informative

mes-sages and prompts

ex -sfile<script Invoke ex on file, taking commands

from script; suppress informative

mes-sages and prompts (POSIX)

vi Commands

vi commands are used in “screen” mode (the default), whereyou use the commands to move around the screen and to per-form operations on the text

Most vi commands follow a general pattern:

[command][number]textobject

or the equivalent form:

[number][command]textobject

Movement Commands

vi movement commands distinguish between two kinds of

“words.” The lowercase commands define a word as a

contig-uous sequence of underscores, letters, and digits The

upper-case commands define a word as a contiguous sequence of

Trang 12

), ( Beginning of next, previous sentence

}, { Beginning of next, previous paragraph

] ], [ [ Beginning of next, previous section

Lines

ENTER First nonblank character of next line

0, $ First, last position of current line

^ First nonblank character of current line

+, - First nonblank character of next, previous

line

H, M, L Top, middle, last line of screen

nH n (number) of lines after top line

nL n (number) of lines before last line

Scrolling

CTRL-F , CTRL-B Scroll forward, backward one screen

CTRL-D , CTRL-U Scroll down, up one half-screen

CTRL-E , CTRL-Y Show one more line at bottom, top of

Trang 13

Command Meaning

Searches

/pattern Search forward for pattern

?pattern Search backward for pattern

n, N Repeat last search in same, opposite

direction

/, ? Repeat previous search forward, backward

fx Search forward for character x in current

; Repeat previous current-line search

, Repeat previous current-line search in

op-posite direction

Line number

CTRL-G Display current line number

:n Move to line n in file

Marking position

mx Mark current position as x

x Move cursor to mark x (grave accent)

‘ ‘ Return to previous mark or context (two

grave accents)

x Move to beginning of line containing mark

x (single quote)

’ ’ Return to beginning of line containing

pre-vious mark (two single quotes)

vi Commands | 5

Trang 14

Editing Commands

Command Action

Insert

i, a Insert text before, after cursor

I, A Insert text before beginning, after end of line

o, O Open new line for text below, above cursor

Change

cmotion Change text between the cursor and the

tar-get of motion

s Substitute: delete character and insert new

text

S Substitute: delete current line and insert new

text

Delete, move

dmotion Delete text between the cursor and the target

of motion

p, P Put deleted text after, before cursor

"np Put text from delete buffer number n after

cursor (for last nine deletions)

Yank

Trang 15

Command Action

"ayy Yank current line into named buffer a (a–z);

uppercase names append text

ymotion Yank text between the cursor and the target

of motion

p, P Put yanked text after, before cursor

"aP Put text from buffer a before cursor (a–z)

Other commands

u, U Undo last edit; restore current line

ex edit commands

:co or :t Copy lines

:.,$d Delete from current line to end of file

:30,60m0 Move lines 30 through 60 to top of file

:.,/pattern/co$ Copy from current line through line

contain-ing pattern to end of file

Exit Commands

Command Meaning

:w! Write (save) file, overriding protection

:wq Write (save) and quit file

:x Write (save) and quit file

ZZ Write (save) and quit file

:30,60wnewfile Write from line 30 through line 60 as newfile

:30,60w>>file Write from line 30 through line 60 and append

Trang 16

Command Meaning

:w %.new Write current buffer named file as file.new

:q! Quit file, overriding protection

Q Quit vi and invoke ex

:efile2 Edit file2 without leaving vi

:e! Return to version of current file as of time of

last write (save)

:e # Edit alternate file

:vi Invoke vi editor from ex

: Invoke one ex command from vi editor

% Current filename (substitutes into ex

^] Look up the location of the identifier under the cursor

in the tags file and move to that location; if tag

stack-ing is enabled, the current location is automatically

pushed onto the tag stack

^T Return to the previous location in the tag stack, i.e.,

pop off one element

Buffer Names

Buffer names Buffer use

1–9 The last nine deletions, from most to least recent

a–z Named buffers to use as needed; uppercase letters

append to the respective buffers

Trang 17

Buffer and Marking Commands

Command Meaning

"b command Do command with buffer b

mx Mark current position with x

x Move cursor to character marked by x (grave accent)

‘ ‘ Return to exact position of previous mark or context

(two grave accents)

x Move cursor to first character of line marked by x

(single quote)

’ ’ Return to beginning of the line of previous mark or

context (two single quotes)

Input Mode Shortcuts

vi provides two ways to decrease the amount of typing you

have to do: abbreviations and maps.

Word Abbreviation

:ababbr phrase

Define abbr as an abbreviation for phrase.

:ab

List all defined abbreviations

:unababbr

Remove definition of abbr.

Command and Input Mode Maps

List the characters that are currently mapped

Input Mode Shortcuts | 9

Trang 18

:map!x sequence

Define character(s) x as a sequence of editing commands

or text that will be recognized in insert mode

interpre-For both command and insert mode maps, the map name x

can take several forms:

Function key notation: a # followed by a digit n represents

the sequence of characters sent by the keyboard’s function

1 Type a vi command sequence or an ex command preceded

by a colon; return to command mode.

2 Delete the text into a named buffer

3 Execute the buffer with the @ command followed by thebuffer letter

The ex command :@buf-name works similarly

Trang 19

Some versions of vi treat * identically to @ when used from the

ex command line In addition, if the buffer character suppliedafter the @ or * commands is *, the command is taken from thedefault (unnamed) buffer

Automatic Indentation

Enable automatic indentation with the following command:

:set autoindent

Four special input sequences affect automatic indentation:

^T Add one level of indentation; typed in insert mode

^D Remove one level of indentation; typed in insert mode

Two commands can be used for shifting source code:

<< Shift a line left eight spaces

>> Shift a line right eight spaces

The default shift is the value of shiftwidth, usually eightspaces

Substitution and Regular Expressions

Regular expressions, and their use with the substitute mand, are what give vi most of its significant editing power

program actually behaves as described here

Substitution and Regular Expressions | 11

Trang 20

The Substitute Command

The general form of the substitute command is:

c Confirm each substitution

g Change all occurrences of old to new on each line (globally)

p Print the line after the change is made

It’s often useful to combine the substitute command with the

ex global command, :g:

:g/Object Oriented/s//Buzzword compliant/g

vi Regular Expressions

(period) Matches any single character except a newline.

Remember that spaces are treated as characters

* Matches zero or more (as many as there are) of the singlecharacter that immediately precedes it

The * can follow a metacharacter, such as , or a range inbrackets

^ When used at the start of a regular expression, ^ requiresthat the following regular expression be found at the be-ginning of the line When not at the beginning of a regularexpression, ^ stands for itself

$ When used at the end of a regular expression, $ requiresthat the preceding regular expression be found at the end

Trang 21

of the line When not at the end of a regular expression,

$ stands for itself

\ Treats the following special character as an ordinary acter Use \\ to get a literal backslash

char-~ Matches whatever regular expression was used in the

last search.

[ ]

Matches any one of the characters enclosed between the

brackets A range of consecutive characters can be fied by separating the first and last characters in the rangewith a hyphen

speci-You can include more than one range inside brackets andspecify a mix of ranges and separate characters

Most metacharacters lose their special meaning insidebrackets, so you don’t need to escape them if you want touse them as ordinary characters Within brackets, thethree metacharacters you still need to escape are \-] Thehyphen (-) acquires meaning as a range specifier; to use

an actual hyphen, you can also place it as the first acter inside the brackets

char-A caret (^) has special meaning only when it’s the firstcharacter inside the brackets, but in this case, the meaningdiffers from that of the normal ^ metacharacter As thefirst character within brackets, a ^ reverses their sense: the

brackets match any one character not in the list For

example, [^a-z] matches any character that’s not a ercase letter

low-CAUTION

On modern systems, the locale can affect the

inter-pretation of ranges within brackets, causing vi to

match letters in a surprising fashion It is better to

use POSIX bracket expressions (see “POSIX

Brack-et Expressions” on page 14) to match specific

kinds of characters, such as all lowercase or all

uppercase characters

Substitution and Regular Expressions | 13

Trang 22

Saves the pattern enclosed between \( and \) into a specialholding space or “hold buffer.” You can save up to ninepatterns in this way on a single line

You can also use the \n notation within a search or stitute string:

POSIX Bracket Expressions

POSIX bracket expressions may contain the following:

Character classes

A POSIX character class consists of keywords bracketed

by [: and :] The keywords describe different classes ofcharacters, such as alphabetic characters, control charac-ters, and so on (see the following table)

Collating symbols

A collating symbol is a multicharacter sequence thatshould be treated as a unit It consists of the charactersbracketed by [ and ]

Equivalence classes

An equivalence class lists a set of characters that should

be considered equivalent, such as e and è It consists of anamed element from the locale, bracketed by [= and =]

All three constructs must appear inside the square brackets of

Trang 23

POSIX character classes

Class Matching characters

[:alnum:] Alphanumeric characters

[:alpha:] Alphabetic characters

[:blank:] Space and tab characters

[:cntrl:] Control characters

[:digit:] Numeric characters

[:graph:] Printable and visible (nonspace) characters

[:lower:] Lowercase characters

[:print:] Printable characters (includes whitespace)

[:punct:] Punctuation characters

[:space:] Whitespace characters

[:upper:] Uppercase characters

[:xdigit:] Hexadecimal digits

Metacharacters Used in Replacement Strings

\n Is replaced with the text matched by the nth pattern

pre-viously saved by \( and \), where n is a number from one

to nine, and previously saved patterns (kept in hold fers) are counted from the left on the line

buf-\ Treats the following special character as an ordinary acter To specify a real backslash, type two in a row (\\)

char-& Is replaced with the entire text matched by the search tern when used in a replacement string This is usefulwhen you want to avoid retyping text

pat-~ The string found is replaced with the replacement textspecified in the last substitute command This is useful forrepeating an edit

Trang 24

More Substitution Tricks

• You can instruct vi to ignore case by typing :setic

• A simple :s is the same as :s//~/

• :& is the same as :s You can follow the & with g to makethe substitution globally on the line, and even use it with

a line range

• You can use the & key as a vi command to performthe :& command, i.e., to repeat the last substitution

• The :~ command is similar to the :& command, but with

a subtle difference The search pattern used is the last

reg-ular expression used in any command, not necessarily the

one used in the last substitute command

• Besides the / character, you may use any meric, nonwhitespace character as your delimiter, exceptbackslash, double quote, and the vertical bar (\, ", and |)

nonalphanu-• When the edcompatible option is enabled, vi remembersthe flags (g for global and c for confirmation) used on thelast substitution and applies them to the next one

Trang 25

Address Symbols

Address Includes

1,$ All lines in the file

x;y Lines x through y, with current line reset to x

-[n] One or n lines previous

+[n] One or n lines ahead

x Line marked with x (single quote)

’ ’ Previous mark (two single quotes)

/pat/ or ?pat? Ahead or back to the line where pat matches

Command Option Symbols

Symbol Meaning

! A variant form of the command

count Repeat the command count times

file Filename: % is current file, # is previous file

Alphabetical List of Commands

The following table of ex commands covers both standard

ex commands and selected commands specific to Vim.Commands covered in “Vim—vi Improved” on page 25 areomitted here

Trang 26

Full name Command Vim only

Append [address] a[!]

text

.

Change [address]c[!]

text

.

Copy [address] codestination

Delete [address] d [buffer]

Join [address]j[!][count]

K (mark) [address] kchar

Left [address] le [count] ✓

List [address] l [count]

Map mapchar commands

Mark [address] machar

Move [address] mdestination

Next n[!] [[+command] filelist]

Number [address] nu [count]

Trang 27

Full name Command Vim only

Open [address] o [/pattern/]

Print [address] p [count]

[address] P [count]Put [address] pu [char]

Read [address] rfilename

Read [address] r !command

set option set nooption set option=value set option?

T (to) [address]tdestination

Tag [address] tatag

V (global exclude) [address] v/pattern/[commands]

Visual [address] vi [type] [count]

Visual vi [+n] [filename]

ex Commands | 19

Trang 28

Full name Command Vim only

Write [address] w[!] [[>>]filename]

Wq (write + quit) wq[!]

Yank [address] y [char] [count]

Z (position line) [address] z[type] [count]

type can be one of:

+ Place line at the top of the dow (default)

win Place line at bottom of thewindow

. Place line in the center of thewindow

^ Print the previous window

= Place line in the center of thewindow and leave the currentline at this line

! (execute command) [address] !command

@ (execute register) [address] @ [char]

= (line number) [address] =

< > (shift) [address] < [count]

Trang 29

vi performs the following initialization steps:

1 If the EXINIT environment variable exists, execute thecommands it contains Separate multiple commands by apipe symbol (|)

2 If EXINIT doesn’t exist, look for the file $HOME/.exrc If itexists, read and execute it

3 If either EXINIT or $HOME/.exrc turns on the exrc option,read and execute the file /.exrc, if it exists

4 Execute search or goto commands given with +/pattern

or +n command-line options (POSIX: -c option)

The exrc files are simple scripts of ex commands; thecommands in them don’t need a leading colon You can putcomments in your scripts by starting a line with a double quote(") This is recommended

Recovery

The commands ex -r or vi -r list any files you can recover.You then use the command:

$ vi -r file

to recover a particular file.

Even without a crash, you can force the system to preserve yourbuffer by using the command :pre (preserve)

Trang 30

optimize (opt) noopt

paragraphs (para) IPLPPPQP LIpplpipbp

Trang 31

Nothing like the Original

For many, many years, the source code to the original vi wasunavailable without a Unix source code license This factprompted the creation of all of the vi clones described in thisreference

In January 2002, the source code for the original ex and vi

became available under an open source license

This code does not compile “out of the box” on modern tems, and porting it is difficult Fortunately, the work hasalready been done If you would like to use the original, “real”

sys-vi, you can download the source code and build it yourself.See http://ex-vi.sourceforge.net/ for more information

Enhanced Tags and Tag Stacks

Vim and most of the other vi clones provide enhanced taggingfacilities You can stack locations on a tag stack, and with Exu-berant ctags, tag more items than just functions

Enhanced Tags and Tag Stacks | 23

Trang 32

Exuberant ctags

The “Exuberant ctags” program was written by DarrenHiebert (home page: http://ctags.sourceforge.net/) As of thiswriting, the current version is 5.8

This enhanced tags file format has three tab-separated fields:the tag name (typically an identifier), the source file containingthe tag, and the location of the identifier Extended attributesare placed after a separating ;" Each attribute is separatedfrom the next by a tab character and consists of two colon-separated subfields The first subfield is a keyword describingthe attribute; the second is the actual value

Extended ctags keywords

Keyword Meaning

arity For functions

class For C++ member functions and variables

enum For values in an enum data type

file For static tags, i.e., local to the file

function For local tags

kind The value is a single letter that indicates the lexical

type of the tag

scope Intended mostly for C++ class member functions

struct For fields in a struct

If the field doesn’t contain a colon, it’s assumed to be of type

kind

Within the value part of each attribute, the backslash, tab, riage return, and newline characters should be encoded as \\,

car-\t, \r, and \n, respectively

Trang 33

Solaris vi Tag Stacking

vi provides ex and vi commands for managing the tag stack

Tag commands—ex

Command Function

ta[g][!] tagstring Edit the file containing tagstring as defined

in the tags file

po[p][!] Pop the tag stack by one element

Tag commands—vi

Command Function

^] Look up the location of the identifier under the cursor

in the tags file and move to that location; if tag ing is enabled, the current location is automaticallypushed onto the tag stack

stack-^T Return to the previous location in the tag stack, i.e.,

pop off one element

Tag management options

Option Function

taglength, tl Controls the number of significant characters in

a tag to be looked up; the default value of zeroindicates that all characters are significant

tags, tagpath The value is a list of filenames in which to look for

tags; the default value is "tags /usr/lib/tags"

tagstack When set to true, vi stacks each location on the

tag stack

Vim—vi Improved

Vim is the most powerful and most popular of the vi clonescurrently in use It is the default version of vi on most GNU/Linux systems

Trang 34

Important Command-Line Options

-b Start in binary mode

-ccommand

Execute command at startup (POSIX version of the

his-torical +command).

-C Run in vi compatibility mode

-f For the GUI version, stay in the foreground

-g Start the GUI version of Vim, if Vim was compiled withsupport for the GUI

Like -o, but split the windows vertically

-n Don’t create a swap file: recovery won’t be possible

-p Open a new tab for each file named on the command line

-qfilename

Treat filename as the “quick fix” file.

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

-s Enter batch (script) mode This is only for ex and intendedfor running editing scripts (POSIX version of the historical

Trang 35

-y Enter “easy” mode, which provides more intuitive ior for beginners.

behav Z Enter restricted mode (same as having a leading r in thename)

Vim Window Management

Vim lets you split the screen into multiple windows and controltheir size and placement

Window management commands—ex

clo[se][!] Close the current window;

be-havior affected by the hidden

option

not the last one on the screen[N]new [position] [file] Create a new window, editing an

empty buffer

on the screen

q[uit][!] Quit the current window (exit if

given in the last window)

res[ize] [±n] Increase or decrease the current

window height by n

res[ize] [n] Set the current window height to

n if supplied; otherwise, set it to

the largest size possible withouthiding the other windows[N]sn[ext] Split the window and move to

the next file in the argument list,

or to the Nth file if a count is

supplied[N]sp[lit] [position] [file] Split the current window in half

Vim—vi Improved | 27

Trang 36

Command Function

sta[g] [tagname] Split the window and run

the :tag command as ate in the new window[N]sv[iew] [position] file Same as :split, but set the

appropri-readonly option for the buffer

wa[ll][!] Write all modified buffers that

Same as :split without a file argument; ^W ^S may

not work on all terminals

With count, go to nth window; otherwise, move to

the window below the current one If in the bottom

window, move to the top one

Trang 37

Command Function

^W W With count, go to nth window; otherwise, move to

window above the current one If in the top

win-dow, move to the bottom one

Rotate all the windows downward; the cursor stays

in the same window

^W R Rotate all the windows upward; the cursor stays in

the same window

^W x

^W ^X

Without count, exchange the current window with

the next one; if there is no next window, exchange

with the previous window With count, exchange

the current window with the nth window (first

win-dow is one; the cursor is put in the other winwin-dow)

^W = Make all windows the same height

^W - Decrease current window height

^W + Increase current window height

Split the current window; in the new upper window,

use the identifier under the cursor as a tag and go to

it

^W f

^W ^F

Split the current window and edit the filename

un-der the cursor in the new window

^W i

^W ^I

Open a new window; move the cursor to the first

line that matches the keyword under the cursor

^W d

^W ^D

Open a new window; move the cursor to the macro

definition that contains the keyword under the

cursor

Vim—vi Improved | 29

Trang 38

Tabbed Editing

Similar to modern web browsers, Vim lets you create and

man-age multiple tabs Within each tab, there can be multiple

win-dows You can then switch back and forth between tabs This

is an easy way to work on multiple unrelated editing taskswithout cluttering up your screen Tabs are supported in boththe character and the GUI versions of Vim

Managing tabs—ex

Tabs are numbered from one

[count] tabcommand Run command, but open a new

tab when otherwise a new dow would be opened, e.g.,use :tab split to split the cur-rent buffer into a new tab

win-tabc[lose][!] [count] Close the current tab page With

count, close the page whose number is indicated in count.

Use ! to force closing, even if filecontents have not been saved(the buffer’s contents are notlost)

tabdocommand Execute command for each tab.

tabe[dit] [option] [command]

[file]

Open a new page with a window

editing file With no arguments,

open an empty page

tabf[ind] [option] [command] file Open a new page and search for

file in the value of the path tion, like :find

tabm[ove] [N] Move the current tab page to

after tab page N (change the

or-dering of the tab pages selves, not which tab you’reworking in) With no argument,

Trang 39

Command Function

make the current tab become thelast one

tabnew [option] [command] [file] Same as :tabedit

tabn[ext] [count] Move to next tab, or to tab count.

tabN[ext] [count] Same as :tabprevious

tabo[nly][!] Close all other tab pages

tabp[revious] [count] Move to previous tab, or go back

count tabs This wraps around.

tabr[ewind] Move to the first tab (same

CTRL Page Up

Same as :tabprevious

^W gf Edit the filename under the cursor in a new tab page

^W gF Edit the filename under the cursor in a new tab page,

starting at the line number following the filename

Tabbed editing options

Trang 40

Vim Extended Regular Expressions

Defines an interval expression Interval expressions

de-scribe counted numbers of repetitions In the following

description, n and m represent integer constants:

\{n} Matches exactly n repetitions of the previous

regular expression

\{n,} Matches n or more repetitions of the previous

regular expression, as many as possible

\{n,m} Matches n to m repetitions.

For Vim, n and m can range from 0 to 32,000 Vim requires

the backslash only on the { and not on the } Vim extendstraditional interval expressions with additional matchingnotations, as follows:

\{,m} Matches 0 to m of the preceding regular

expression, as much as possible

\{} Matches 0 or more of the preceding regular

expressions, as much as possible (same as *)

\{-n,m} Matches n to m of the preceding regular

ex-pression, as few as possible

\{-n} Matches n of the preceding regular

expression

\{-n,} Matches at least n of the preceding regular

ex-pression, as few as possible

\{-,m} Matches 0 to m of the preceding regular

expression, as few as possible

\i Matches any identifier character, as defined by the

isident option

\I Like \i, excluding digits

Ngày đăng: 24/04/2014, 16:23

TỪ KHÓA LIÊN QUAN