The web site of Vas — 2019-10-10

Emacs for writing, 2019 edition

I used to have some tips and tricks for writing in Emacs, but that article no longer exists. I spent the first 6 months of this year giving Atom and VS Code a serious try for coding and writing, and after that I took a short break and returned to vim/neovim. Now I’m back to Emacs, with an entirely new configuration, and these are hacks I use every day to make writing easier. This isn’t an Emacs tutorial. init.el here

Should I use Emacs?

If you use something else and are considering switching to Emacs, here’s the reasons why you’d want that, coming from a person with years of experience with all of the following:

You probably won’t like it if:

Ignore default Emacs key bindings

The only reason Emacs key bindings are what they are is because changing the defaults would break existing configurations. There’s cheatsheets available for Emacs that immitate the cheatsheets for vim; these are useless and go against the spirit of Emacs. Ignore everything and set it to what you’re comfortable with, for example Ctrl-S to save the current file. Don’t care about what makes sense for other people. Use what makes sense for you.

You can set a key binding with the following template:

(define-key the-name-of-a-map (kbd "command-string") 'name-of-function)

The default map for all buffers is appropriately named global-map, and each mode has its own map with additional bindings and behaviour as needed. These can override each other, the more specific map takes precedence. Other maps you might find important are text-mode-map, prog-mode-map, and, if you install markdown-mode, markdown-mode-map.

Navigation

The default navigation with C-n and C-pis dumb. I’ve replaced them with M-h, M-l, M-j, and M-k. You can also use the arrow keys if you want, but that’s a habit I’ve picked up from using vim a lot and I’ve found it useful. I’ve also set M-tab to flip between the current buffer and the one I used previously with mode-line-other-buffer. I use C-h and C-l for previous and next buffer respectively, and if I have lots of buffers, I have C-tab bound to switch-to-tab, which autocompletes buffer names.

I also use similar HJKL bindings for going to the next/previous word, next/previous paragraph and so on.

Abbreviations

Through markdown-mode-hook, I enable abbrev-mode when writing in markdown and only markdown. I’ve written more about the subject in a separate article and it’s saved me over ten thousuand keystrokes so far. It hasn’t even been a full month since I’ve started using it, so that’s impressive. Of note is also dabbrev-expand which I’ve bound to C-e, because it’s easy to reach. Dynamic abbreviations are automatically calculated based on the text of the buffer, so if you type “Jonathan” and then “JonC-e”, it will expand to “Jonathan”.

This is mainly useful for names or terminology you know you’ll only be using in the current document, for example while you’re writing Game of Thrones fic.

macOS-like spotlight

This is less Emacs-specific, but macOS has a feature were you press a key and type the first few letters of the file you’re looking for and it shows you search results almost instantaneously. That way you don’t have to navigate to the file every time you want to resume working on something.

First I search for files and store them in a compressed format.


#!/bin/dash IFS='
'
DIRS="$HOME/Desktop
$HOME/Projects"
file="$HOME/filedb.xz"
rm -v $file
for dir in $DIRS
do
	fd -c never -t f -- . $dir | sort | xz >> $file
done
echo "updated $file"

Then I can pipe the file to ido

(defun filedb-open() (interactive) (find-file
	(ido-completing-read "select a file > "
	(split-newlines
	(shell-command-to-string "xzcat ~/filedb.xz")))))

I’ve bound it to C-S-O in Emacs. For example I’m currently working on a fic about unicorns, so I just need to type unic and it’ll be the first result. Because the file is only 41KB compressed, it’s incredibly fast to parse.

VSCode-like shell

VSCode has a built-in shell, and you can open and close it with C-backtick. Emacs has a built-into shell too (three, actually), and I wanted to replicate that functionality. I set C-backtick to launch eshell, and then in eshell-mode-map I set the same binding to switch to the previous buffer, so it feels like the same binding opens and closes the shell.

Ghetto vi mode

If you want to do a lot of navigation, it can get tiresome having to press modifier keys before movement, and find yourself missing proper vi. You can install evil-mode, but because I don’t want the full functionality, I just made my own vi mode, which for example binds HJKL not to print characters, but to move the cursor, and dd kills the current line, et cetera.

This isn’t proper vi, but it’s 99% of what I want from vi, so it works much better for me.

References

Sometimes I have picture references I want to look at, for example a map of an area, and I store them in a ref/ directory in the project I’m currently working at. Because it was getting tiresome navigating to the folder and launching sxiv from there, I’ve bound C-c r e f to do it for me automatically.


(defun sxiv-ref() (interactive) (spawn "sxiv" "-b" "ref/"))

(defun spawn(program &rest args)
	(apply 'start-process
		(append (list program program program) args)))

Theming

emacs screenshot

I use the default theme, but with my own customisations. I’ve removed all toolbars and scrollbars, and I’ve even removed the modeline. The only user interface I’ve left visible that isn’t the document I’m currently working at is the echo/command minibuffer, which is one line tall so it doesn’t bother me much. For colours I use the solarised palette, but with fewer colours of the rainbow; the reasoning is explained by this guy. Basically I find too many colours distracting, so I only colour very specific things, like strings, links, and emphasis.

I display emphasised (<em>) text not as italics, but in red, a nod to rubrication. I do this so it’s easier to spot using too much emphasis in a paragraph.

Other writing adjustments

I have bound C-i to emphasise text, and C-b to strongly emphasise it, like Word does it. I use C-c c m to run the current buffer through cmark, which is the markdown implementation that I prefer.

I use C-j to capitalise the word I’ve just typed, so that I don’t have to go back and fix it with navigation commands.

Paragraphs in markdown are separated by double newlines. I got tired of pressing the enter key twice, so I bound <return> to insert two newlines instead of one.

I use simple lists to note down ideas I’d like to include in a work. I use C-c o k to mark that I have followed through and written that idea, by inserting a checkmark ✔ at the beginning. Likewise, there’s the C-c n o binding, which inserts a cross ✘. These work no matter where I am in the line.

I count words with C-c w c.

I prefer writing with proportional fonts, so I automatically turn on variable-pitch-mode in markdown-mode and I use a serif font in it. I also scale the text up a little bit, and I increase the line spacing to look more like a text editor.

Environment

I use the dwm tiling window manager, which handles window placement and sizing for me. I have various patches installed, but since getting an ultrawide monitor, I find the centeredmaster patch very useful. I use the centeredfloatingmaster layout, with Emacs in the centre, and one or two reference windows behind it, like Firefox with a thesaurus open. I used to have specialised key bindings for looking things up in the thesaurus, but in my experience, this is faster.

I use sxhkd for launching programs, and I have bound emacsclient to super + F3. I use a master Emacs daemon at startup, so that emacsclient starts instantaneously, faster than my terminal, vim, or anything else.

I use a mechanical keyboard for increased key travel and key distance, which makes accurate typing easier. This is especially valuable with abbrev-mode, which favours accuracy and finesse over raw speed. If you are interested in one, there should be major sales on aliexpress this holiday season. If you can, snatch a keyboard with kailh switches with clickbar; they’re fan favourites for a reason.

I use git to manage past records of the things I’m writing. I don’t use magit in Emacs. I have my own helper script with the things I most commonly want.