Emacs Redux

Return to the Essence of Text Editing

Ace-jump-mode Is Dead, Long Live Avy

| Comments

People often ask how am I navigating efficiently Emacs windows and buffers. I have the feeling they expect me to share with them some secrets that would turbo-charge common commands like C-s, M-f, C-x o, etc. I don’t, however, use those commands that much. Ever since I saw that vim’s EasyMotion has been ported to Emacs, I’ve been using that port - namely ace-jump-mode.

Basically, it allows you to navigate to every visible portion of your Emacs (buffers & windows) with only a handful of keystrokes (usually one two activate it and one or two to get where you want to go). You can see it in action in this excellent video. ace-jump served me well for years, but I’ve had a few gripes with it that were never addressed (multi-char targets, CamelCase support, etc). I would have implemented those myself, if the project was maintained at all, but alas - that’s not the case. Seems I wasn’t the only one who was frustrated with ace-jump, as the prolific Oleh Krehel reimplemented it pretty much from scratch for the purposes of his excellent ace-window library. Once I got wind of this, I managed to persuade Oleh to start distributing his rewrite as a standalone project, which he dubbed avy.

Avy features everything ace-jump does and more. Apart from the many extra features, its codebase is way cleaner and readable and Oleh is a fantastic and very responsive maintainer. So, as far as I’m concerned ace-jump is now a dead project and pretty much everyone who’s using it should try out avy instead. Their usage and interface are pretty similar, so the learning curve is non-existing. By the way, here’s avy in action:

And what about my usage of avy? Nothing fancy here - I just bind the commands I consider most important to handy keystrokes.

1
2
3
(global-set-key (kbd "C-c j") 'avy-goto-word-or-subword-1)
(global-set-key (kbd "s-.") 'avy-goto-word-or-subword-1)
(global-set-key (kbd "s-w") 'ace-window)

avy-goto-word-or-subword-1 is aware of CamelCase words and I do a lot of programming in languages that use those extensively.

avy has one more thing going for it - it’s part of the default Emacs package repo GNU ELPA, which means that you can install it right away without having to setup any third-party repositories (which you’ll probably need sooner or later).

avy and ace-window are naturally part of Prelude.

P.S. Oleh, one of those days you should rename ace-window to avy-window. :-)

JDEE Reborn

| Comments

JDEE (Java Development Environment for Emacs) used to be best way to develop Java apps in Emacs a while back. It offered a ton of cool features, like smart auto-completion and a debugger. Unfortunately at some point the development slowed down significantly and the project went in a catatonic state. There was pretty much no activity for years in the official SourceForge repo and there was also no support for features introduced in Java 5+.

Recently, however, the project migrated to GitHub and it’s now way easier to get involved. Seems that some people have already started work on updating JDEE to support modern JDKs and modern Emacs functionality.

That’s a really exciting project, as Java support has traditionally been a weakness of Emacs and I’m writing this post with the hope that more people will help make JDEE great again.

So what are you waiting for? If you’re into Java and friends (Scala, Clojure, Groovy, etc), check out the new official repo and let’s get this party started!

Mastering Emacs (the First Emacs Book in Over a Decade) Is Out

| Comments

Mickey Petersen just released Mastering Emacs, the first new book about our beloved editor, since Learning GNU Emacs(released way back in 2004).

I haven’t had the time to read the book yet, but being familiar with Mickey’s work I have no doubt it’s outstanding. That’s all from me for now - go buy the book and start mastering Emacs.

P.S.

I hope we won’t have to wait another decade for the next great Emacs book.

Learning Emacs Lisp

| Comments

People who have been using Emacs for a while often develop the desire to learn Emacs Lisp, so they can customize Emacs more extensively, develop extra packages and create the ultimate editing experience, uniquely tailored to their needs & preferences.

There are a ton of Emacs Lisp resources our there, but most people generally need only one - the official Emacs Lisp manual. It’s bundled with Emacs and you can start reading right away by pressing C-h i m Elisp RET. If you’re relatively new to programming in general you might also check out the Introduction to Emacs Lisp (C-h i m Emacs Lisp Intro RET), before diving into the manual.

There are also plenty of Emacs Lisp tutorials online, but I’d advise against using them, as most of them have never been updated after originally published and Emacs Lisp keeps evolving all the time (albeit not as fast as I would have liked it to). That being said, Learn Emacs Lisp in 15 minutes is a short and sweet intro to the language. You can find more online educational resources on the EmacsWiki.

Trust me on this - any time invested in learning Emacs Lisp will be time well spent!

Pragmatic Emacs

| Comments

I recently noticed a new blog targeting beginner and intermediate Emacs users - Pragmatic Emacs by Ben Maughan.

If you’re relatively new to Emacs you should definitely check it out. Knowing how vast the Emacs ecosystem is, I’m pretty sure that more experienced Emacs users will also learn a thing or two there.

Building MELPA Packages Locally

| Comments

Don’t want to wait for the MELPA packages to be automatically rebuilt? Want to try out some package recipe before submitting it upstream? You can totally do this!

Just clone the MELPA github repo, open a recipe from it in Emacs and press C-c C-c (bound to the command package-build-current-recipe). Once the package is built you’ll be prompted to install it.

Pretty neat, right?

Emacs on OS X

| Comments

Prelude

In this article I’ll share with you a few tips and tricks about running Emacs under the Max OS X operating system. This article will focus on the vanilla GNU Emacs, but if you want a more native OS X experience you might have a look at the enhanced Emacs Mac port.

Installation

While Emacs is available for installation from various sources I recommend you to use the Emacs for Mac OS X binary distribution.

I always run the latest development version and I use homebrew to install it:

1
$ brew install emacs --HEAD --with-cocoa

Keep in mind there’s an ancient Emacs 22 that ships with OS X. You might want to alter your PATH, so that the new Emacs is picked up in a shell.

Alternatively you can just create an alias in your shell and when you invoke emacs it will run the newly installed version:

1
$ alias emacs="/Applications/Emacs.app/Contents/MacOS/Emacs -nw"

If you installed via Homebrew that path might look like this:

1
$ alias emacs="/usr/local/Cellar/emacs/24.5/Emacs.app/Contents/MacOS/Emacs -nw"

To make it permanent, if using bash, add that line to ~/.bash_profile. zsh users will want to update ~/.zshrc instead.

In case you’re wondering - nw tells Emacs to start in “terminal” mode (instead of in GUI mode).

Keybindings

I heartily recommend you to remap your Caps Lock key to Control. This can be easily done via Preferences -> Keyboard -> Modifier Keys. If you’re using a laptop keyboard or the bluetooth keyboard you might want to remap your right Option key to Control as well. No one can use effectively Emacs without a right Control key. Remapping it is a bit more involved and requires the use of the third-party utility Karabiner.

If you’re adventurous you might even try a crazier idea.

Setting the PATH variable

Long story short - if you’re running Emacs from Spotlight (or any other launcher for that matter) your PATH and exec-path variables won’t be same as the ones in your shell (and that’s every nasty since you want be able to run some external programs from Emacs). The best way to handle this would be installing the package exec-path-from-shell by Steve Purcell.

Flyspell

For flyspell to work correctly you’ll need to install aspell plus a few dictionaries.

1
$ brew install aspell --lang=en

Proced

The mighty proced doesn’t work on OS X. You can use vkill as a replacement. It’s kind of basic, but it mostly works.

Dired

OS X ships with BSD’s ls command which doesn’t have all the features of GNU ls (used internally by dired). Fortunately, this is easily solvable:

1
$ brew install coreutils

To avoid conflicts the GNU utils are prefixed with g, so ls becomes gls.

1
(setq insert-directory-program (executable-find "gls"))

Dash

Dash is the ultimately API documentation browser. I can’t live without it!

If you’re using it as well, you might want to install dash-at-point.

More goodies

If you want to spare yourself part of the headache of configuring Emacs on OSX and get a lot of extra firepower you might want to install Emacs Prelude - an enhanced Emacs 24.x configuration (developed by yours truly) that should make your experience with Emacs both more pleasant and more powerful.

P.S. I’d like to hear your tips & suggestions about making the Emacs experience on OS X nicer and more powerful!

Clear Comint Buffers

| Comments

comint provides Emacs infrastructure for building command interpreters. It’s backing a lot of popular shell/REPL modes - like eshell, inferior-lisp, inf-ruby, inf-clojure, etc.

comint provides a myriad of built-in commands, but somewhat surprisingly it doesn’t feature a command to clear the contents of a comint buffer. Let’s write one such command ourselves!

While there are several way to tackle it, I feel this is the simplest (and the one that makes the best use of comint’s existing functionality):

1
2
3
4
5
6
7
(defun comint-clear-buffer ()
  (interactive)
  (let ((comint-buffer-maximum-size 0))
    (comint-truncate-buffer)))

;; let's bind the new command to a keycombo
(define-key comint-mode-map "\C-c\M-o" #'comint-clear-buffer)

Simple and elegant, right? One day it might even end up being part of comint itself.

Customizing the Fringes

| Comments

On graphical displays, each Emacs window normally has narrow fringes (gutters/margins) on the left and right edges. The fringes are used to display symbols that provide information about the text in the window. You can type M-x fringe-mode to disable the fringes, or modify their width. This command affects fringes in all frames; to modify fringes on the selected frame only, use M-x set-fringe-style. You can make your changes to the fringes permanent by customizing the variable fringe-mode.

Out-of-the-box the most common use of the fringes is to indicate a continuation line. When one line of text is split into multiple screen lines, the left fringe shows a curving arrow for each screen line except the first, indicating that “this is not the real beginning”. The right fringe shows a curving arrow for each screen line except the last, indicating that “this is not the real end”. If the line’s direction is right-to-left, the meanings of the curving arrows in the fringes are swapped.

Third-party modes like flycheck and diff-hl also make use of the fringe to display valuable information there (e.g. lint and VC information).

By default both fringes have width 8 pixels, but we can easily adjust this:

1
2
3
4
5
6
7
8
;; make both fringes 4 pixels wide
(fringe-mode 4)

;; make the left fringe 4 pixels wide and the right disappear
(fringe-mode '(4 . 0))

;; restore the default sizes
(fringe-mode nil)

As mentioned before, you can also invoke this command interactively and determine the optimal fringe size for you, before making it permanent in your config. The options presented by the fring-mode command are defined in the fringe-styles list:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
(defconst fringe-styles
  '(("default" . nil)
    ("no-fringes" . 0)
    ("right-only" . (0 . nil))
    ("left-only" . (nil . 0))
    ("half-width" . (4 . 4))
    ("minimal" . (1 . 1)))
  "Alist mapping fringe mode names to fringe widths.
Each list element has the form (NAME . WIDTH), where NAME is a
mnemonic fringe mode name and WIDTH is one of the following:
- nil, which means the default width (8 pixels).
- a cons cell (LEFT . RIGHT), where LEFT and RIGHT are
  respectively the left and right fringe widths in pixels, or
  nil (meaning the default width).
- a single integer, which specifies the pixel widths of both
fringes.")

Be careful when playing with the fringe size. Certain info doesn’t look very good when the fringe is too small (e.g. less than 4 pixels).

Lossage

| Comments

Are you sure you’re using Emacs in an efficient way? Perhaps you’re overusing commands like C-f and C-b to navigate around when you can be using more efficient alternatives like M-f and M-b (or a third-party package like ace-jump-mode). Gaining such insight is somewhat hard, as we generally don’t pay attention to our sub-optimal usage patterns. If we could only see what were the last few hundred keystrokes we entered in our current Emacs session… Turns out we can do this with the aptly-named command view-lossage (C-h l) (lossage means malfunction). It displays the last 300 keystrokes and its output looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
n e r a l l y SPC d o n ' t SPC n o t i c e SPC o u
r SPC s u b o p t i m a l SPC s u a g <backspace> <backspace>
<backspace> <backspace> u s a g e SPC p a t e <backspace>
t e r n s . SPC I f SPC w e SPC c o u l d SPC o n l
y <return> s e e SPC w h a t SPC w e r e SPC t h e
SPC l a s t SPC f <backspace> h e w SPC <backspace>
<backspace> <backspace> <backspace> f e w SPC h u n
d r e d SPC k e y s t r o k e s SPC w e SPC i <backspace>
e n t e r e d SPC i n SPC o u r SPC c u r r e n t SPC
E m a c s SPC s e s s i o n . . . SPC T u r n s SPC
o u t SPC <backspace> SPC w e SPC c a n SPC d o SPC
t h i s <company-dummy-event> . <backspace> SPC w i
t h SPC t h e SPC m a c <backspace> g i c a l <backspace>
<backspace> <backspace> <backspace> <backspace> <backspace>
<backspace> <backspace> SPC c o m m a n d <return>
` v i e w - l o s s a g e ` SPC ( b o u n <backspace>
<backspace> <backspace> <backspace> ` C - h SPC l `
) . C-p C-p M-q C-n C-n C-n C-e C-x b l o s s <return>
C-x b l o s C-g C-h l

Inspecting it can be super enlightening! I do this all the time.