Emacs, that Free-as-in-Freedom operating system that also includes a text editor, is one wild, complex and irritating beast. One of the reasons why, in spite of this, Emacs still has lots of users is that it can save you lots of time — if you have the patience to learn a few tricks and keyboard shortcuts. This can happen in many ways: the simplest and most obvious one, which is the topic of this post, is to make you type less. Emacs has so many ways to do this that there is a whole chapter of its manual devoted to “autotyping”. Here I’ll present the tricks that I find most useful and use more frequently (with the one exception noted below). They’re also quite easy to configure and customize, and can help you to acquire confidence before messing with the other, more esoteric functions.
The basic technique is always the same. In order to customize Emacs, you must add the definition of some Lisp functions and variables to its configuration file, normally $HOME/.emacs.
To many people, Lisp looks much more arcane and unreadable than a Klingon to Sanskrit dictionary. Luckily, you can still use and customize the Emacs functions described here, even if you don’t understand every detail of their syntax. All you need to do is modify some file paths to suit your taste and don’t remove any parenthesis.
Just like full blown word processors and all other advanced text editors, Emacs has a text autocomplete feature that recognizes what you are typing and suggests a list of possible completions. Personally I don’t use it very much (personal taste, nothing else), but it wouldn’t make sense to not mention it in a post on this topic. This said, the Emacs autocomplete function that I prefer, complete with pop-up menu, is the one described in the Emacs Wiki.
Very often, I need something from Emacs that may look similar to autocompletion, but isn’t, that is support for abbreviations. The difference between the two features may not be evident at first sight, but is really simple. Autocompletion works on single words. Emacs abbreviations support, instead, replaces typing whole sentences or group of words with typing one short, single word. It makes it possible, for example, to type only fmivhm and have Emacs insert “For more information, please visit my <http://mfioretti.com>home page</a>”. Before defining your abbreviations, you must enable global support for them in Emacs, by adding this code to your .emacs file:
(setq-default abbrev-mode t)
(setq save-address t)
Once you’re done it, restart Emacs, ignoring any warnings that the abbreviations file doesn’t exist: Emacs itself will create it when you define the first abbreviation. In order to do that, type in any buffer some string, for example, litgose followed by CTRL-x aig (which means more or less “abbreviation insert global”). Next, enter “Linux is the greatest operating system ever!” at the Emacs prompt and press Return. At that point, Emacs will fill the predefined file (”~/.my_emacs_abbreviations” in the example above) with all the Lisp code needed to use the abbreviations. From now on, every time you’ll type litgose, Emacs will replace that string with the whole sentence you associated to it. The actual list of abbreviations stored in the read-abbrev-file is worth a look. Here’s an excerpt of mine:
("myname" "Marco Fioretti" nil 1)
("litgose" "Linux is the greatest operating system ever!" nil 1)
("fmivh" "For more information, please visit my <http://mfioretti.com>home page</a>" nil 1)
As you can see, the syntax is simple enough to add abbreviations by hand or write some scripts that generates them.
Emacs also has what is called a QuickURL insertion feature which works in a similar way. As its name suggests, QuickURL was originally developed to quickly add long URLs after certain predefined strings, without typing them entirely. Of course, in practice you may use QuickURL for any kind of strings. Personally, after using it for a while, I abandoned QuickURL because it was more convenient for me to use abbreviation for that purpose. However, since you may find it more efficient, here is how it works. Unlike abbreviations, QuickUrl adds text to what you type, instead of replacing it. For example, if you have defined in the QuickURL list something like this:
((”Tech Republic” “http://www.techrepublic.com/” “My favourite Tech Blog”)
Emacs will add the string <URL: http://www.techrepublic.com/> every time you type “Tech Republic” followed by M-x quickurl (the markup can be customized, of course). Other QuickURL functions, well described in its documentation, facilitate the insertion of URLs in the list. The main difference between abbreviations and QuickURL is that the first feature is automatic once you enable it, while the second must be explicitly invoked every time you actually need it. It is up to you to figure out which behavior fits best with your writing habits.
Dates and times
The last Emacs function for typing less that I’ll present today is the one for dates. If you add this code to your .emacs file, you’ll get a quick way to insert the current day in your text:
;; insert current day in the buffer
(defvar insert-time-format "WRITTEN ON: %Y/%m/%d"
"*Format for \\[insert-time] (c.f. `format-time-string').")
(defun insert-day ()
"Insert the current day according to insert-time-format."
(insert (format-time-string insert-time-format
This Lisp snippet tells Emacs to add a string like “WRITTEN ON: 2011/11/15″ every time you type M-x insert-day. The format of the time string can be modified in almost any way you’d like. Besides the three variables used here, you may, for example, add the hour in several formats or use codes like %w to insert the day of the week or %j for the day of the year. To know all the possibilities, type M-x describe-function in Emacs, then press Enter, and type format-time-string. Happy (quick) typing!