vi/vim FAQ: Can you provide some examples of how to create vim macros?
Here's a quick example of how to define macros that you can use in the vim editor (or vi editor). As you can tell by looking at them, I use these particular vim macros when editing HTML files.
Define vim macros with the vim map command
Step 1 in the process or creating vim macros is to edit the vi/vim startup file in your home directory. On Unix, Linux, and Mac OS X systems, this file is named .vimrc
.
So, move to your home directory like this:
cd
Then edit your vimrc configuration file like this:
vi .vimrc
Next, insert the following vim macro command lines (vim map commands) into your .vimrc
file. Note that wherever you see the ^M
character below, you actually need to type the following key sequence: [Ctrl]v [Ctrl]m
(That's "control v, control m".)
Here are my file contents:
map #1 o<table>^M <tr>^M <td>^M </td>^M </tr>^M</table> map #2 o<ul>^M <li></li>^M</ul> map #3 o<ul>^M <li><a href=""></a></li>^M</ul>
With those vi map
commands, you are mapping the function keys F1
, F2
, and F3
, respectively.
After you've added these commands, save your changes, and exit vi.
Using the vim macros
To use these vim map settings, open the vim editor, like this:
vim
Now, press the F1
function key, and you should see these lines inserted into your file:
<table> <tr> <td> </td> </tr> </table>
Pretty cool, isn't it? vim macros can save you a bunch of time.