vi Editor
vi Editor
vi
Unix Overview
1
vi
• vi is a screen based editor
• vi is used by many Unix programmers
• vi has powerful features
• vi has two modes
– Command Mode
– Insert Mode
vi
• “vi” is used in lower case in this course
to familiarize you with the Unix
invocation method when using the
editor.
• The control character will be designated
by the carat symbol (^) in this course.
Unix Overview
2
vi Editor
The Modes of vi
The Modes of vi
Command Mode:
• Allows the entry of commands to
•
•
Unix Overview
manipulate text
vi starts up in Command Mode
Commands can be used to cut and
paste text, delete characters or lines
of text, or move the cursor position
3
The Modes of vi
Command Mode:
• Most commands are one or two
•
characters long
Some commands also use control
characters
The Modes of vi
Command Mode:
• Format of commands
(Optional arguments are in brackets)
[count] command [where]
Unix Overview
4
The Modes of vi
Command Mode:
• The count precedes any command and
•
tells vi to repeat that command that many
times
-> 23x will delete 23 characters
The where parameter can specify how
much of the document the command will
effect or be a command that moves the
current cursor position
The Modes of vi
Command Mode:
• Commands can use both upper and
lower case letters, but the case usage
changes the action of the command:
-> P
put back before cursor
-> p
put back after cursor
Unix Overview
5
The Modes of vi
Command Mode:
• Commands can use the plus (+) and
minus (-) signs to change the action of
the command:
-> z+ move line to top of screen
-> z
move line to middle of screen
-> z- move line to bottom of screen
The Modes of vi
Command Mode:
• Commands can use control characters (^):
-> F
-> ^F
Unix Overview
finds a character on the line under
the cursor going backwards
move forward one screen
6
The Modes of vi
Command Mode:
Line Editor Mode use a colon (:)
• Colon commands are used when
commands are not easily expressed by one
or two key strokes
• Colon commands are prefixed with a colon
• Colon commands will appear on the bottom
of the screen
The Modes of vi
Insert Mode:
• Insert Mode puts anything typed on
the keyboard into the current file
Unix Overview
7
The Modes of vi
Whenever you are unsure of which
mode you are in, use the ESCAPE key
or ^[ to return to Command Mode.
If you are already in Command mode
and you use the ESCAPE key or ^[ ,
nothing will happen.
vi Editor
Starting the vi Editor
Unix Overview
8
Starting the vi Editor
• Create new files
– vi newfilename
• Edit existing files
– vi oldfilename
Starting the vi Editor
• Edit an unnamed file
– vi
– When exiting vi, you will be
prompted to name the file
Unix Overview
9
vi Editor
Exiting the vi Editor
Exiting the vi Editor
• You can only exit the editor while in
•
•
Unix Overview
Command Mode
Hitting the ESCAPE or ESC key puts
you in Command Mode
Hitting ^[ will put you in Command
Mode
10
Exiting the vi Editor
• Once in Command Mode, you can
•
•
either save or not save your work
before you quit vi
If you try to simply exit vi without
saving your modifications, vi will
prompt you with a warning
You can also save your changes
without exiting vi
Exiting the vi Editor
Quit Commands:
• Variations of the colon command :q are
•
•
Unix Overview
used to quit vi
To quit vi when no changes have been
made use :q
The :q command will not create a new
file when nothing has been typed in the
new file
11
Exiting the vi Editor
Quit Commands:
• Do not save your changes and exit vi
-> :q
-> :q!
vi will warn you that the file
has not been written since
changes were made
do not write the file and exit
Exiting the vi Editor
Quit Commands:
• Save your changes and exit vi
-> :wq write the file and exit vi
• Use the :w command to save changes
periodically
-> :w write the file
Unix Overview
12
Exiting the vi Editor
Quit Commands:
• There is a non-Colon Command that
can be used to save changes and exit
vi
-> ZZ
write the file and exit vi
vi Editor
vi Editor Commands
Unix Overview
13
vi Editor Commands
The following tables list some of the
available vi Editor Command Mode
commands.
vi Editor Commands
Cursor Movement
h
j
k
l
$
0
^
Unix Overview
Move left (backspace)
Move down
Move up
Move right (spacebar)
Last column of current line
(zero) move cursor to the first column on the current line
Move cursor to first nonblank column on the current line
14
vi Editor Commands
Cursor Movement
w
Move to the beginning of next word or punctuation
mark
W
b
Move past next space
Move to beginning of the previous word or
punctuation mark
B
Move to the beginning of the previous word, ignores
punctuation mark
H
M
L
Move cursor to top of screen
Move cursor to middle of screen
Move cursor to bottom of screen
vi Editor Commands
Screen Movement
G
xG
z+
z
z^F
^B
^D
^U
Unix Overview
Move to the last line in the file
Move to line x
Move current line to top of screen
Move current line to middle of screen
Move current line to bottom of screen
Move forward one screen
Move backward one screen
Move forward one half screen
Move backward one half screen
15
vi Editor Commands
Inserting
r
Replace character under cursor with next character
typed
R
i
a
A
O
Keep replacing character until [esc] is hit
o
Insert before cursor
Append after cursor
Append at end of line
(OH) Open line above cursor, place cursor on line,
enter append mode
(oh) Open line below cursor, place cursor on line,
enter append mode
vi Editor Commands
Deleting
x
##x
cw
dd
##dd
dw
db
d^
d$
Unix Overview
Delete character under cursor
Delete ## of characters
Delete word starting at cursor, enter insert mode
Delete line under cursor
Delete ## lines from cursor position downwards
Delete word under cursor
Delete word before cursor
Delete from cursor position to the beginning of line
Delete from cursor position to the end of line
16
vi Editor Commands
Copying Code
yy
##yy
(yank) ‘copies’ line which may then be put by p or P
(put) command.
Precede with a count for multiple lines.
Put Command
Brings back previous deletion or yank of lines, words, or
characters
p
P
Bring back after cursor
Bring back before cursor
vi Editor Commands
Find Commands
?
/
f
Unix Overview
Finds a word going backwards
Finds a word going forwards
Finds a character on the line under the cursor going
forward
F
Finds a character on the line under the cursor going
backward
t
Find a character on the current line going forward
and stop one character before it
T
Find a character on the current line going backward
and stop one character before it
;
Repeat last f, F, t, T
17
vi Editor Commands
Miscellaneous Commands
.
u
U
xp
Repeat last command
Undoes last command
Undoes all command on one line
swaps character under the cursor with the character
after the cursor
ZZ
J
^g ^G
Write file changes and exit vi
%
If at one parenthesis, will jump to its mate
Join current line with the next line
Display information about file:
name, if file modified, current line number
vi Editor Commands
Line Editor Mode Commands
:q
:q!
:wq
:w
:#
:$
Unix Overview
Quit vi
Quit vi and do not save file changes
Write file changes and quit vi
Write file changes
Move to line #
Move to last line of file
18
vi Editor
Advanced vi Editor
Advanced vi Editor
You can customize the way vi behaves
upon start up.
• To see a list of options that can be set
in vi
-> :set all
Unix Overview
19
Advanced vi Editor
:set all
Advanced vi Editor
There are two methods to customize the
vi editor with your own settings
• Create a .exrc file in your home directory
and vi will read it upon start up
• Create an environment variable called
EXINIT in your .profile file
Unix Overview
20