Table Of Contents
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
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
# --
\`\`\`$1
$2
\`\`\`
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
Problem:
Search using a regular expression or a simple text in multiple files at once, or a entire directory and replace all the matches found.
Solution:
- Run
M-x find-name-dired
and write the directory where files in which to look for matches are. - Press
t
to “mark” all directory files. - Press
Q
to run the commandquery-replace
for every file marked. - 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
Problem:
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.
Solution:
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:
C-x r k
: Kill the text of the region-rectangle, saving its contents as the “last killed rectangle” (kill-rectangle).C-x r d
: Delete the text of the region-rectangle (delete-rectangle).C-x r y
: Yank the last killed rectangle with its upper left corner at point (yank-rectangle).C-x r o
: Insert blank space to fill the space of the region-rectangle (open-rectangle). This pushes the previous contents of the region-rectangle rightward.M-x clear-rectangle
: Clear the region-rectangle by replacing its contents with spaces.M-x delete-whitespace-rectangle
: Delete whitespace in each of the lines on the specified rectangle, starting from the left edge column of the rectangle.C-x r t string RET
: Replace rectangle contents with string on each line. (string-rectangle).M-x string-insert-rectangle RET string RET
: Insert string on each line of the rectangle.
Source: GNU Emacs Manual
Replace a character with a new line
Problem:
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.
Solution:
- Press
M-x replace-string
. - Write the text to replace, in this case
^N
. - Insert the new line character, for this:
C-q
: In order to tell emacs we want to insert a raw character.C-j
: This key combination represents a new line.
- 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
Problem:
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.
Solution:
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
Problem:
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.
Solution:
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:
- Press
t
to select all files. - Press
S
to save all the marked buffers.
Source: Execute a particular command on multiple emacs buffers
Vertical align with the given character
M-x align-regex =
Convert to uppercase/lowercase
C-x C-u
: Convert the selected region to uppercase.C-x C-l
: Convert the selected region to lowercase.M-l
: Convert the next word to lowercase.M-u
: Convert the next word to uppercase.M-c
: Capitalize the next word.
Replace tabs by spaces and vice versa
M-x tabify
: Replace spaces by tabs in the selected region.M-x untabify
: Above inverse, replace all tabs by spaces.Source: mdk.fr
Prelude
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.
Spot a typo?: Help me fix it by contacting me or commenting below!