Alejandro Alcalde

Graduado en Ingeniería Informática en la ETSIIT, Granada.

More Alejandro Alcalde's posts

There is a bunch of post about cheatsheets in this blog:

Today CheatSheet is going to be about Emacs. But I want this cheatsheet to be different, my idea is that all of you using emacs help this cheatsheet grow with your own tips and favorite commands & packages.

I’ve been using emacs for a few months now and I’m pretty happy about it. It is very powerful. Now, lets start with the collaborative Emacs CheatSheet:

Useful packages


YaSnippet is a template system for emacs, it is a must have package for every emacs user. By default YaSnippet has a set of pre-defined snippets for practically every language, but it is possible to configure our own ones. For example:

Every blog post in this blog has a Frontmatter, one of its variables holds the last time the post was modified, in order to quickly write the current date, I’ve created the following YaSnippet:

# -*- mode: snippet -*-
# name: Modified
# key: mod
# # modified: 2016-$1-$2T$3:$4$0
# --
lastmod = "`(format-time-string "%Y-%m-%dT%H:%M:%S+01:00")`"$0

In the comments some metadata is written, such as the snippet name, and the key that is going to be associated with that snippet, mod in this case. So every time mod TAB is written, the snippet is expanded, writing something like this:

lastmod = "2017-03-08T16:58:45+01:00"

Here is another example:

# -*- mode: snippet -*-
# name: CodeBlock
# key: code
# --

This snippet expand to a Markdown code block, $1 is the first position where the cursor is placed to start writing, $2 the second and so on. Here is the result:

```<Cursor is placed here first>
<After writing the first time and pressing TAB, cursor is placed here>

Miscellaneous / Common tasks

Search & replace in multiple files at once


Search using a regular expression or a simple text in multiple files at once, or a entire directory and replace all the matches found.


  1. Run M-x find-name-dired and write the directory where files in which to look for matches are.
  2. Press t to “mark” all directory files.
  3. Press Q to run the command query-replace for every file marked.
  4. Write down the regex, for every file matches it is necessary to confirm the operation, in order to replace the contents in all the files at once, press A.

Source: Using Emacs to recursively find and replace in text files not already open

Rectangular selection


When wanting to select a region of text and perform an action in it, for example remove white spaces, add some text right before each sentence and so on.


Suppose the following text:

line 1
line 2
line 3
line 4

The desired text is:

- line 1
- line 2
- line 3
- line 4

In order to accomplish this, select the region and press C-x r t, emacs will ask the text to introduce, then press enter and that’s it. There are more operations for rectangular selection:

Source: GNU Emacs Manual

Replace a character with a new line


Imagine a bad formatted text, and in need to replace a character by a new line. This usually happens when reading files with different codifications in which the new line is interpreted differently (Windows and Linux). For example, in the below text we want to replace ^N with a new line:

Lorem ipsum dolor sit amet^N, consectetur adipiscing elit.^N Fusce vestibulum.


  1. Press M-x replace-string.
  2. Write the text to replace, in this case ^N.
  3. Insert the new line character, for this:
    1. C-q: In order to tell emacs we want to insert a raw character.
    2. C-j: This key combination represents a new line.
  4. Hit Enter and the resulting text is:
Lorem ipsum dolor sit amet
, consectetur adipiscing elit.
 Fusce vestibulum.

Source: How to replace a character with a newline in Emacs?

Delete trailing white spaces at the end of a line

Simply execute M-x delete-trailing-whitespace.

Save frequently used commands.

In a previous post this problem was explained in detail: Cómo crear comandos personalizados en Emacs.

Manage emacs backups


By default emacs saves a backup copy of the file being edited in the same directory, but ending in ~ , I find this very annoying. There is a way of telling emacs to save its backups in another directory.


Edit your ~/.emacs/init.el:

;; Set a directory for backup files
(setq backup-directory-alist `(("." . "~/.saves")))
(setq delete-old-versions t
    kept-new-versions 6
    kept-old-versions 2
    version-control t)

Source: How do I control how Emacs makes backup files?

Execute an action for all opened buffers


Some actions above, like searching & replacing in multiple files at once perform the action, but not save the changes in the files. In order to save the buffers to the files, ibuffer comes in handy.


To replace the default bufer by ibuffer add the following to emacs config:

;; make ibuffer the default
(global-set-key "\C-x\C-b" 'ibuffer)

For now on, every time the buffer window is opened, emacs will open ibuffer by default. To select all file buffers opened and save them:

  1. Press t to select all files.
  2. Press S to save all the marked buffers.

Source: Execute a particular command on multiple emacs buffers

Vertical align with the given character

Convert to uppercase/lowercase

Replace tabs by spaces and vice versa


Github’s prelude description:

Prelude is an enhanced Emacs 24 distribution that should make your experience with Emacs both more pleasant and more powerful.

It works as well on emacs 25 though, I am using this version in Gentoo and works well.

Prelude installation is very easy, all steps are described in its GitHub repo.

Contribute to this cheatsheet

If you want to contribute to this Emacs cheatsheet, you can add your useful packages and shortcuts via Pull Request.