Developer's Blog

Finish jobs in no time with the keyboard macros of Emacs

Today, I want to introduce the [keyboard macro] function used for processing text files with well-loved Emacs.

When is it useful?

I am sure there may be times in your normal day life when you use text data with lines such as in CSV files. If you want to delete just the first data of each line in a CSV file with 3 pieces of data per line, what would you do?

For example, take a look at this file.

abcd,ef,ghi
jk,lmno,pqr
stu,vwx,yz0
(continues for about 2000 lines)

And you want to change the file to the below.

ef,ghi
lmno,pqr
vwx,yz0
(continues for about 2000 lines)

There are various ways of dealing with this task from regular expressions to scripting languages, but I would like to introduce to you how to do so with keyboard macros with just a little Emacs know-how.

What are keyboard macros?

[Keyboard macros] are functions used in Emacs that can be recorded and executed as many times as you want.
To start recording a keyboard macro just press the F3 key.
From then on, all operations are recorded until the F4 key is pressed.
Operations from cursor movements to incremental searches can be recorded one after another.

Decide what to record as a macro

The process to create a keyboard macro for the task shown today is as follows.

1. Define a keyboard macro to process a line to achieve your goal
2. Apply the macro to all lines

OK, so let’s work out what kind of processing is necessary for the 1st line.

abcd,ef,ghi

So we want to remove the first data from this line. How can we achieve this in Emacs?

ef,ghi

The following can be thought when the cursor is at the start of a line.

1. Start region specification (select) (Ctrl-SPC)
2. Move the cursor from the first area to the next using an incremental search. (Ctrl-s , ENTER)
3. Delete a region(From the start of a line to the end of the area) (Ctrl-w)

Using this macro looks like it will work. The main thing to remember is to make an operation that will work on every line.
Take care with data that changes by line such as the number of characters in data.

Let’s try recording a macro

OK, so are you ready?

So, let us firstly place the cursor at the top left edge of row one, and then press F3 to start recording the macro. Perform the instructions just like explained before, and then press F4 to finish recording the macro once you have succeeded in deleting the first data. Even if you make a mistake during the recording there is no need to worry. Just press F4 to finish recording, and then restart recording by pressing F3 again.

Let’s give it a go

You can execute your macro by pressing Ctrl-x e. Also try executing it on the 2nd line. Did it work correctly? If it did, then try the 3rd line. If it didn’t work correctly, then try recording the macro again from the beginning.

Apply it to every line!

If the macro worked correctly, then it is time to apply it to all lines. If you are thinking about repeatedly pressing Ctrl-x e, you are being a little reckless.

Firstly, set the region to the lines you want to apply the macro to. Then press Ctrl-x Ctrl-k r.

So, how was it? This should have applied the macro to all lines, and deleted the first data of each line. Incidentally, Ctrl-x Ctrl-k r calls the command called “apply-macro-to-region-lines”.

Like this, you can quickly finish tasks that would take ages if doing one at a time, in one go.

Summary

The macro I introduced today was very simple, but you can record any key operations so it can be useful for many different tasks. Macros can be defined on the spot when necessary without having to think whether you will reuse it or not, and then once you have finished with it, it really doesn’t matter if you forget about it.

Out of the macros I have used recently, I particularly found the [Only copy data surrounded by double quotation marks] and [Surround region with p tag] functions useful. Macros are useful for those times when [the task is simple, but it is too much bother to write a script to deal with it].
Please try creating your own macro.

Reference: Keyboard Macros: GNU Emacs Manual
http://www.gnu.org/software/emacs/manual/html_node/emacs/Keyboard-Macros.html

Please join us on Facebook for Sleipnir for Windows!

Please join us on Facebook for Sleipnir Mobile!

Please follow us on Twitter.

Facebook Comments