UNIX: vi
|
| | |
|
|
|
|
|
Modes in vi
|
|
There are three basic modes of vi:
- Command mode
- This is the default when you enter vi. In command mode, most letters, or
short sequences of letters, that you type will be interpreted as commands,
without explicitly pressing Enter . If you press Esc
when you're in command mode, your terminal will beep at you. This is a very
good way to tell when you're in command mode. (So is :set showmode
-- see ".exrc Profile and Miscellaneous Commands".)
- Insert mode
- In insert mode, whatever you type is inserted in the file at the cursor
position. Type a (lowercase letter a, for append) to enter insert
mode from command mode; press Esc to end insert mode, and return to
command mode.
- Line mode
- Use line mode to enter line oriented commands. To enter line mode from command mode, type a
colon ( : ). Your cursor moves to the bottom of the screen, by a colon
prompt. Type a line mode command, then press Enter. Any sensible command
from the Unix line editor ex will work, and a few are good to know about.
These commands are indicated in this handout by a colon in front of the command.
Each time you use a line mode command, you must type a colon to enter line
mode, then type the command by the colon prompt at the bottom of the screen,
then press Enter when you finish typing the command. (The search commands
starting with / and ? work similarly.
|
|
|
|
|
Starting vi and Saving Files
|
|
Starting vi: |
vi filename |
start editing filename, create it if necessary |
Saving the file you're working on and/or leaving vi: |
:wq |
write the file to disk and quit |
:q! |
quit without saving any changes |
:w! newfile |
write all lines from the entire current file into the file 'newfile',
overwriting any existing newfile |
:n,m w! newfile |
write the lines from n to m, inclusive, into the file
newfile,
overwriting any existing newfile |
|
|
|
|
|
Moving the Cursor
|
|
Many commands take number prefixes; for example 5w moves
to the right by 5 words.
Type: |
To Move To: |
h |
one space to the left (also try left arrow) |
j |
one line down (also try down arrow) |
k |
one line up (also try up arrow) |
l |
one space to the right (also try right arrow) |
$ |
end of current line |
^ |
beginning of current line |
Enter |
beginning first word on the next line |
G |
end of file |
:n |
line n; use :0 to move the beginning of the file |
w |
beginning of next word; 5w moves to the beginning of the 5th word to the right |
e |
end of next word |
b |
beginning of previous word |
Ctrl-b |
one page up |
Ctrl-f |
one page down |
% |
the matching (, ), [, ], {, or }
(Press % with your cursor on one of these characters to move
your cursor its mate.) |
|
|
|
|
|
Searching for Text
|
|
Type: |
To: |
/string |
search down for string |
?string |
search up for string |
n |
repeat last search from present position |
|
|
|
|
|
Inserting Text
|
|
Type: |
To: |
a |
append starting right of cursor |
A |
append at the end of the current line |
i |
insert starting left of cursor |
I |
insert at beginning of the current line |
o |
open line below cursor, then enter insert mode |
O |
open line above cursor, then enter insert mode |
:r newfile |
add the contents of the file newfile starting below the current
line |
|
|
|
|
|
Deleting Text
|
|
Type: |
To: |
x |
delete single character; 5x deletes 5 characters |
dw |
delete word; 5dw deletes 5 words |
dd |
delete line; 5dd deletes ... well you get the idea! |
cw |
delete word, leaves you in insert mode (i.e. change word) |
cc |
change line -- delete line and start insert mode |
s |
change character -- delete character and start insert mode |
D |
delete from cursor to end of line |
C |
change from cursor to end of line -- delete and start insert mode |
u |
undo last change |
U |
undo all changes to current line |
J |
join current line with line that follows (press Enter in insert
mode to split line) |
|
|
|
|
|
Cutting and Pasting
|
|
Type: |
To: |
xp |
transpose two characters (two commands, x followed by p) |
yy |
yank (i.e. copy) one line into a general buffer (5yy
to yank 5 lines) |
"ayy |
yank into the buffer named a |
P |
put the general buffer back before the current line |
"aP |
put from buffer a before current line |
p |
put the general buffer back after the current line |
"ap |
put from buffer a after the current line |
Note: dd or any other delete will save a copy of the deleted string
in the same general buffer. Therefore a cut and paste can be done with dd
and p, rather than copy and paste with yy and p. But make
sure not to overwrite the buffer with some other yank or delete command before
you have a chance to paste.
|
|
|
|
|
Miscellaneous Commands
|
|
Type: |
To: |
Ctrl-g |
show line number of current line |
Ctrl-l |
redraw the entire display |
:!sh |
fork a shell; type Ctrl-d to get back to vi |
. |
repeat last text change command at current cursor position |
|
|
|
|
|
Customizing vi: the .exrc file
|
|
The .exrc file in your login directory is a collection of vi commands
to customize that environment. For example, an .exrc file containing:
:set showmode
:set ic
tells vi to show the words INPUT MODE in the lower right-hand corner
of the screen when in Insert mode, and :set ic tells the editor to ignore case
during searches. To temporarily tell vi to respect case during
searches, enter :set noic before searching. When you leave and reenter
vi, your .exrc file takes effect again so that case is ignored during
searches.
:set showmode |
show when you are in insert mode |
:set ic |
ignore case when searching |
:set noic |
turn ignore case off |
:set nu |
turn on line numbering |
:set nonu |
turn line numbering off |
and many more |
see the vi man page: man vi for more information |
|
|
|
|