The Great Emacsification of 2013

First, Some Backstory

When I first started getting serious about Unix around the turn of the century I got really hard into Vim. In 2003 I had already had my first glimpse of RSI so I switched to the Dvorak keyboard layout and bought myself a beautiful Kinesis Ergo keyboard.

For some reason I decided this would be a good time to switch to Emacs, so I did. I never really achieved the fluence I had with Vim after switching from Emacs (though I did lose what I had with Vim). So I've slowly grown to resent Emacs over the years, and I made a point of trying out new editors pretty frequently.

Most recently this culminated in trying out Textadept.

Let me take a break here to make something really clear.

Textadept Rules

No, seriously. It does. Everyone should try it. It has a console mode, sensible modern keybindings, and it's scripted in freaking Lua. It's really awesome. Use it. Love it.

So it's a real shame that I continue this post by telling you how I set up Emacs and how amazing it is, finally, after basically a decade of ignoring it, to actually use it.

Emacs Basics

My life with Emacs utterly sucked until I found out about the following settings. I'm going to explain them as I go.

If you don't set this Emacs may think you're some kind of ISO-9660-1 jackass.

(prefer-coding-system 'utf-8)

While learning Emacs, it's going to beep at you a lot. This is infuriating. Disable the bell.

(setq visible-bell t)

Emacs by default thinks that you use the computer as if you emerged from some kind of 1970s typing class and your instructor took points off if you put a single space after the period. But it's 2013, and nobody does this, and nobody should, because everything that matters either ignores the extra space (HTML) or typesets properly despite it (TeX). So fix it.

(setq sentence-end-double-space nil)

Impossibly, it will tell you the current line but not the current column by default. Fix it.

(setq column-number-mode t)

Emacs loves to tell you that it is Emacs and how proud you should feel for using it, so you actually have to modify bunches of settings to really make it clear to Emacs that you just don't care about it or its feelings.

(setq inhibit-startup-message 't)
(setq initial-scratch-message "")
(setq inhibit-splash-screen t)

Emacs defaults, in a depressingly predictable manner, to Lisp mode, as if that's something you give a shit about. Let's make it sensible:

(setq default-major-mode 'text-mode)
(setq initial-major-mode 'text-mode)

Of course you could put a language mode in there if you mostly edit a particular language, but I find I usually have no idea what I'm up to when I start it up so text mode is more honest. Fundamental mode is even more useless than Lisp mode to me.

Now let's disable those hideous backup files.

(setq make-backup-files nil)

Emacs by default is configured to be some kind of idiot savant about indentation, switching between tabs and spaces as necessary to achieve the layout of your dreams. In practice nobody wants this ever, so shut it down:

(setq indent-tabs-mode nil)
(setq tab-width 4)

Now global key bindings are a treacherous territory but I think this one is completely obvious. Every other editor on earth proudly advertises that it will indent properly by doing it after a newline, so Emacs should too:

(global-set-key (kbd "RET") 'newline-and-indent)

Finally, because Emacs has more keybindings than you can ever hope to master, let it tell you when it has a quicker way to do something than the M-x long-ass-name abomination you just used:

(setq suggest-key-bindings t)

A setup trick

So it happens that I have a fairly complex configuration now, but most of my settings are either host-specific or mode-specific. My ~/.emacs.d/init.el is actually very short:

;; load all my initialization files
(dolist (file (file-expand-wildcards "~/.emacs.d/init/*.el"))
  (load file))

;; load custom per-host files
(let ((host-file (format "~/.emacs.d/hosts/%s.el" system-name)))
  (setq custom-file host-file)
  (if (file-exists-p host-file)
      (load host-file)))

This file is really establishing two naming conventions: one for all my mode-specific crap and one for all my host-specific crap. The custom-file trick there ensures that if I make the mistake of using M-x customize the changes wind up cordoned off in the per-host file.

I'm sure other people have equally clever tricks, but this lets me use one repository for one Emacs configuration that varies slightly on each host. This is super handy because normally I'd make a small change to, say, the work configuration and then lose it on the way home or, possibly worse, go home and manually re-apply it elsewhere, slightly differently. Now by simply choosing the right file I can shared things that should be shared or I can sequester changes off in the per-host file that I don't want shared.

Another quick tip

Hey! Refuse to use Emacs < 24.3. Just say no. It's bad enough you're learning Emacs, dealing with lots of different Emacses would be even worse.

Packages and Themes

Be sure to use the new package system for your own sanity:

(package-initialize)

This lets you install stuff directly from Emacs. Give it a shot:

M-x package-install RET auto-complete RET

Tricked you! You just installed magic Emacs tab completion! Now add this to your ~/.emacs.d/init/autocomp.el file, or whatever:

(add-hook 'after-init-hook
  (lambda ()
    (require 'auto-complete-config)
    (ac-config-default)))

Restart Emacs and enjoy the fact that you now have freakin' tab completion in there.

Oh, and get you a proper theme: M-x load-theme RET misterioso. On the house. Or you could go get solarized with package-install.

mu4e

Your next question is going to be, but Dan, why should I use Emacs if it isn't even a decent MUA?

Well my friend, it actually is a great MUA, using the elaborately-named mu4e. This mailer has the #1 most-needed feature of any mailer, which is that it is search-based. I could write at great length about how nice it has been, using mu4e, but you should just try it out yourself. It's really nice. Tab completes addresses, ido-like folder choosing, and fast as hell. Emacs 24+ can connect directly to your SMTP host, so there's no excuse there. Set up offlineimap to get your mail and get on with your day.

This can't really be why you switched

No, and it isn't. Unfortunately, nxml-mode is. It's really fantastic at editing XML. I'm sure you didn't want to hear that. I recently had need for editing some Docbook. Docbook is a ferocious XML format designed to kill your hands. However, Emacs makes it a lot more tolerable with nxml-mode, because while you're working on your XML file it's constantly telling you just how invalid your document is. And it has some shortcuts for inserting partial tags and completing tags. And since it understands RelaxNG, it also will make autocomplete suggestions for you.

It's worth noting that it really does understand RelaxNG. You can design your own schema from scratch and it will give you context-sensitive completions.

Misc

I'm just starting to use org-mode so I can't give you a proper endorsement there, but suffice to say, it's really handy to be able to make links to email messages and files on your filesystem to embed in your todo list.

Conclusion

So there you have it.

Note that this is not an endorsement of Emacs. Please go on using Vim or Textadept as you please.