Emacs

basics

Getting help

Goal Key binding
Emacs manual C-h r
Emacs tutorial C-h t
Browse manuals C-h i
Describe variable C-h v

Activating modes

To activate a (minor/major) mode in the current buffer, type

M-x <name of mode> RET

e.g.

M-x hs-minor-mode RET

String replacement

To replace certain string in whole document from cursor onwards:

M-x replace-string RETURN string-to-be-replaced RETURN string-to-replace-with RETURN

To replace certain string on a one by one basis from cursor onwards:

ESC % string-to-be-replaced RETURN string-to-replace-with RETURN

then

  • y --> yes
  • n --> no
  • q --> quit
  • . --> replace current instance and quit
  • ! --> replace all the rest and don't ask

Indentation

To indent a region, mark it and run

C-u <number of spaces to indent> C-x TAB

Fonts

To temporarily enlargen/reduce the font size in Emacs buffers you can use C-x C-+ and C-x C--, respectively.

To set the font size permanently, you can put e.g.

(set-frame-font "Inconsolata 12" nil t)

or

(set-face-attribute 'default nil :height 100)

into your Emacs configuration file.

Insert characters

If you want to force Emacs to accept a character as is (and not its special meaning in a specific context), use C-q <your_char> (quoted-insert).

Display special characters

To show whitespaces and tabs, use M-x whitespace-mode.

To show glyphless chars, read the Glyphless Character Display and Text Display node in the Emacs manual.

configuration

Emacs is configured via one an initialization file. Typically it is located at ~/.emacs.d/init.el.

The configuration is written in Emacs Lisp. As such, comments in the config file are marked with ;;.

One can split the Emacs configuration into multiple files for improved clarity like in this example Emacs config repo.

org mode

One of the most powerful Emacs packages out there.

useful commands

Goal Key binding Command
fold/unfold TAB
cycle through TODO states C-c C-t
assign priorities C-c C-,
add notes C-c C-z
schedule tasks C-c C-s
insert link C-c C-l (org-insert-link)
store link C-c l (org-store-link)
open link C-c C-o (org-open-at-point)
open full agenda file C-c a t
open agenda for current week C-c a a
open agenda for current week and enable log mode C-c a a l
show sparse todo tree for current document C-c / t
turning on column view C-c C-x C-c (org-columns)
remove overlay / exit column view C-c C-c
add property C-c C-x p (org-set-property)
delete property C-c C-x d (org-delete-property)
add unique ID (to files or headings) (org-id-get-create)
add tags C-c C-q (org-set-tags-command)
promote current subtree by one level M-S-LEFT (org-promote-subtree)
demote current subtree by one level M-S-RIGHT (org-demote-subtree)
move subtree up M-UP (org-move-subtree-up)
move subtree down M-DOWN (org-move-subtree-down)
clock in C-c C-x C-i (org-clock-in)
clock out C-c C-x C-o (org-clock-out
reevaluate time range after changing time manually C-c C-c (org-evaluate-time-range)
update org clocktable reports (on #+BEGIN line) C-c C-c (org-dblock-update)

More on structure editing and clocking commands.

syntax

[[link][description]] - link syntax

exports

org mode files to ...

  • pdf via LaTeX: C-c C-e l o

more on org mode

Dired

The Directory Editor. Allows for file system interactions from within an Emacs buffer other than the shell.

Deleting files

Enter Dired with M-x dired or C-x d, mark the files for deletion with d (use u to unmark), then execute the deletion with x. Dired will ask you for another confirmation before deleting.

more on Dired

markdown mode

goal key bindings command
insert table C-c C-s t (markdown-insert-table)

more on markdown mode

bibtex mode

A builtin major mode to edit bibtex files.

more on bibtex mode

ledger mode

The emacs mode to work on ledger files and use ledger-cli utilities.

goal key bindings command
access manual C-h i
add new entry in .ledger C-c C-a
create report C-c C-o C-r + report name

more on ledger mode

email with isync, msmtp, and notmuch

notmuch under the hood

While notmuch is a separate program (to index, search and tag emails), I bundle its here because I usually interact with it through the Emacs interface.

notmuch by default does not delete email ever. Instead it typically drops mails with the "deleted" tag from the search. If you really want those files removed from disk, you can run:

notmuch search --output=files tag:deleted | xargs -l rm

Rerun notmuch new afterwards to see these changes be reflected in the notmuch database.

basics

goal command key binding
attach file M-x mml-attach-file C-c C-a

PGP integration

goal command key binding
sign message M-x mml-secure-sign-pgpmime C-c C-m s p
encrypt message M-x mml-secure-encrypt-pgpmime C-c C-m c p

more on email with notmuch

git with magit

goal key bindings command
get git status info and more C-x g
open magit helper popup ?
close commit message C-c C-c

more on magit

gopher / gemini with elpher

IRC with erc

Emacs comes with 2 IRC clients. erc is the more fully-featured but also more complex one, while rcirc is the simpler one.

For details on Internet Relay Chat as a chat system, see here.

Default key bindings

goal key bindings command
join channel C-c C-j (erc-join-channel)
leave channel C-c C-p (erc-part-from-channel)
quit server C-c C-q (erc-quit-server)

more on erc

programming

general

goal key binding command
(un)comment code block M-;
fold all code blocks (requires hs-minor-mode) C-c @ C-M-h
fold current block (requires hs-minor-mode) C-c @ C-h
unfold current block (requires hs-minor-mode) C-c @ C-s

R

Use the Emacs Speaks Statistics package.

Python

Go

Java

Use lsp-java instead of the no longer maintained eclim.

HTML

To pretty format HTML run sgml-pretty-print followed by indent-for-tab.

see also