Donation?

Harley Hahn
Home Page

Send a Message
to Harley


A Personal Note
from Harley Hahn

Unix Book
Home Page

List of Chapters

Table of Contents

List of Figures

Chapters...
   1   2   3
   4   5   6
   7   8   9
  10  11  12
  13  14  15
  16  17  18
  19  20  21
  22  23  24
  25  26

Glossary

Appendixes...
  A  B  C
  D  E  F
  G  H

Command
Summary...

• Alphabetical
• By category

Unix-Linux
Timeline

Internet
Resources

Errors and
Corrections

Endorsements


INSTRUCTOR
AND STUDENT
MATERIAL...

Home Page
& Overview

Exercises
& Answers

The Unix Model
Curriculum &
Course Outlines

PowerPoint Files
for Teachers

Appendix C...

Summary of vi Commands

This appendix contains a summary of all the vi commands covered in this book. For more information, see Chapter 22 in which I discuss vi in detail.

Starting vi

vi fileStart vi, edit specified file
vi -R fileStart vi read-only, edit specified file
view fileStart vi read-only, edit specified file
vim fileStart Vim, edit specified file
vim -C fileStart vi in compatibility mode

Stopping vi

:q!Stop without saving data
ZZSave data and stop
:wqSave data and stop
:xSave data and stop

Recovering After System Failure

vi -rDisplay names of files that can be recovered
vi -r fileStart vi, recover specified file

Keys to Use to Make Corrections

<Backspace>/Erase the last character typed
^WErase the last word typed
^X/^UErase the entire line

Controlling the Display

^LRedisplay the current screen
:set numberDisplay internal line numbers
:set nonumberDo not display internal line numbers

Moving the Cursor

hMove cursor one position left
jMove cursor one position down
kMove cursor one position up
lMove cursor one position right
<Left>Move cursor one position left
<Down>Move cursor one position down
<Up>Move cursor one position up
<Right>Move cursor one position right
<Backspace>Move cursor one position left
<Space>Move cursor one position right
-Move cursor to beginning of previous line
+Move cursor to beginning of next line
<Return>Move cursor to beginning of next line
0Move cursor to beginning of current line
$Move cursor to end of current line
^Move cursor to first non-space/tab in current line
wMove cursor forward to first character of next word
eMove cursor forward to last character of next word
bMove cursor backward to first character of previous word
WSame as w; ignore punctuation
ESame as e; ignore punctuation
BSame as b; ignore punctuation
)Move forward to next sentence beginning
(Move backward to previous sentence beginning
}Move forward to next paragraph beginning
{Move backward to previous paragraph beginning
HMove cursor to top line
MMove cursor to middle line
LMove cursor to last line

Moving Through the Editing Buffer

^FMove down (forwards) one screenful
^BMove up (backwards) one screenful
n^FMove down n screenfuls
n^BMove up n screenfuls
^DMove down a half screenful
^UMove up a half screenful
n^DMove down n lines
n^UMove up n lines

Searching for a Pattern

/regexSearch forward for specified regular expression
/Repeat forward search for previous pattern
?regexSearch backward for specified regular expression
?Repeat backward search for previous pattern
nRepeat last / or ? command, same direction
NRepeat last / or ? command, opposite direction

Special Characters to Use in Regular Expressions

.Match any single character except newline
*Match zero or more of the preceding characters
^Match the beginning of a line
$Match the end of a line
\<Match the beginning of a word
\>Match the end of a word
[ ]Match one of the enclosed characters
[^ ] Match any character that is not enclosed
\Interpret the following symbol literally

Line Numbers

nGJump to line number n
1GJump to first line in editing buffer
ggJump to first line in editing buffer
GJump to last line in editing buffer
:map g 1GDefine macro so g will be the same as 1G
:nJump to line number n
:1Jump to first line in editing buffer
:$Jump to last line in editing buffer

Inserting

iChange to insert mode: insert before cursor position
aChange to insert mode: insert after cursor position
IChange to insert mode: insert at start of current line
AChange to insert mode: insert at end of current line
oChange to insert mode: open below current line
OChange to insert mode: open above current line
<Escape>Leave insert mode, change to command mode

Making Changes

rReplace exactly 1 character (do not enter input mode)
RReplace by typing over
sReplace 1 character by insertion
CReplace from cursor to end of line by insertion
ccReplace entire current line by insertion
SReplace entire current line by insertion
cmoveReplace from cursor to move by insertion
~Change the case of a letter

Replacing a Pattern

:s/pattern/replace/Substitute, current line
:line s/pattern/replace/Substitute, specified line
:line,line s/pattern/replace/Substitute, specified range
:%s/pattern/replace/Substitute, all lines

Notes:

At the end of a command, use c to ask for confirmation, and g (global) to replace all occurrences on each line.

To specify a line number, you can use:
  an actual number
  . (period) for the current line
  1 for the first line in the editing buffer
  $ for the last line in the editing buffer

Undoing or Repeating a Change

uUndo last command that modified the editing buffer
URestore current line
.Repeat last command that modified the editing buffer

Breaking and Joining Lines

rBreak current line into two (replace character with newline)
JJoin current line and next line into one long line
:set wm=nAuto line break within n positions of right margin

Deleting

xDelete character at cursor
XDelete character to left of cursor
DDelete from cursor to end of line
ddDelete the entire current line
dmoveDelete from cursor to move
dGDelete from current line to end of editing buffer
d1GDelete from current line to start of editing buffer
:line  dDelete specified line
:line,linedDelete specified range

Deleting: Useful Combinations

dwDelete 1 word
dnwDelete n words
dnWDelete n words (ignore punctuation)
dbDelete backward, 1 word
dn)Delete n sentences
dn}Delete n paragraphs
dGDelete from current line to end of editing buffer
dggDelete from current line to start of editing buffer
d1GDelete from current line to start of editing buffer

Copying the Last Deletion

pCopy buffer; insert after/below cursor
PCopy buffer; insert before/above cursor
xpTranspose two characters
deepTranspose two words (start to the left of first word)
ddpTranspose two lines
"1pu.u.u ...Recall one deletion after another

Copying and Moving Lines

:linecotargetCopy specified line; insert below target
:line,linecotargetCopy specified range; insert below target
:linemtargetMove specified line; insert below target
:line,linemtargetMove specified range; insert below target

Yanking

ymoveYank from cursor to move
yyYank the entire current line

Yanking: Useful Combinations

ywYank 1 word
ynwYank n words
ynWYank n words (ignore punctuation)
ybYank backward, 1 word
yn)Yank n sentences
yn}Yank n paragraphs
yGYank from current line to end of editing buffer
yggYank from current line to start of editing buffer
y1GYank from current line to start of editing buffer

Executing Shell Commands

:!commandPause vi, execute specified shell command
:!!Pause vi, execute previous shell command
:shPause vi, start a shell
:!cshPause vi, start a new C-Shell

Reading Data Into Editing Buffer

:liner fileInsert contents of file after specified line
:r fileInsert contents of file after current line
:liner !commandInsert output of command after specified line
:r !commandInsert output of command after current line
:r !look  patternInsert words that begin with specified pattern

Using Shell Commands to Process Data

n!!commandExecute command on n lines
!move commandExecute command from cursor to move
!move fmtFormat lines from cursor to move

Writing Data

:wWrite data to original file
:w fileWrite data to specified file
:w>> fileAppend data to specified file

Changing the File While Editing

:e fileEdit the specified file
:e! fileEdit the specified file, omit automatic check

Abbreviations

:ab short longSet short as an abbreviation for long
:abDisplay current abbreviations
:una shortCancel abbreviation short

Jump to top of page