lets you define keymaps:
short names for a series of one or more other commands.
You can enter
to define a keymap while you're editing a
file with vi
But if you make a mistake, you usually have to re-enter the whole
command to correct the problem.
-functions (say "at-functions")
give you another way to define complex commands.
You can define 26
They're stored in
named buffers (30.5
So if you're also using named buffers for copying and pasting text, you'll
need to share them with your
To define an
Enter the command(s) you want to execute onto one or more lines of the
file you're editing.
Yank or delete the line(s) into a named buffer with a command like
To use the function, type a command like
You can repeat the function by typing
or a dot (
to undo the effects of the
Here's an example.
You're editing a long HTML file with lines like these:
Some heading here
Another heading here
When you see one of those lines, you need to change the
A global substitution with
won't do the job because
some lines need
and others need
; you have to
decide line-by-line as you work through the file.
So you define the function
to change a line to
to change to
To design an
-function, start by thinking how you'd
make the changes by hand.
You'd probably move to the start of the line with
, move to
the right one character with
to change the
, and type in
Then press ESC to return to command mode.
Go to the end of the line with
, move to the character after
the slash with
, then change the second
way you fixed the first one.
To define the function, open a new empty line of your file
(go into text-input mode).
Then type the keystrokes that will make the
type CTRL-v before each ESC or RETURN (31.6
When you're done, press ESC again to go to command mode.
Because the commands for the
change are similar, the easiest
way to make them is by copying and pasting the line for
)-then edit the copy.
The pair of command lines should look like this (where
for the [CTRL-v]
Move to the start of the first line and delete it into the a
buffer by typing
Go to the next line and type
(This will leave two empty lines; delete them with
Now, when you type
, it will execute the commands to change a
on a line will change it to
Move through your file (maybe with a search:
as you go.
to make the same change you made on a previous line.
-function can execute other
For example, here are four lines ready for storing as
0l@c$T/@c ...becomes @a
0l@d$T/@d ...becomes @b
cwH3^[ ...becomes @c
cwH4^[ ...becomes @d
See that the definition of
in it twice?
When you execute
, it will do
to move to the
second character on the line, then do
to change the word
, move to the end of the line and use
-function from another can save re-typing
A disadvantage is that
won't always work as you might expect.
If you type
to make a change in one place,
then move somewhere else and type
does (instead of what you might have wanted,
That's because the
function finishes by doing a
You don't have to delete the definition line into a buffer with
If you think you might need to fine-tune the command, you can yank
(copy) it into a buffer with a command like
Then, if you need to revise the command, re-edit the line and type
to put the revised version into the buffer.
to copy the revised line into another buffer.
-functions can span multiple lines.
For example, if you delete the following four lines with
will open a new line below (
insert four new lines of text:
oThis is the new line one.
This is the new line two.
This is the third line.
This is the fourth.^[
After you execute the function with
your cursor will move to the line below the new fourth line.
Because you included the newlines (RETURNs) in the buffer; each
RETURN moves down a line - including the RETURN after the
If you don't want that, there are two ways to fix it:
Delete the first three lines, including the newlines, into the buffer
Delete the fourth line, without its newline, and append
the buffer by typing
(An uppercase letter like
appends to a named buffer.
deletes all of a line except the newline.)
Some versions of vi
will delete four lines, without the last
newline, when you use
Type all of the text onto a single line; embed the newlines in
that line by typing [CTRL-v]
between each finished line.
It'll look like this:
oThis is the new line one.^MThis is the new line two.^MThis is the new...
Delete that long line into your buffer with
doesn't delete the final newline, your cursor will
stay at the end of the fourth new line after you execute the