Misc: vi cheat sheet

Title:

Misc: vi cheat sheet

Author:

Douglas O’Leary <dkoleary@olearycomputers.com>

Description:

Misc: vi cheat sheet

Date created:

02/1997

Date updated:

03/1999

Disclaimer:

Standard: Use the information that follows at your own risk. If you screw up a system, don’t blame it on me…

Edit commands

Text Object

Change

Delete

Copy

1 word

cw

dw

yw

2 words not counting punctuation

2cW or c2W

2dW or d2W

2yW or y2W

3 words back

3cb or c3b

3db or d3b

3yb or y3b

1 line

cc

dd

yy or Y

To end of line

c$ or C

d$ or D

y$

To beginning of line

c0

d0

y0

Single character

r

x or X

y1 or yh

5 characters

5s

5x

5y1


Insert text at current position

i

Insert text at beginnging of line

I

Append text at current position

a

Append text at beginning of line

A

Open new line below cursor for new text

o

Open new line above cursor for new text

O

Delete line and substitute text

S

Overstrike existing characters with new text

R

Join current and next line

J

Toggle case

~

Repeat last action

.

Undo last change

u

Restore line to original state

U


Movement commands

Movement

Commands

left, down, up, right

h, j, k, l

Forward by word

w or W

Backward by word

b or B

To end of line

$

To beginning of line

0 or ^

Scroll forward one screen

^F

Scroll backward one screen

^B

Scroll forward half a screen

^D

Scroll backward half a screen

^U

Scroll forward one line

^E

Scroll backward one line

^Y

Move current line to the top of the screen and scroll

z < RETURN>

Move current line to the center of the screen and scroll

Move current line to the bottom of the screen and scroll.

z-

Redraw the scren

^L

Move to the top of the screen

H

Move to the middle of the screen

M

Move to the bottom of the screen

L

Move to the first character of the next line

  • or <RETURN>

Move to the first non-blank character of current line.

^

Move to column n of current line

n|

Move to the end of a word

e

Move to the end of a word, ignoring punctuation.

E

Move to the beginning of the current sentence

(

Move to the beginning of the next sentence.

)

Move to the beginning of the current paragraph

{

Move to the beginning of the next paragraph

}

Move to the beginning of the current section

[[

Move to the beginning of the next section

]]

Search forward for a pattern

/pattern

Search backward for a pattern

?pattern

Repeat last search

n

Repeat last search in opposite direction

N

Repeat last search forward

/

Repeat last search backward.

n

Move to next occurrence of x in current line.

fx

Move to previous occurrence of x in current line.

Fx

Move to just before next occurrence of x in current line.

tx

Move to just before previous occurrence of x in current line.

Tx

Repeat previous find command in same direction

;

Repeat previous find command in opposite direction

,

Go to n line

nG

Go to the end of the file

G

Show current line

^G


Buffer and marking commands

Command

Meaning

“bcommand

Do commnad with buffer b

mx

Mark current position with x

‘x

Move cursor to the first character of line marked with x

`x

Move cursor to character marked by x

``

Return to previous mark

‘’

Return to beginning of line containing previous mark


ex editor examples

Ex command:

Description

:3,18d

Delete lines 3 - 18

160,224m23

Move lines 160-224 to the line following 23.

23,29co100

Copy lines 23-29 and place them after line 100.

%d

Delete all lines in the file

:100;+5p

Sets the current line to 100 then prints the next five lines. The semicolon is the important syntax. This gets around the error that states First address exceeds second.

340,$w >>new_file

Appends lines 340 - EOF to new_file

:$r

in_file Reads in_file and places contents at the end of the current file.

:0r

in_file Reads in_file and places contents at the top of the current file.

:e# or <ctrl>-^

Switches to the previously edited file.

“f4yy

Yanks four lines into buffer f

“fp

Puts the contents of buffer f after the current line.

:200,210ya j

Yanks lines 200 - 210 into buffer j

:$pu j

Puts the contents of buffer j at the end of the file.

:%s/old/new/g

Global search of the file, replacing old w/new

:g/pattern/s/old/new/g

Searches the file for pattern; once found, changes old to new on matching lines only. A context-sensitive search/replace, if you will.

:g/^$/d

Deletes any blank lines in the file

:%s/biteme/& harder/

Searches globally for biteme and replaces it with biteme harder. The & is shorthand for the entire original search.


UNIX cmds, macros, abbrevs & key mappings

Command

Meaning

:r !date

Results in Mon Jan 6 14:12:20 CST 2003 being put in the line following the current.

:r !sort phone

Reads in the sorted file phone and places it below the current line.

:%sort -r

Reverse sorts the contents of the file

:%!<tr> ‘[A-Z]’ ‘[a-z]’

Converts all capitalized letters to lowercase.

:ab bm biteme harder

Creates an abbreviation such that any time you type bm as a single word, vi will automatically replace it with biteme harder. Must be in insert mode for this to work.

:unab bm

Removes the abbreviation for bm

:map ^R o</<tr>>^M<tr>^M^I<td>

Maps -R to the keysequence that opens a line below the current and adds terminating row, new row, and table data html tags. The ^M and ^I key sequences must be added using <CTRL>-V <return> and <ctrl>-V <TAB> characters.

:unmap ^R

Removes the key mapping.