Sections

Sections

Just Enough to Get By

As the heading suggests, in this section I attempt to present a subset of the vi commands large enough to enable you to accomplish basic tasks, but small enough that a beginner can remember most of them. Note that the topics covered in this section are covered in more detail in other sections.

Opening and Closing

You open a file by typing "vi <filename>" on the command line for example:

vi chapter29.txt

You close a file by getting into command mode (use the <Esc> key), typing a colon ":" and then typing wq (write quit). If you don't want to save changes to the file, type q (quit). If it gives you any trouble, type wq! (enthusiastic quit).

Inserting Text

You insert new text by getting into edit mode. There are a lot of ways to get into edit mode. An incomplete list is presented next:

Key

Function

i

Insert new text at current cursor position.

A

Append new text to the end of the current line.

Deleting Text

Delete text by getting into command mode (press <Esc>) and using one of the following keys:

Key

Function

x

Delete a character.

dw

Delete a word.

dd

Delete a line.

Moving Around

Get into command mode by pressing <Esc>. The following keys are active:

Key

Function

h

Left one space

j

Down one line

k

Up one line

l

Right one space

Undo

The undo key is "u". Get into command mode (press <Esc>) and then press "u". This will undo all the changes back to the last time you entered edit mode. You may press undo repeatedly, each time undoing an edit session.

Searching

Search for text by

1.       Getting into command mode (press <Esc>).

2.       Press the forward slash "/". A forward slash character should appear in the lower left corner of your screen.

3.       Typing the string you wish to search for.

Starting and Stopping vi

vi can be invoked from the command line as follows:

Command

Effect

vi -rfile

Recover a buffer saved when vi terminates.

vi +nfile

Open file on line n.

vi +file

Open a file at the last line of file.

vi +/pattern file

Open file at the first instance of pattern.

vifile file...

Open multiple files. Once inside vi, to get to the next file, type :n from command mode.

viewfile

Open file in read-only mode.

The following commands will either get you out of vi, write any changes you may have made, or both:

Key

Function

:wfile

Write to the specified file.

:w!file

Write to specified file, regardless of file permissions.

:wq

Write and quit.

:wq!

Write and quit, regardless of access mode.

:x, ZZ

Quit and save any changes.

:q

Quit.

:q!

Quit without saving changes.

Q

Quit vi and start ex.

Position within a File

Note that for these control keys to be active, you must be in command mode.

By Character Key

Function

H

Left one space

J

Down one line

K

Up one line

l, <space>

Right one space

By Line Key

Function

0

First position of current line

^

First nonblank of current line

$

Last position of current line

+

First character of previous line

-

First character of next line

H

Top line of screen

nH

n lines after top line

M

Middle line of screen

L

Bottom line of screen

nL

n lines before bottom line

By Screen Key

Function

<Ctrl>-f

Forward one screen

<Ctrl>-b

Backward one screen

<Ctrl>-d

Forward one half screen

<Ctrl>-u

Backward one half screen

Line Numbers Key

Function

<Crtrl>-g

Display line number and file

:n

Go to line number n

Searching and Replacing

Searching Key

Function

%

Move to matching parentheses, brace, or bracket.

/pattern

Find next instance of pattern.

/

Repeat previously specified pattern search.

/pattern+n

Go to the nth line after the next instance of pattern.

?

Search backward for pattern.

?pattern?-n

Go to nth line before pattern.

Replacing

s/pattern/replacement

Substitute replacement for first instance of pattern on each line found.

s/pattern/replacement/g

Substitute replacement for all instances of pattern on each line found.

 

graphics/tip.gif

It's usually a good idea to specify a line range when doing pattern replacements. Just indicate the lines you want the changes to apply to, separated by commas for example,

:1,15

 

graphics/tip.gif

To specify control characters in a pattern search, use the key combination <Ctrl>-v, followed by whatever the letter is that corresponds to the control character you're after.

 

Example: To chop out the first five spaces from the beginning of each line (indicated with ^), starting with line 100 and ending with line 200, use

:100,200 s/^ //g

Example: To substitute the word "good" for the word "excellent" from the first line of the file to the bottom of the file ($), use

:1,$ s/excellent/good/g

vi recognizes the following in the context of pattern specification:

Specifying Position Character

Stands for

.

Matches any single character.

^

The beginning of a line.

$

The end of a line.

\<

Beginning of a word.

>

End of a word.

b

Empty string at edge of a word.

B

Empty string not at edge of a word.

Pattern Repetition Character

Action

?

Match the preceding item at most once.

*

Match the preceding any number of times (including none).

+

Match the preceding one or more times.

{n}

Match the preceding exactly n times.

{n,}

Match the preceding n or more times.

{,m}

Match the preceding 0-m times.

{n,m}

Match the preceding at least n, but no more than m times.

Character Classes Notation

Stands for

[:alnum:]

Alphanumeric characters [0-9A-Za-z]

[:alpha:]

Alphabetic characters [A-Za-z]

[:cntrl:]

Control characters

[:digit:]

Digits

[:graph:]

Graphic characters

[:lower:]

Lowercase characters

[:print:]

Printable characters

[:punct:]

Punctuation characters

[:space:]

White space (space, tab )

[:upper:]

Uppercase characters

[:xdigit:]

Hexadecimal digits

Adding and Deleting Text

Either the control characters in this section put you into edit mode at one position or another, or they delete text.

Invoking Edit Mode Character

Effect

a

Append after cursor.

A

Append at end of line.

i

Insert before cursor.

I

Insert at end of line.

o

Open a line below the current line.

O

Open a line above the current line.

Deleting Text Character

Effect

d^

Delete back to line beginning.

d}

Delete up to next paragraph.

d/pattern

Delete up to specified pattern.

dd

Delete current line.

dG

Delete to end of file.

dL

Delete up to last line on screen.

dn

Delete up to next occurrence of previously specified pattern.

dw

Delete word.

x

Delete a character.

X

Delete previous character.

Yanking and Putting

Yanking and putting are more or less the same as the copying and pasting operations found in GUI word processors. You first select some section of text (with one of the yank commands, defined shortly). The text is stored in a buffer either explicitly specified or implied. You then select a new position to place it and use one of the put commands to put it there.

In the following descriptions, buffer is the text buffer specified by a single letter from a to z:

Yank Character

Action

y

Copy current line to new buffer.

yy

Copy current line.

"<buffer><count>yy

Yank count lines to specified buffer.

"<buffer>d

Delete into specified buffer.

Ye

Copy to end of word.

Put Character

Action

"<buffer>p

Put contents of specified buffer.

p

Put most recently deleted line after cursor.

p

Put most recently deleted line before cursor.

Shell Interaction

Rather than force you to quit out of vi when you need to remain something shell related, vi has a variety of mechanisms to enable you to do shell without interrupting your edit session.

Character

Action

:!command

Run the specified command.

:!!

Repeat last command.

:start,stop!command

Send the specified range of lines to command and replace the lines with output of command.

:count!command

Send count lines to command and replace with output.

:<Ctrl>-Z

Suspend edit session.

:rfile

Read in contents of specified file.

:r !command

Read in output of command.

:sh

Invoke a subshell.

:sofile

Source (execute) commands in file.

 



Linux Desk Reference
Linux Desk Reference (2nd Edition)
ISBN: 0130619892
EAN: 2147483647
Year: 2000
Pages: 174
Authors: Scott Hawkins

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net