Maksym Prokopov personal blog
Idea is a something worth sharing

Emacs For Beginners

10.06.2024

The article provides overview of facts why Emacs is so different but also so powerful comparing to other editors. It gives a good starting point for the reader to understand the fundamentals and start own journey to effective work.

The way of learning is painful, but rewarding. The essense of Emacs is to codify your specific repetitive tasks so that eventually you realize, the editor contains a lot of customizations speeding up your daily routines, as no other editor does.

I do the following things in Emacs:

First of all, Emacs is a runtime environment with a set of named functions you can call any time. Use Meta-x key combination to spawn a list of “interactive” functions. There are more functions, but only those who marked as interactive supposed to be in this list.

Functions are written in a dialect of Lisp, so called emacs-lisp. If you want one sentence definition of lisp, here it is. First argument in a brackets is a function name, the rest is its arguments. Example

(+ 1 2)

In this example + is a function name, 1 and 2 are the arguments. The output is 3. Yeah, this is it. Emacs can execute a freshly written function on the fly.

Every time you hit a keystroke in Emacs you call a function. Even for a cursor movement. There is an easy way to find what function is bound to a keystroke by using C-h k followed by the keystroke. Remarkable feature is a documentation for any function is only C-h f away. This makes Emacs exploration a breeze.

An opened file is called a buffer. Depending on the file type, Emacs loads automatically a set of functions and key bindings to those functions. This is called a mode. Also how the content of the buffer is highlighted is defined in the “mode”. A set of functions, syntax highlighting and keybindings is called a major mode.

Whenever you switch between buffers, Emacs also switches the major mode depending on the selected buffer.

There are different modes for different purposes. Sometimes you want to share the same functionality across different types of buffers, so what you typically have in opened buffer is a mixture of different “modes”. A set of shared functions with keybindings is called a minor mode.

A package is a way to distribute such modes. Other editors call this a “plug-in”. There are repositories for the packages, such as MELPA. In case you want to install a new package, hit Meta-X and call the function list-packages-list.

Despite you can install packages and change settings manually, among the best practices is to keep Emacs configuration as a code. This is why you might find many “dotemacs“ repositories with configuration of other users in internet. It’s always a great source of insights of how other people use Emacs.

Typically users start their journey with popular packages and a standard or community-driven configuration, but with the time they recognize own repetitive tasks and create their own functions and assign them own keybindings. In this way they end up having an editor naturally fitting their workflows, saving significant amount of time every day.

Though, the learning curve is quite steep and there are good reasons for this. Emacs lisp is not so popular as Javascript or Python. The naming is always hard and is a something that is not easy to change over time. Other editors don’t use the same terminology as packages, buffers, modes, frames and windows. The syntax highlighting using regexps is a quite outdated technology nowadays. Even regexp library has a very special syntax from what users got used to. Another big issue is a combination of packages can be fragile, it’s a very common case when a package after update can break functionality of other packages.

Even key modifiers are named differently from other editors: Meta, Hyper, Super. My personal take on default keybindings - they’re aweful and RSI prone. Once you realize the Control key supposed to be a key closest to the spacebar, as it was designed for Space Cadet keyboards (it had also Meta, Hyper and Super keys), it became obvious how to make Emacs usage much more wrist friendly. Just do the same on your keyboard, map the control key to whatever key you have closest to the spacebar, in my case it’s cmd on mac keyboard.

Summary

In essence Emacs is an editing environment with set of functions, variables, syntax highlighting and keybindings.

Bare Emacs is not so feature reach, but packages ecosystem provides a good foundation for any type of everyday tasks and some of those are unmatched in another editors.

The real power comes after user learns enough fundamentals to start customizing Emacs to perform repetitive tasks faster.

Though, the learning curve can be steep for different reasons and it’s strongly advised to change default keybindings to improve ergonomic.

Emacs, apple keyboard, and RSI

07.06.2022

Recently I’ve started investigation on the most effective shortcuts for Emacs. Already for a long time I’ve been using Caps Lock remapped to Esc when pressed alone, and Ctrl-Key when pressed with any other key.

I didn’t use Emacs with native bindings for a long time, because of wrist related issues, which immediately appeared after using pinky for pressing long chords which normally included Ctrl-C combination. This is why I used Spacemacs and later Doom Emacs as the configuration of choice.

And this worked relatively good, I used the same bindings for Vim in terminal, and no RSI since pinky wasn’t overloaded with long presses. But something were not so good, I needed to keep a context in the head and using special modes like eshell or repl weren’t fun because of context switch losses.

So I’ve started reading the book Mastering Emacs, which I definitely recommend, I decided to give a try to alternative solutions for Emacs keybindings.

First of which were using interesting combinations for the keychords like pressing J+K equals to Control and pressing F+D equals to control. The idea was to reduce pressure on pinky by moving control to the “home” row. This worked to some extent. Long combinations with Ctrl, like “C-c C-o C-r” needed to have very uncomfortable key sequences. Apart from this constant typos where annoying, when you switched keys too fast. So I dropped this option and started looking for new ways.

I realised, that Ctrl should not be pressed by pinky, it should be just convenient enough to reach. Then I saw old design of the Space Kadet keyboard, and it had Control right near the Space key.

Space Cadet keyboard

Whan I swapped Command and Control keys, man, that was real enlightenment, literally everything started being comfortable. Look, Ctrl-x-s is just a single row. Ctrl-c feels natural, because you literally use mighty thumb and index fingers. Movement with Ctrl-a, Ctrl-e, Ctrl-n and Ctrl-p just started having much more sence and convenience. Most of the Emacs combinations with Ctrl-C and Ctrl-X are sane now.

It comes as a bonus using readline compatible utilities in terminal.

I really think swapping Caps Lock and Control is a BAD advice that did enourmous harm for neglecting Emacs than anything else. Either Windows based keyboards having Alt, or Mac based keyboards having Command, should be swapped with Control at least in Emacs and other readline applications. Do this right now using Karabiner Elements free application for MacOS and never experience any RSI.

Using Karabiner Elements you can make this change only for the several applications, like Emacs and iTerm, and not the system-wide.

Update:

In order to remap command to control you don’t event need Karabiner Element, just map command to control using the following snippet

(setq ns-modifier-command 'control)

In iTerm it’s also easy to do, open

Preferences -> Keys -> Remap modifiers

and change the settings. One more trick is how to switch between apps from iTerm using Cmd+Tab when it’s remapped?

Here is how to do this https://gitlab.com/gnachman/iterm2/-/issues/6394

You can add exceptions to keyboard remapping, but it’s not very intuitive. Add a key mapping to Prefs > Keys. First, select the action Do Not Remap Modifiers. Then set the shortcut to Cmd-Tab (by pressing the physical cmd key plus tab).

RSI stands for repetitive stain injury https://www.nhs.uk/conditions/repetitive-strain-injury-rsi/

Disable Chrome browser confirmation to open external protocol with org-protocol

02.02.2021

I recently added org-capture chrome extension to my toolbelt. Unfortunalely chrome annoys me by asking if I want to open this protocol link every time.

Here is the simple way to fix this.

Execute this in the shell

defaults write com.google.Chrome URLAllowlist -array-add -string 'org-protocol://*'

check chrome://policy url that it contains proper settings for org-protocol

Chrome policy