Open Source Software Shortcut, Permanently Storing Macros in Vim

Read more articles about Vim

Tip of the Trade: If you find yourself using the same commands over and over between Vim session, it's time to consider permanently saving your macros.

A really useful feature of the open source editor Vim is the macro command, which allows you to record and reply a series of commands. To start recording, hit "q" then "a" (or any other lowercase letter identifier). The status bar at the bottom will show recording. Enter your commands as normal, then hit "q" again to stop recording when you're done. To replay the macro, type @a.

So far, so good, but a macro saved like this will not last between sessions. However, the macro is saved into a normal register in the same way as yanked text. To save it permanently, open your ~/.vimrc file in the same session, and use "ap" to paste in the macro commands. Then edit the line to add a mapping, so it looks like this:

map ,PARA v}y}p

This maps the command, PARA to a short macro, which copies the next paragraph, then pastes it underneath. This mapping will be loaded every time you open a new Vim session.

If you find yourself doing this often, it may be worth checking out the marvim vim script, which provides hotkeys to store macros and templates. This also allows you to tab-complete macro names, which is helpful if you have a lot of them.

For occasional use, however, it's just as fast to do it by hand.

Juliet Kemp has been messing around with Linux systems, for financial reward and otherwise, for about a decade. She is also the author of "Linux System Administration Recipes: A Problem-Solution Approach" (Apress, 2009).

Follow ServerWatch on Twitter

This article was originally published on Jun 7, 2010
Page 1 of 1

Thanks for your registration, follow us on our social networks to keep up-to-date