This is the second in my series of posts pointing out resources on my site. This week’s topic is Emacs.
Last week: Miscellaneous math notes
Next week: R resources
I spoke with with Sacha Chua last week. We talked about entrepreneurship, Emacs, having eclectic interests, delegation, and more.
J: I ran into you by searching on Emacs topics. When I look at your blog, I see that you do a lot of interesting things, but it’s a little hard to get a handle on exactly what you do.
S: Oh, the dreaded networking quirky question. What exactly do you do?
J: Yeah, people have said the same thing to me. Not to put you in a box, but I was curious. I see from your site that you do graphic art — sketching and such — and it doesn’t create the impression that you’re someone who would spend a lot of time in front of Emacs. So I’m curious how these things fit together, how you got started using Emacs and how you use it now.
S: So my background is actually fairly technical. I’ve been doing computer programming for ages and ages. In high school I came across a book Unix Power Tools, which is how I got interested in Emacs. And because I was interested in programming, in open source, a little bit of wearable computing as well, I got to know Emacs and all these different modules it had. For example, Emacspeak is amazing! It’s been around since the 1990s and it’s a great way to use the computer while you’re walking around. Because I love programming and because I wanted to find a way to help out, I ended up maintaining PlannerMode and later EmacsWiki mode as well.
When I went to university, I took up computer science. After I finished, I taught. Then I took my masters in Toronto, where I am now. Emacs was super helpful — being able to do everything in one place. After I finished my masters, I did a lot of software consulting with IBM. I did business consulting as well. Then in 2012, after saving up, I decided to go on pretty much the same adventure you’re on. I’m completely unhirable for the next five years! Most businesses struggle for the first five years, so I saved up enough to not worry too much about my expenses for the next five years. I’m one year in, four years to go, and that’s where I am.
At networking events, I like to shake people up a bit by telling them I’m semi-retired. I’m in this five-year experiment to see how awesome life can be and what I can do to make things better. I’ve done technical consulting, business consulting, sketching, illustration, writing, all sorts of things. Basically, my job description is context-dependent.
J: I understand that.
S: I use Emacs across all the things I do. When I’m doing technical and business consulting, I use Emacs to edit code, to draft documents, even to outline comic strips. And when I’m doing illustration, Emacs — especially Org Mode — helps me keep track of clients and deliverables, things to do, agenda, calendar, deadlines.
J: I’m basically running my life through Org Mode right now. When you say you use Emacs to draft documents, are you using LaTeX?
S: I used LaTeX when I was working on my master’s thesis and other papers, I think. Now I mostly use org mode and export from there.
J: Are you using Emacs for email?
S: I used to. But I’m stuck on Windows to use drawing programs like Sketchbook Pro on my Tablet PC. So it’s harder to set up my email like I had it set up when I used Ubuntu. Back when I used Ubuntu, I was very happy with Gnus.
J: Do you work entirely on Windows, or do you go back and forth between operating systems?
S: I have a private server that runs Linux. On Windows I run Cygwin, but I miss some of the conveniences I had when I had a nicely set-up Linux installation.
J: When you’re running Emacs on Windows, I’m sure you run into things that don’t quite work. What do you do about that?
S: Most things work OK if they’re just Emacs Lisp, but some things call a shell command or use some library that hasn’t been ported over yet. Then I basically wail and gnash my teeth. Sometimes I get things working by using Cygwin, but sometimes it’s a bit of a mess. I don’t use Emacs under Cygwin because I prefer how it works natively. I don’t run into much that doesn’t work.
J: So what programming languages do you use when you’re writing code?
S: I do a lot of quick-and-dirty things in Emacs Lisp. When I need to do some XML parsing or web development, I’ll use Ruby because a lot of people can read it and there are a lot of useful gems. Sometimes I’ll do some miscellaneous things in Perl.
I love doing programming and putting together tools. And I quite enjoy drawing, helping people with presentation and design. So this is left brain plus right brain.
It does boggle people that you can have more than one passion, but others are, like, “Yeah, I know, I’m like that too.”
J: I think having an interest in multiple things is a healthier lifestyle, but it’s a little harder to market.
S: Actually, no. I finally figured out a name for my company, ExperiVis, after a year of playing with it. People reach out to me and we figure out whether it’s a good fit. I don’t need to necessarily guide people to just this aspect or another of my work. I like the fact that people bump into these different things.
J: When we scheduled this call, I went through your virtual assistant. How do you use a virtual assistant?
S: One of the things I don’t like to do is scheduling. I used to get stressed out about scheduling when I did it myself. I’ve always been interested in delegating and taking advantage of what other people enjoy and are good at. I work with an assistant — Criselda. She lives in the Philippines. I found her on oDesk. She works one to four hours a week, more or less, and keeps track of her time.
J: What else might you ask a VA to do?
S: I’ve asked people to do web research. I’ve had someone do a little bit of illustration for me. I’ve had someone do a little bit of programming for me because I want to learn how to delegate technical tasks. He does some Rails prototyping for me. I have someone doing data entry and transcription. It’s fascinating to see how you can swap money for time, especially for things that stress me out, or bore me, or things I can’t do.
Every week I go over my task list with my VA to see which of the tasks I should have delegated. Still working on it!
Later on in the conversation Sacha asked about my new career and had this gem of advice:
Treating this as a grand experiment makes it much easier for me to try different approaches and not be so scared, to not treat it as a personal rejection if something doesn’t work.
When I got a new computer a few days ago, I installed the latest version of Emacs, 24.2, and broke my Python environment. I decided to re-evaluate my environment and start over. I asked a question on the Python Google+ group and here’s a summary of the Emacs packages recommended.
What recommendations do you have for packages? Links to relevant articles?
It might be better to leave your comments on Google+ rather than here so the answers will be in one place.
Vivek Haldar had a nice rant about editors a couple days ago. In response to complaints that some editors are ugly, he writes:
The primary factor in looking good should be the choice of a good font at a comfortable size, and a syntax coloring theme that you like. And that is not something specific to an editor. Editors like Emacs and vi have almost no UI!
To illustrate his point, here’s what my Emacs looks like without text:
There’s just not much there, not enough to say it’s pretty or ugly.
When people say that Emacs is not pretty, I think they mean that plain text is not pretty.
For better and for worse, everything in Emacs is text. The advantage of this approach is consistency. Everything uses the same commands for navigation and editing: source code, error messages, directory listings, … Everything is just text. The disadvantage is that you don’t have nicely designed special windows for each of these things, and it does get a little monotonous.
When people say they love their text editor, I think they love text. They prefer an environment that allows them to solve problems by editing text files rather than clicking buttons. And as Vivek says, that is not something specific to a particular editor.
The most basic text editor commands operate on lines and characters: move up or down a line, delete the next or previous character, etc. More advanced commands operate on context-specific semantic units. In the context of English prose, this means words, sentences, and paragraphs. In the context of source code, it means blocks and functions, however these are delimited in a particular language. In the context of an outline, it means navigating the tree.
One way to become more proficient at using your editor of choice is to become fluent at working with larger semantic units. Of course this saves keystrokes, though as I argued here, I don’t think typing speed matters so much. However, working in larger semantic units reduces errors. It also improves the chances that you can act on an idea before you forget what you were doing. These factors matter more than typing speed per se.
The general principles above apply to any editor. Here I’ll sketch how these ideas work in Emacs.
As a general rule, commands that operate on physical units begin with Control and analogous commands that work on semantic units being with Meta (Alt). For example, C-f moves forward a character and M-f moves forward a word. C-e moves to the end of a line and M-e moves to the end of a sentence.
Emacs extends the meaning of prose units by analogy in other contexts. For example, in calendar mode, a “line” corresponds to a week, and a “paragraph” corresponds to a month.
Emacs has numerous commands to work on “balanced expressions,” i.e. text surrounded by parentheses, brackets, braces, quotation marks, etc. Often the default keybindings are consistent with those used for other units. For example, “f” means forward and “b” means backward in any context. C-f and C-b move by character, M-f and M-b move by word, and C-M-f and C-M-b move by balanced expression. Emacs also has commands for working with function definitions: selecting a function definition, moving to the beginning or end of a function definition, etc. In Lisp, function definitions are balanced expressions. In other languages, like C, these ideas are distinct.
This morning, Irreal points out an Emacs package
expand-region that selects larger and larger semantic regions. The first time it is invoked, it selects the immediate context of the cursor. Each subsequent invocation selects a larger context, moving up the parse tree of the content. I look forwarding to trying it.
Ruby creator Yukihiro Matsumoto gave a presentation How Emacs changed my Life in which he explains how Emacs influenced him personally and how it influenced the programming language he created. Here is his summary:
Emacs lets you split your screen into windows, what other applications might call panels. This can be quite handy. However, I often want to move the windows around and I couldn’t find how to do that. I asked Xah Lee, he posted the question on G+, and Mark Hepburn answered with a pointer to code that does what I wanted: buffer-move.el
(Strictly speaking the windows don’t move; the buffers move between the windows. But I think of it as moving the windows around.)
Lucas Bonnet explains how his
This file is for lazy people wanting to swap buffers without typing C-x b on each window. This is useful when you have : +--------------+-------------+ | | | | #emacs | #gnus | | | | +--------------+-------------+ | | | .emacs | | | +----------------------------+ and you want to have : +--------------+-------------+ | | | | #gnus | .emacs | | | | +--------------+-------------+ | | | #emacs | | | +----------------------------+
This is exactly what I had in mind. I did have one problem, however, though it wasn’t with
buffer-move per se. The file defines four functions for moving buffers between windows and suggests key bindings. The author says he uses the following.
(global-set-key (kbd "<C-S-up>") 'buf-move-up) (global-set-key (kbd "<C-S-down>") 'buf-move-down) (global-set-key (kbd "<C-S-left>") 'buf-move-left) (global-set-key (kbd "<C-S-right>") 'buf-move-right)
This works fine, except when one of the windows contains an org-mode buffer. Org-mode has its own bindings for control-shift-up etc.
I learned that
global-set-key does not globally set a key binding! Well, it does as long as there are no conflicts. But local bindings, i.e. bindings specific to a mode, have precedent. After some searching and trail-and-error, I added the following to my
.emacs to override org-mode’s local bindings.
(add-hook 'org-mode-hook '(lambda () (local-set-key [C-S-up] 'buf-move-up) (local-set-key [C-S-down] 'buf-move-down) (local-set-key [C-S-left] 'buf-move-left) (local-set-key [C-S-right] 'buf-move-right)))
The Windows clipboard only remembers the most recent thing you copied . This can be very disappointing. Maybe you cut a large block of text intending to paste it somewhere, but without thinking you cut something else, and then realize you’ve just wiped out your first cut. Here are a few possible alternatives for Windows, MS Office, and Emacs.
A few years ago I tried ClipX, a free clipboard history manager for Windows, based on Jeff Atwood’s recommendation. It worked OK at first, but then I had problems with it and stopped using it. It looks like the software hasn’t been updated since 2008. If you know of a better alternative, please let me know. Also, please let me know if you have suggestions for Ubuntu.
(Update: See the comments for numerous suggestions of ClipX-like products. Also, see PHenry’s comment about clipboard history in Visual Studio.)
MS Office has its own clipboard that will store 24 entries. It stores things that have been copied either inside or outside of Office applications, but you can only paste from the Office clipboard using an Office application.
Emacs has a “kill ring” which is essentially a clipboard. It also has “registers” which are like named clipboard items.
Yesterday Emacs Knight posted a handy little bit of code for viewing your kill ring.
(global-set-key "C-cy" '(lambda () (interactive) (popup-menu 'yank-menu)))
If you add this to your
.emacs file, you can type
ctrl-c y to pull up a little GUI menu displaying the contents of your kill ring.
 I’ve heard that Windows actually keeps more clipboard history, but it doesn’t provide a user interface for accessing that history. I don’t know whether this is true. But if it is, you could access the history programmatically. Maybe Office isn’t maintaining its own clipboard but just exposing more of the system clipboard.
I’ve been trying out
Eshell lately. It’s a shell implemented in Emacs Lisp. Here I’ll mention a few features I’ve found interesting.
M-x shell lets you run a shell inside Emacs. This is quite handy, but it runs a different shell on each platform. For example, it might pull up
bash on Linux and
cmd.exe on Windows. Eshell provides a consistent shell across operating systems.
Eshell is one way to have a bash-like shell on Windows, so it’s an alternative to Cygwin. I haven’t used Eshell or Cygwin extensively, but Cygwin feels like some alien thing awkwardly grafted on to my Windows machine. Eshell feels more like
bash features added.
Of course Eshell is integrated with Emacs. One example of that is being able to run Emacs commands as shell commands. For example, you can run
dired from the shell to open a directory in the Emacs directory editor.
Eshell has pseudo-devices. These act like Unix devices, but they’re implemented in Emacs and therefore run on non-Unix platforms. For example, you could direct command output to
/dev/clip to send it to the system clipboard or to
/dev/kill to add it to the Emacs kill ring.
And now for a little silliness. Here’s an excerpt from Tron featuring Eshell.
It goes by very fast, but here’s the frame that shows Eshell:
For much more on Eshell, see A Complete Guide to Mastering Eshell.
Emacs org-mode lets you manage blocks of source code inside a text file. You can execute these blocks and have the output display in your text file. Or you could export the file, say to HTML or PDF, and show the code and/or the results of executing the code.
Here I’ll show some of the most basic possibilities. For much more information, see orgmode.org. And for the use of org-mode in research, see A Multi-Language Computing Environment for Literate Programming and Reproducible Research.
Source code blocks go between lines of the form
#+begin_src line, specify the programming language. Here I’ll demonstrate Python and R, but org-mode currently supports C++, Java, Perl, etc. for a total of 35 languages.
Suppose we want to compute √42 using R.
#+begin_src R sqrt(42) #+end_src
If we put the cursor somewhere in the code block and type C-c C-c, org-mode will add these lines:
#+results: : 6.48074069840786
Now suppose we do the same with Python:
#+begin_src python from math import sqrt sqrt(42) #+end_src
This time we get disappointing results:
#+results: : None
What happened? The org-mode manual explains:
… code should be written as if it were the body of such a function. In particular, note that Python does not automatically return a value from a function unless a
returnstatement is present, and so a ‘
return’ statement will usually be required in Python.
If we change
return sqrt(42) then we get the same result that we got when using R.
By default, evaluating a block of code returns a single result. If you want to see the output as if you were interactively using Python from the REPL, you can add
:results output :session following the language name.
#+begin_src python :results output :session print "There are %d hours in a week." % (7*24) 2**10 #+end_src
This produces the lines
#+results: : There are 168 hours in a week. : 1024
:session tag, the second line would not appear because there was no
I had to do a couple things before I could get the examples above to work. First, I had to upgrade org-mode. The version of org-mode that shipped with Emacs 23.3 was quite out of date. Second, the only language you can run by default is Emacs Lisp. You have to turn on support for other languages in your
.emacs file. Here’s the code to turn on support for Python and R.
(org-babel-do-load-languages 'org-babel-load-languages '((python . t) (R . t)))
Update: My next post shows how to call code in written in one language from code written in another language.
In a typical Windows program, some output text can be copied as text but some is only an image. For example, the text in the body of a document is text, but the words on the menus are not. The text in a dialog box may or may not be.
In Emacs, text is just text, no matter where you see it. This means that Emacs is less visually interesting because there is less variety. But this also has several advantages that may not be immediately obvious. I’ll give a couple examples.
The Emacs directory editor
dired lists files in a text buffer. The output is essentially the output of
ls. (On Windows, Emacs emulates
ls.) That means you can do anything to the output you could do to text: copy, paste, search, etc. No text is off-limits. And in editable mode, you can rename files just by editing their names. You could, for example, rename many files at once using regular expressions.
Dired also lets you navigate the file system with the same commands you use to edit files. In particular, you can keep your hands on the keyboard. (It’s possible to use the Windows file explorer without a mouse, but it’s limited and awkward.)
Another advantage of everything simply being text is that you can run a shell inside Emacs. Why not? A shell is just text output. In an ordinary shell, editing commands are mostly limited to the current line. Not all text is created equal. But when you’re running a shell inside an editor, all text is equal: current line, previous lines, and text on either side of the command prompt. And if you want a transcript of a shell session, just save the buffer displaying the session.
If you’re shopping for a text editor and spend an hour trying each one out, Emacs may not look very promising. It has odd terminology, weird key strokes, etc. But if you try Emacs long enough, and particularly if you have someone to help you get started, you start to appreciate the advantages of subtle features such as everything simply being a text buffer.
“I use Emacs, which might be thought of as a thermonuclear word processor.” — Neal Stephenson
I had a little surprise when I tried to open an Excel file from Emacs. I was using
dired, a sort of file explorer inside Emacs. I expected one of two things to happen. Maybe Emacs would know to launch the file using Excel. Or maybe it would open the binary file as a bunch of gibberish. Instead I got something like this:
This was disorienting at first. Then I thought about how Office 2007 documents are zipped XML files. But how does
dired know that my
.xlsx file is a zip file? I suppose since Emacs is a Unix application at heart, it’s acting like a Unix application even though I was using it on Windows. It’s determining the file type by inspected the file itself and ignoring the file extension.
(Office 2007 documents are not entirely XML. The data and styling directives are XML, but embedded files are just files. The spreadsheet in this example contains a large photo. That photo is a JPEG file that gets zipped up with all the XML files that make up the spreadsheet.)
So I learned that Emacs knows how to navigate inside zip files, and that a convenient way to poke around inside an Office 2007 file is to browse into it from Emacs.
Here’s another post that discusses Emacs and Office 2007, contrasting their interface designs: Clutter-discoverability trade-off
This week I’ve run across two examples of technical strategies to reduce mental context switches.
The second example is Ryan Barrett’s blog post Why I run shells inside Emacs.
Mental context switches are evil. Some people handle them better than others, but not me. I handle them worse. I feel it, almost physically, when context switches whittle down my precious flow piece by piece.
Barrett explains that he first tried configuring Emacs and his shell to be similar, but the very similarity made the context switch more irritating.
Even something as small as switching between Emacs and the shell can hurt, especially when I do it thousands of times a day. … What’s worse, only some parts are different, so my muscle memory is constantly seduced into a false sense of familiarity, only to have the rug yanked out from under it seconds later.
Both examples highlight the cost of context switches. Neither Kruckenberg nor Barrett mentioned the cost of learning two contexts. Instead both focused on the cost switching between two contexts. Novices might understandably want to avoid having to learn two similar tools, but these stories were from men who had learned two tools and wanted to avoid oscillating between them.
My favorite line from Barrett is “context switches whittle down my precious flow piece by piece.” I’ve felt little distractions erode my ability to concentrate but hadn’t expressed that feeling in words.
It’s no exaggeration to call flow “precious.” Productivity can easily vary by an order of magnitude depending on whether you’re in the zone. It may sound fussy to try to eliminate minor distractions, but if these distractions make it harder to get into the zone, they’re worth eliminating.