Emacs Tips & Tricks
The Great Radar Oven's Emacs Advice

Table of Contents

Site Map

Easy KMacro Binding

Keyboard macros (or kmacros) are a powerful way of automating text editing according to common sequences of commands. This rapidly speeds up repetitive tasks. Start a kmacro with C-x ( and stop with C-x ).

Emacs uses its kmacros in a stack similar to the kill-ring. The last macro is always C-x e. You can access macros produced before the current one with C-x C-k C-p.

But when you are using a lot of macros, this feels slow and clunky. It makes much more sense to bind the macro to a convenient key. I like the Vim method, which has the user choose the key at the start of the macro recording.

While default Emacs does not offer this, you can easily map the last macro with kmacro-bind-to-key, the binding being C-x C-k b. I remap this to M-/. A general kmacro convention that I have found helpful is to reserve F5-F8 for kmacros only.

Store Buffer Layout

Registers are an Emacs feature that lets the user store small bits of data for later use, addressed to keys. It gets interesting when you realise you can store more than just text in registers.

Emacs allows you to organise buffers in virtual windows. Some commands spawn a buffer, messing up your layout, which can get annoying if you really need a certain layout. It is also slow to switch to another common layout.

emacs_buffer_layout.png

Figure 1: This is a buffer layout that I often use.

I just <2018-03-23 Fri> found out about the ability to store buffer layouts in Emacs registers. This means that you can jump to a layout, or switch to a different one without having to adjust the windows manually.

Set a buffer-window layout to a register with C-x r w <KEY> and jump to it with C-x r j <KEY>.

Custom Mode-Line

The mode-line is the line displayed at the bottom of each Emacs buffer that displays information to the user like the current buffer name, the encoding of the file, the active frame, and, of course, the active modes.

I am not a fan of the default mode-line. I believe it to both confusing for newbies, but with an annoying lack of concision for slightly more advanced users. The answer to which is to make a custom mode-line. This is easier than you may think, as the mode-line has custom variables, and %-constructs, as well as being a customise-able variable. For more information, describe the variable (C-h v) mode-line-format.

My init.el file reads:

(setq display-time-mail-file 0)
(setq display-time-default-load-average nil)
(setq display-time-24hr-format t)
(display-time-mode 1)
(column-number-mode 1)
(setq-default mode-line-format
              (list "%e%@%F%Z%*%* "
                    mode-line-buffer-identification
                    " {%I}[%c,%l]"
                    mode-line-modes
                    display-time-string
                    " %-"))

Giving an example mode-line of:

-F1UU-:** emacs.org    {7.0k}[104,32](Org => Wg Fly) 13:36 --------

If you are new to Emacs and want a less complex mode-line then the Emacs Wiki has a page about simple mode-line configuration.

That gives an example mode line of:

Lisp Interaction: buffer *scratch*, line 5 -- user: alice

Modes

I keep a described list of some modes that I use on the Applications page.

Make Alt-Gr Be Alt (on Linux)

…Or less concisely: ISO Keyboard Layout; Map Alt to Alt-Gr (Linux Command Line) for Emacs Use

Emacs maps meta to the alt key as a modifier. The American ANSI layout has an alt key for each thumb, making it easy to reach any meta+key combination. Unfortunately, ISO layout keyboards (such as the UK layout) have one alt key and one alt-gr key.

Personally, I rarely if ever use the alt-gr key and would much rather save myself the thumb-strain. As I use the command line Emacs, this article will not include any X.Org settings.

Simply put, Linux looks up the keyboard event code in a keymap table and outputs the relevant character. Therefore, we can edit this file to output the alt key instead of alt-gr. This is less difficult than you might think; Linux wants you to hack around.

# Copy the file to avoid breaking the default keymap:
$ cp /usr/share/kbd/keymaps/ARCHITECTURE/LAYOUT/COUNTRY.map.gz ~
# E.g. ~/usr/.../i368/qwerty/uk.map.gz~
# Rename it:
$ mv uk.map.gz my.map.gz
# Open it in Emacs:
$ emacs my.map.gz 

Change the line: include "linux-with-alt-and-altgr" to include "linux-with-two-alt-keys"

Save my.map (C-x-s) and copy it back into /usr/share/kbd/keymaps/i386/qwerty/.

Now to edit the startup settings file. Note: This file changes slightly between distros, Void Linux uses /etc/rc.conf. Un-comment the keymap line and add the name of your keymap without "map.gz"

It should now read: KEYMAP=my

Upon reboot your alt-gr key should now register as alt.

Efficient Movement

Emacs' default navigation keybindings are a tad awkward (for beginners especially), often people revert to using a Vi emulator for ease of use, or having large amounts of custom keybindings.

However, this creates bad habits by encouraging the use of the incremental navigation keys for large-scale movement. This makes editing and movement in the buffer slow and imprecise. Ideally, you should only use them for moving a few characters at a time.

How should one go about boycotting C-n, C-p, C-f, C-b?

Efficient Use of Other Navigation Commands

For example, rather than holding down C-b for long periods, it is much faster to C-a to the start of the line and C-f forward a couple of spaces.

A particularly useful example is that M-e/a moves to the end or start of a paragraph, often saving multiple C-b/f and C-n/p keystrokes.

Mode-Specific Keybindings.

Some modes have specific shortcuts for travelling around the document quickly.

For example, Org-mode allows for jumping between headings by prefixing the movement keys with C-c. It also has a buffer-oriented movement system using C-c-j.

Search & I-search

Simply using the default I-search command can allow easy movement within a document. To access standard search (move straight to next occurrence), use C-s/r (I-search) and press enter before typing your search parameter.

I tend to use the standard search for moving to places which are already on screen (rather, I used to) and I-search for moving to areas not visible.

C-u (& Linum-Relative-mode)

This is cheating a little bit since you are technically typing the navigation keys multiple times, you are just getting Emacs to do it for you automatically.

Pressing C-u, a number, and then the navigation command can be a hassle. Instead, just press meta and a number for faster use.

The trouble is that, even with line numbers in the margin, it is usually a guess as to how much to move. Linum-Relative-mode solves this by showing the amount of lines away from the current line in the margin, making it easy to see how many lines to jump. Note that this is not a default package and you will need to install this.

Avy

This method is by far my favourite; it not only allows jumping within the visible areas of the buffer, but also within visible areas of the whole window. I also use this method for jumping easily between visible buffers, which is faster than using C-x-o repeatedly.

To install Avy, use package-install avy. If Avy is not to your liking, then a similar package is Ace-Jump-mode, but it requires a bit more setup.

Note that Avy has much more functionality beyond what I mention here. This is just the basics.

Open a buffer with lots of text in it and use the command avy-goto-char (with M-x). The minibuffer should prompt with "char:". Find a character to jump to and type it. If you have multiples of this character on screen, highlighted letters will appear wherever that character appears. Type the identifier character(s) at your intended destination and Avy will jump your cursor to that point.

Personally, I found I was typing too many identifier characters when jumping to a single character. Luckily Avy has avy-goto-char-2 which will jump to a pair of characters.

Now that we have acquainted ourselves with Avy, I think that it would be best to bind it to a shortcut for ease of use. My key of preference is C-z.

Chuck (global-set-key (kbd "C-z") #'avy-goto-char-2) into your .emacs file and reload Emacs.