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:
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
Stop recording the macro:
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.
Say that you want to reverse the order of the parameter for the following function:
foo(Second, First) -> ok.
Position the cursor between the words
First. Then, press:
You should obtain the following:
foo(First, Second) -> ok.
Given the following two lines:
Second Line First Line
Put the cursor at the beginning of
First Line and press:
You should get:
First Line Second Line
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:
C-x v v
- Write a meaningful change comment
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:
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:
first second third
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:
Insert a new counter value:
C-x C-k C-i.
0 will appear. Append a dot and a space:
Move the cursor to the next line and stop registering the macro:
Select the list of items and apply the macro to the selected region:
C-x C-k r
0 that you added at the beginning of the list and enjoy your brand new numbered list.