Emacs Macro Tutorial: How to Record and Play

Using Emacs Macro feature you can record and play a sequence of actions inside the editor.

This article explains how to perform record and play inside Emacs editor with an example.

If you are a Vim editor fan, refer to our How to record and play inside Vim editor article.



High Level Steps to Record and Play inside Emacs

Start recording a macro by pressing ctrl+x ( Perform any actions inside the Emacs editor that you would like to record. Stop recording by pressing ctrl+x ) Play the last recorded macro by pressing ctrl+x e

For using multiple macros, you have to name and save the macros.

High Level Steps for Naming and Saving emacs macro, and playing it later

Press Ctrl+x Ctrl+k n Give name-of-macro, and press ENTER Run the named macro by pressing, M-x name-of-macro

Example: Title case first word in each bullet points

Let us take the following text snippet as an example.

$ cat thegeekstuff.txt * virtualization technologies * sed and awk tips/tricks * emacs tutorials * bash scripting tutorial series

1. Open the thegeekstuff.txt that has the bullet points

Open the above sample text file, and by default the cursor will be placed in the first line.

$ emacs -nw thegeekstuff.txt * virtualization technologies * sed and awk tips/tricks * emacs tutorials * bash scripting tutorial series

2. Start the Recording

Type: Ctrl+x (

Ctrl+x ( indicates start the recording

When you do Ctrl+x (, it will display Defining kbd macro .. at the bottom of screen.

3. Change the case of next

Type: M-c

Press M-c which will title case the next word, and places the cursor after the word.

4. Go to the next line

Type: C-n

Press C-n which will move the cursor to the next line.

Refer our earlier article 7 Essential emacs Editor Navigation Fundamentals.

5. Go to the start of line

Type: C-a

Press C-a which will move the cursor to the starting of the line.

6. Stop recording the macro

Type: C-x )

Ctrl+x ) indicates stop the recording

When you do Ctrl+x ), it will display Keyboard macro defined in the bottom of screen.

7. Give name and save macro

Type: C-x C-k n title-case-macro

Ctrl+x Ctrl+k n indicates name the previous macro

When you do Ctrl+x Ctrl+k n, it will ask ‘Name for last kbd macro:’ (enter the name you require). I am naming it as title-case-macro.

8. Execute the macro for required number of times

Type: M-x title-case-macro

M-x title-case-macro indicates execute the macro which is stored with that name.

To repeat the macro, just prepend the macro with C-u and count.

Type: C-u 3 M-x title-case-macro

C-u N indicates N number of times

M-x title-case-macro indicates execute the macro which is stored with that name.

So, now all your four lines are title cased, as shown below.

* Virtualization technologies * Sed and awk tips/tricks * Emacs tutorials * Bash scripting tutorial series

By default, emacs macro will not be available for further sessions.

Saving the macro in .emacs for future session

After you have named the macro,

open the ~/.emacs file in another buffer ( from same session — do C-x C-f & open ~/.emacs file )

insert the macro by, Type: M-x insert-kbd-macro , which will ask you the name of macro to insert.

, which will ask you the name of macro to insert. Give the name with which you have saved already ( in this example it is title-case-macro ), which will insert some lisp code.

Save, and exit. In all your future session you will be able to access this macro by M-x macroname.

If you enjoyed this article, you might also like..