Emacs Tips and Tricks

In this blog post I collect some miscellaneous Emacs tips and tricks.

Playing with Macros

In Emacs, you can record a set of actions that you can then apply to a selected region. As an example, let’s transform the following three lines:

First Line
Second Line
Third Line

into a list of items, using a macro:

- First Line
- Second Line
- Third Line

Put your cursor on the First line, then start recording a new keyboard macro by typing:

C-x (

Jump to the beginning of the line (C-a), insert a dash followed by a space (- ) and then move your cursor down one line, so it resides on the Second Line.

Stop recording the macro:

C-x )

Now select the lines Second Line and Third Line and apply the newly recorded macro to the selected region by pressing:

C-x C-k r

You should now see the desired output.

Swapping Words

Say that you want to reverse the order of the parameter for the following function:

foo(Second, First) ->

Position the cursor between the words Second and First. Then, press:


You should obtain the following:

foo(First, Second) ->

Swapping Lines

Given the following two lines:

Second Line
First Line

Put the cursor at the beginning of First Line and press:

C-x C-t

You should get:

First Line
Second Line

Version Control

Emacs has support for the most common Version Control systems (e.g. SVN).

To check-in a single file directly from Emacs, simply follow the steps below:

  • Press C-x v v
  • Write a meaningful change comment
  • Press C-c C-c

To revert changes for the current buffer:

C-x v u

To see the differences for a buffer before committing it:

C-x v =

Executing a Shell Command

If you want to execute a shell command within Emacs, you can simply type:


If you want to include the output into the current buffer, then:

C-u M-!

Clipboard History

Emacs keeps a clipboard history, allowing you to paste old clipboard entries. To utilize this function:

Press the following to paste:


And then browse the clipboard history by repeatedly typing:


Create a Numbered List

Say that you have a list of items that you want to convert into a numbered list:

1. first
2. second
3. third

There are many ways to achieve this. One is to use the Emacs Keyboard Macro Counter.

Position the cursor one line above your list and start registering a new macro:

C-x (

Insert a new counter value:

C-x C-k C-i.

A 0 will appear. Append a dot and a space:


Move the cursor to the next line and stop registering the macro:

C-x )

Select the list of items and apply the macro to the selected region:

C-x C-k r

Delete the 0 that you added at the beginning of the list and enjoy your brand new numbered list.