Emacs Macro Tutorial: How to Record and Play

by SathiyaMoorthy on July 28, 2010

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

  1. Start recording a macro by pressing ctrl+x (
  2. Perform any actions inside the Emacs editor that you would like to record.
  3. Stop recording by pressing ctrl+x )
  4. 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

  1. Press Ctrl+x Ctrl+k n
  2. Give name-of-macro, and press ENTER
  3. 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

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.
  • 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.

Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

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

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 3 comments… read them below or add one }

1 Rick Stanley July 28, 2010 at 9:42 am

Great article for those who are new to Emacs and macros. I do have one point, which I have gotten bitten by in the beginning.

I never ass/u/me that when I am executing the macro that my cursor is at the start of the line, or wherever I need it to be. I would recommend, especially for newbies:

C-a (Start of line)
M-c (Title case the word)
C-n (Move to the next line)

Thanks!

2 Eyal Privman January 21, 2013 at 4:13 am

Very nice, thanks!

Another suggestion – often you would want to define a short key for a frequently used macro. Simply add another line after the macro definition in the .emacs file, such as:
(global-set-key “\M-t” ‘title-case-macro)

This will assign the short key M-t to this macro.

3 Vivek Kumar November 20, 2013 at 11:51 pm

When I do google for some tip and trick and if I get this site hit, then definitely I opens this link for reference.

You explain really awesome thanks for all your post :)

Leave a Comment

Previous post:

Next post: