Editor Macros

Published: March 16, 2015

Something really interesting happened to me over the course of the last few weeks: I switched from using a combination of tmux, vim, and the fish shell for my daily computing needs to doing everything and more in emacs!

I can already see some readers of this make faces and scream out profanities and other nice things in disgust: "You are such a heretic!", "How could you become one of them?", "You officially joined the dark side", "We have to stop being friends now."

But wait… Let me explain myself. Emacs is actually quite a pleasure to use.

The most important thing that I learned on my still very short journey through emacs-land is that comparing emacs and vim is not a very fair comparison. This might sound a little counterintuitive at first, but consider the following:

Text editor vs. Text processing system

Vim (and of course, vi; excuse me if instead of writing vim/vi, I will just write vim in this article) is a text editor. Using vim, your workflow will consist of opening a file, editing it, and then closing it and doing something else. Editing that file is a dream. I have absolutely no doubt that vim and its great text editing language is the fastest and most efficient way of editing any kind of plain text file.

Emacs, on the other hand, is a different beast: You can think of it as a text processing system. What I mean by that is that emacs is designed to manipulate and process any kind of text, which includes text files, shell I/O, human communications, the web, or anything else that, in its essence, is composed of text.

An integral part of making emacs so powerful is the emacs lisp, or 'elisp', programming language. At its core, emacs is really just an elisp interpreter and everything the user does to interact with a piece of text is an elisp command. For example, pressing any character key while editing a writable buffer would call a command to insert text at that specific point.

Of course, users can write custom commands in elisp, and over the years, many of those custom commands have been integrated in official emacs releases. Emacs has a command for just about anything! At this point, I have to point to the obligatory xkcd comic:

real_programmers.png

Emacs' configurability and vast number of features have sparked a lot of interest in me, up until the day that I actually tried it. All of emacs' configuration files live in ~/.emacs.d/, and the equivalent to the .vimrc file would be the file .emacs.d/init.el. At the point of writing this post, my init.el is already 366 lines of elisp code, after using emacs for only about a month! As you can see, hacking emacs is not only encouraged, it is almost required.

Just to showcase some of the powers of emacs, consider the following screen shot, which shows an emacs frame in which I'm editing the article you are reading, but I'm also running a full-blown MATLAB shell, complete with plotting window, and I'm reading email. Pretty cool, eh?

emacs_small.png

Below, I will present a few features that sold me on emacs. Detailed configurations will have to come in blog posts by themselves, otherwise this article would not be a very pleasant read.

A few highlights from my emacs configuration

TRAMP

I do a lot of editing of files on remote resources. With vim, that involved installing vim on the remote machine, configuring it to my liking, and editing the file using SSH. With emacs, most of this is obsolete! TRAMP, a feature that is enabled in emacs by default, lets me edit remote files almost as if they were local files! It integrates seamlessly in the default find-file commands and dired-mode and is just an absolute joy to use. Even the auto-completion of filenames on remote systems works out of the box.

TRAMP still uses SSH as underlying connection, but handles opening and closing sessions for us. Right now, I'm not using any customization here, it just kind of works. Here is another good blog post describing some of the features and usage of TRAMP.

ansi-term

Since working with the shell in Unix is 100% text-based, emacs lends itself perfectly as a terminal emulator. There are multiple ways of accessing shells in emacs, some more native than others, but I chose to use ansi-term, which to me feels like a good URxvt, which is the standalone terminal that I use. More info about the different shells and terminal emulators available in emacs can be found here and here.

One of the interesting consequences of me using ansi-term as my main terminal emulator is that it made my use of a terminal multiplexer (I was using tmux) obsolete! Emacs can split windows horizontally and vertically, and of course it can have buffers open in the background, which are all features which I was using in tmux extensively.

I also found that using emacs made me use the shell less, especially because of the great directory editor dired. Using dired to navigate and manipulate directories makes me use commands like cd, ls, mv, cp, mkdir, and all the other basic shell commands a lot less, since, of course, they are a lot less efficient than using a powerful, keyboard-driven file manager.

org-mode

Org-mode is great. I can hardly describe it's vast feature set in a few paragraphs, you just have to try it out. What got me started was this great screen cast.

Org-mode provides the user with tools that make it easier, smarter, and more pleasant to write things like outlines or TODO lists or really any kind of structured document. What differentiates org-mode from \(\LaTeX\) (at least in my understanding) is that \(\LaTeX\) focuses more on typesetting and the looks of a document, whereas org-mode focuses on the structure of a document.

Another great feature of org-mode is the fact that I can combine a document with data and code that operates on that data. I personally do not use this (yet), but it sounds interesting.

Right now, I mostly use org-mode to write README's (yes, GitHub understands .org for README files) and outlines for meetings. It has already transformed to way I write documents and I'm excited to explore further ways of using it.

matlab-mode

In my research and my courses I use a lot of MATLAB. Since I dislike their regular GUI (It's not you MATLAB, it's me…), I used to have a MATLAB shell open in one tmux pane and a edit a source file in another. It worked, but it also never felt quite right.

Cue matlab-mode. A marriage between MATLAB and emacs. It's introductory blog post is here. Now I'm able to run a MATLAB shell inside of emacs and interact with it from my editor window! Matlab-mode also makes gud, the emacs debugger interface, play nice with the MATLAB debugger, and comes with nice smaller features like checking and making sure that function names are the same as filenames (MATLAB people will know the struggle) and letting me run sections of code from my editing window by just selecting them. (I know that all of these things are standard in the MATLAB GUI, but like I said… It's me.)

Using MATLAB feels right now. I know that I'm still not using all of matlab-mode's features (I'm especially interested in the CEDET integration, although I should probably learn how to use CEDET first…), but I will pick more up as I go.

mu4e

Yes, I read my mail in emacs now. No, I will not turn into rms, but I must say that reading mail in emacs is a pleasure. Prior to emacs, I was using claws, which is undoubtedly the best graphical email client, but there is something about reading and sending mail using mu and mu4e together with emacs that gives me back a feeling of control over my email.

I need to keep things simple and short, so I will provide a short overview of the set-up:

Mu is a tool that creates an index of emails stored in the Maildir format, and then accesses it at breakneck pace. Mu4e Is simply an interface of the command-line tool mu to emacs.

Both tools do not receive mail or send mail, which is taken care of by two outside utilities, on which the reader can read up elsewhere: I use OfflineIMAP to sync my two IMAP mailboxes (School and Gmail) to a Maildir on my local hard drive, and I use msmtp to send mail through either one of my configured SMTP servers. Mu4e provides the plumbing to make these two play nice with mu, emacs, and even GPG.

This results in a very nice an intuitive text-based set-up where searching for emails is faster than anything I've seen before (yes, faster than Gmail's search). Mu4e's bookmarks feature has so far been a major productivity boost. It is best explained by showing my current mu4e home screen, which looks something like this:

* mu4e - mu for emacs version 0.9.11 CG

  Basics

    * [j]ump to some maildir
    * enter a [s]earch query
    * [C]ompose a new message

  Bookmarks

    * [bs] unread school email from today
    * [bp] unread gmail from today
    * [b7] last 7 days unread
    * [bt] all today
    * [ba] all unread

  Misc

    * [U]pdate email & database

    * [A]bout mu4e
    * [H]elp
    * [q]uit

The bookmarks feature lets me save searches for email and enables me to pull up exactly what I ask for, without any hassle. For example, if I press the keys b and t from my home screen, mu4e finds every email I've received today, and if I press b and a, I get presented with every email from today that is marked unread. Of course, searches can be narrowed down after the initial pattern.

Describing how to set up and configure mu4e is its own article, so stay tuned for a lot more in-depth information on this in the future.

Honorable mentions

There are plenty of other features that did not make it in this post (although they might get an individual post at some point). Those include the great AUCTeX package for editing \(\LaTeX\) documents, jabber.el for using XMPP chat (yes, I access facebook chat from emacs), FlyCheck as a better version of vim's Syntastic, the mighty gud debugger,and more cosmetic features like powerline.

Is this the start of a series?

Even though I did not go into a lot of detail, this has been a very long post. Maybe it's the start of a series, since each section could be followed up with its own article. I think it is. So stay tuned for the next article in my new series on emacs!

Contact me if you find out something cool, or have any requests for further articles about emacs and its features.

Dennis