Making and Undoing
Changes
There are a lot of ways to change, remove or replace text in
vi. The following commands allow specific changes to be made, and
perhaps most useful, undo changes that have been made.
Command |
Action |
x |
Delete one character |
r |
Replace one character with the next character typed |
dw |
Delete from the cursor position to the end of the current word |
dd |
Delete the current line |
D |
Delete from the current character to the end of this line |
cw |
Same as dw then enter insert mode |
cc |
Clear the text on this line and enter insert mode |
C |
Delete from the current cursor position to the end of the
current line and enter insert mode |
J |
Join the next line to the current line |
u |
Undo the last change. Can be repeated to undo the past
several changes. |
. |
Repeat the last edit command at the current cursor position |
Table 3.4: Commands for Changing Text and Undoing Changes
Like the cursor movement commands, many of these commands can
be preceded with a number to repeat the given command. Even the
undo comman can
be preceded by a number to undo several recent commands.
Copying and Pasting
vi offers many options for copying and pasting including the
ability to copy and paste from multiple buffers (a buffer is
similar to the clipboard in Windows or Mac OS), but the basic copy
and paste commands will only be touched upon. The following
commands allow you to copy and paste.
Command |
Action |
yy |
Copy the current line into the default buffer |
7yy |
Copy the current line plus the next six lines to the default
buffer |
p |
Paste the contents of the default buffer below the current
line |
P |
Paste the contents of the default buffer above the current
line |
Table 3.5: Common Copy and Paste Commands
If multiple lines are copied with the yy command, the number of
lines does not need to be specified when pasting. The paste
commandautomatically pastes
the entire contents of the default buffer.
Searching and Replacing
Searching for text and replacing text in vi can be very handy,
but it is not as easy as you might think! Here are some of the
more useful search and replace commands in vi:
Command |
Action |
/search text |
Find and move the cursor to the next occurrence of search text |
'search text |
Find and move the cursor to the previous occurrence of search
text |
n |
Repeat the last search |
N |
Repeat the last search but in the opposite direction (previous
occurrence instead of next or next occurrence instead of
previous) |
:%s/search text/replace text/g |
Find all occurrences of search text and replace it with
replace text |
:noh |
Turn off highlighting of searched for text |
Table 3.6: Some Search and Replace Commands
When these commands encounter the end of the file, they wrap
around to the beginning and continue searching. If searching
backwards, they wrap from the beginning to the end. It has
probably been noticed that the find and replace command is very
complicated. There are ways of making it even more complicated
and having it search only specific lines, but that level of
complexity is not particularly needed.
Saving and Exiting
Work should be saved often and when all is done, exit vi. As
with other things in vi, there are several ways to save and exit.
Here are a few common ones:
Command |
Action |
:w |
Save changes |
:w filename.txt |
Save changes to filename.txt instead of the file opened |
:q |
Exit vi (changes should have already been saved) |
:q! |
Exit vi without saving changes |
:wq |
Save changes and exit vi |
ZZ |
Save changes and exit vi (same as :wq) |
:w! |
Write changes despite read-only permissions (must be file
owner) |
Table 3.7: Commands for Saving and Exiting in vi
This short section is only a brief introduction to vi, but it
should be enough to do basic editing and some shell scripting. vi
allows text to be manipulated in just about any way imaginable, so
if there is something specific that is to be done in vi, take a
look through the help documents available by entering :help within
vi or in an easier-to-navigate form on the Web at
http://vimdoc.sourceforge.net.