Why Learn Emacs

I taught myself to program as a teenager by learning Basic and 6502 assembly language. By the time I got to college, I had already been programming C professionally. I then studied Lisp, Scheme and Prolog in school, but used C and C++ and eventually Java and Scala in the industry.

My first job assumed the “Waterfall Approach” to software development, and our industry debated the merits and potential of “Object Oriented Programming”. Telling that for the last 25 years, Emacs has been the only constant in my life as a software engineer.1

During a screen sharing session, a colleague asked about that editor I was using. He then stunned me when he said:

Emacs, huh? That’s the editor all the hipsters are now using.

Emacs is positively ancient2, but many younger programmers have found Emacs quite useful…especially when editing hip kid-languages like Python and Clojure. Also, they’ve extended the system with some fantastic new ideas that I’ve been enjoying lately.

Why would anyone be interested in using Emacs? Let me list a few advantages of Emacs. Keep in mind, that even if you choose another editor, it really should have these features.

Editor Crafting

I’m stunned that you, as a professional software engineer, would eschew inferior computer languages that hinder your ability to craft code, but you put up with editors that bind your fingers to someone else’s accepted practice.

Seriously, you should have an editor that is trivial to customize to your own idiosyncracies and utter dementia. In Emacs’ case, you just write a wee bit of Lisp, which might be good for you to learn anyway.

While typing this essay, I thought I should come up with an example…

Let’s suppose you want to bind a keystroke to insert a blank line before your cursor (somewhere, I’ve heard this idea before), I spent three minutes to write it…mainly, because I’m a bear of very little brains and Lisp ability and had to look a few things up along the way:

(defun insertline-before ()             ;; Every extension is a function
  "Inserts a line prior to the cursor." ;; Describe the function for fun.
  (interactive)                         ;; This can be called from M-x
  (let ( (current-spot (point) ) )      ;; Store the position of the cursor
    (move-beginning-of-line 1)          ;; Move cursor to front of line, Duh.
    (insert "\n")                       ;; New line
    (goto-char current-spot)            ;; Go back to where we were... Oh no.
    (forward-char)))                    ;; We need to move forward since we
                                        ;; inserted a new line.

Not too useful unless I can bind it to keystroke:

(global-set-key (kbd "C-S-o")  'insertline-before)

And scene.

Actually, I should call newline instead of insert to make sure files on different operating systems, get the correct end of line sequences, but come on now, that doesn’t really happen. ;-)

Org Mode

While I recommend cool apps like Evernote for mere mortals, you are a demigod, and therefore, your mental wranglings must be stored in simple text files that can be searched with grep,3 manipulated with scripts, and backed up with git.

Text files, baby.

Now, org-mode is a plugin for treating normal text files as special…just like your teachers treated you as a child. Oh, come back; I was only kidding. Do you need me to call you a demi-god again?

Too bad most editors can’t seem to port anything but a slim subset of features from org-mode. It is so helpful, you should probably use Emacs on the side just for this GTD aspect.

Interested? Check out the following videos:

Literate Programming

While Donald Knuth first came up with literate programming in the 1980’s, org-mode makes this incredibly useful by allowing you to evaluate code blocks within your text files…even using the results as values in other code blocks. When working on particularly difficult algorithm, talk it out with yourself in a literate way.

For example, I could have a block of code that gives me the number of unique words in a list of files. One approach is to use Doug McIlroy’s famous approach using the shell:

#+NAME: data
#+BEGIN_SRC sh

  for FILE in ~/journal/2*
  do
     cat $FILE | tr -cs A-Za-z '\n' | tr A-Z a-z | sort | uniq -c | wc -l
  done

#+END_SRC

In org-mode, the lines beginning with a #+NAME labels both this script as well as its output (which we’ll use in a minute). We then begin our shell script. Running this little code snippet will put the results back into my file, like:

#+RESULTS: data
  - 10
  - 22
  - 9
  - 216
  - 165
  - 129
  - 111
  - 12
  …

While I have a lot of files, if I hit Tab on the first line, it collapses to a single line:

#+RESULTS: data…

Now I want to figure out the standard deviation of unique words, but that would be difficult to do in shell, but easy in another language. Since I’m bragging about Emacs’ Lisp, I might as well use that, however, you could use Python, JavaScript, etc.

#+BEGIN_SRC elisp :var samples=data
    (defun sqr (x) (* x x))

    (defun std-dev (samples)
      (let* ( (n (length samples))
              (mean (/ (reduce #'+ samples) n))
              (tmp (mapcar (lambda (x) (sqr (- x mean))) samples)))
        (sqrt (/ (reduce #'+ tmp) n))))

    (std-dev samples)
#+END_SRC

Notice that on the first line, I define a variable, samples which is initialized with the output from data. Running this gives me my output:

#+RESULTS:
: 12.68857754044952

This example is beyond Literate Programming and enters the realm of reproducible research where the commentary, algorithms and data are included in the same “paper”.

Mature Features

Most editors should give you the following features:

Some IDEs excel at providing a great environment for a particular language, Emacs, however, is a general system, but since programming in Emacs is easy, most languages are well supported, for instance, I use all these features for Python development.

My latest love affair is multiple cursors. This brilliant idea came from Sublime, but Magnar Sven ported it…probably just to increase his Emacs Rocks fan base.

My point here is the Emacs community is still bringing new features and language support…even after thirty years.

Mind Share and Shareable Effort

Funny that while most languages have some CPAN-like feature for sharing code, Emacs just barely got it. This delay, I think, may be due to how easy it is to write our features and share our wisdom in small snippets, instead of Herculean packages.

Sure, some Emacs packages, like org-mode, gnus, and the calculator are pretty large and require their own manuals, but those normally shipped with Emacs. Uh, yeah, Emacs’ calculator is no little ten key pad, but also a cool, symbolic processing system. Just check out this video.

With such a malleable system as Emacs, and thousands of unique opinions on what an “editor should be”, if you don’t craft your own version of Emacs, you can start with some good, helpful bases:

  • Graphene by Robert Dallas Gray attempts to woo users of GUI IDEs like Sublime. If you use a Mac, you may want to try Aquamacs
  • Spacemacs, a great start, especially if you like VI keybindings
  • Scimax is oriented to the scientist/engineer for reproducible research and publishing, with a focus on LaTeX and org-mode
  • Prelude by Bozhidar Batsov
  • emacs24-starter-kit by Eric Schulte
  • emacs-live by Sam Aaron is oriented to Live Coding, especially in performance settings (you haven’t heard or seen Overtone in action? Then check out some of these videos)

Summary

Using Emacs means you’ll customize it–and customization and extension is Emacs’ greatest feature. Remember, being efficient in Emacs comes with the temptation to spend that extra time gained to program further efficiencies. ;-)

Wanna see my Emacs in action? Check out this online video:

Footnotes:

1

Sure, I dabbled in XEmacs, but I didn’t inhale.

2

Did you know that Emacs predates Linux!?

3

Actually, ack (Beyond Grep) and ag (The Silver Searcher)