Here’s Sacha telling us how she uses emacs:
And don’t miss her behind the scenes writeup!
As much as i try to avoid it, i always end up with lots of usernames and passwords to remember, not to mention a couple of bank accounts and a credit card number for on-line shopping. There’s no way i’m going to remember any of them—why, i even need to keep track of my telephone number. Time to write down a tidy nice little list, that is, time to look for and set up an adequate emacs mode or two.
When it comes to keeping lists, the table editor of org-mode is what you need. Org-mode is included in emacs 22, but Carsten &co. keep adding new stuff and fixing bugs, so it won’t hurt you to get the unstable version from its website. It comes with a nice manual and installing it is a freeze. You enter table mode by typing a vertical bar (
|) to separate columns:
* Bank accounts |Account | Credit card | Expiry date | Password | |-
RET are your friends: new rows are created and column widths adjusted automagically. You can also add separators by starting a line with
|- (as i did above) and typing
TAB. In no time you’ll have something like this:
* Banks |-----------------+------------------+----------| | Account | Credit card | password | |-----------------+------------------+----------| | The credit box | 8180819999999333 | fooo | | GNU Free Credit | 6969696969696969 | boobarp | | Engineering Safe| 0000000111111111 | passwdd | | paypal | | paaassss | |-----------------+------------------+----------| * Sites |-------------------------------+------------------------+ | site | user | password | |-------------------------------+------------------------+ | http://www.gnu.org | a user name | password | | http://journals.foo.org | a user name | password | | http://philosophy.org | a user name | password | | http://linkedin.com | a user name | password | | http://www.pragpro.com | a user name | password | | http://www.tug.org/members | a user name | password | |-------------------------------+------------------------+ * Source code repositories...
All in conveniently foldable sections, so that you can expand only the interesting section.
But, of course, you don’t want to save this as a regular file (let alone publish it on the internet). Even on a Unix machine, protecting it via file permissions is very weak. Nah, what you want is to encrypt the thing. To that end, one can use public key cryptography.
In a nutshell, you generate a pair of keys: one of them is private, only for your eyes, and therefore should be protected by a solid password; the other one is public: you make it available to anyone that wants to communicate with you. People then write their secret text and encrypt it using the public key. When that’s done, only your secret key (barring the NSA) can decipher the text. Of course, nothing prevents you from using the same device to encrypt and decrypt your passwords file.
This being an emacs blog, i won’t delve into the details of using GnuPG to create a key pair if you don’t already have it. But you being an emacs user, i’m sure you’ll be quite able to run
gpg --key-gen to generate your keys.
You could now use
gpg to manually cipher and decipher the passwords file, but, you know, one uses emacs because it can do almost any thing for you. In this case, EasyPG will take care of the chore of decrypting the file every time you open it and encrypting it back when it goes to disk. The EasyPG package comes bundled with emacs 23, and, again, it is very easy to install if you are using previous emacs versions. This is the configuration i use for this package:
;; Emacs 23: bundled EasyPG (require 'epa) (epa-file-enable)
or, if you installed it externally:
;; EasyPG installed in path/to/epg (add-to-list 'load-path "path/to/epg") (require 'epa-setup) (epa-file-enable)
(Yeah, it’s called easy for a reason!) With this magic incantation in place, every time you open a file with the extension
.gpg, EasyPG will do the work for you.
So, all that is left to do is to save our file as, say,
dobeedoo.gpg and inform emacs that we want to open it as an org-mode file by adding the following first line to it:
-*- mode: org -*- -*- epa-file-encrypt-to: ("email@example.com") -*-
As you can see, we’re also telling EasyPG what key it should use for its cryptographic activities.
That’s it. No rocket science here, but very handy nonetheless, and a very nice example of how different major (org) and minor (org-table, epa) emacs modes can work together for you. A perfect use case of minor modes providing functionality orthogonal to that in the major mode, which is caring about the actual file contents. Personally, this is also the use case that got me started with org-mode: may it enlighten you too
(BTW, now that you have EasyPG installed, try
M-x epa-list-keys, a nice keyring browser, if you ask me.)
As it happens, recent (as in
CVS 1.4.4) versions of emacs-w3m offer a similar functionality in the
M-x w3m-link-numbering-mode toggles a minor mode showing link numbers on an overlay, and
M-n M-x w3m-move-numbered-anchor, where
n is the link number, moves the pointer to the desired link. Providing conkeror’s
more handy functionality is then a matter of a few elisp lines:
(require 'w3m-lnum) (defun jao-w3m-go-to-linknum () "Turn on link numbers and ask for one to go to." (interactive) (let ((active w3m-link-numbering-mode)) (when (not active) (w3m-link-numbering-mode)) (unwind-protect (w3m-move-numbered-anchor (read-number "Anchor number: ")) (when (not active) (w3m-link-numbering-mode))))) (define-key w3m-mode-map "f" 'jao-w3m-go-to-linknum)
A quick and sweet tip for one of those things surprisingly difficult to get right in emacs: buffer scrolling. If you find awkward the way emacs makes your buffers jump when you move the cursor near their begin or end, try putting this file in your load path and
(require 'smooth-scrolling). You can control when scrolling starts by setting the variable
smooth-scroll-margin: when the cursor is that far from the top or bottom borders, scrolling begins (default is 10 lines). Works like a charm here.
So, you just executed a complex, wonderful emacs command using the minibuffer and you’re so excited that need to immediately write it down, lest you forget. As you may already know, you don’t need pen and paper for that. Emacs will do it for you: just press
C-x ESC ESC (that is,
repeat-complex-command) and you’ll see an Elisp form in your minibuffer that reproduces your last command (
C-h w C-x ESC ESC for more details). Now
C-a C-k and you got it in the kill-ring, ready to be yanked in one of your configuration files.
I don’t use this trick that much because over the years more and more emacs automatisms are recorded either in my nervous system or my config files. Except for one thing: defining keybindings. There’re a bunch of ways to define a keybinding in emacs, and i’m never sure what’s the right one when there’re special keys involved. The solution is to define the key interactively with
M-x global-set-key. Once i’m done, i play
C-x ESC ESC and presto: an elisp snippet to yank in my .emacs appears in the minibuffer.
Let me tell you how Emacs is more and more taking the center stage in my MacBook. In a previous post i explained how i set up Gnus as my mail (and of course, news) reader: i like Gnus so much that it quickly became my default. And, just in case you didn’t know, Emacs can be OS X’s default mail handler too: just go to Mail’s general preferences pane and set it–it’s that easy. (In the image, i’m setting fink’s carbon emacs package, but you’ll see there too CarbonEmacs or Aquamacs if you have them installed.) With that setting in place, OS X will dutifully use Gnus whenever a mail handler is requested (e.g., when following
mailto: URLs, or when using ‘Send this page…’ in Safari).
Unlike Gnus, Emacs is not my default web browser, although i use w3m-el quite a bit (specially for technical manuals). So, every now and then i find myself seeing a page in Safari than i want to open in Emacs. Applescript to the rescue: fire up that ugly Script Editor and type this simple program:
property eclient : "/sw/bin/emacsclient -e " tell application "Safari" set this_url to the URL of document 1 do shell script eclient & \ "'(w3m-browse-url \"" & this_url & "\")'" tell application "Emacs22" to activate end tell
(changing the path to emacsclient and the name of your Emacs as needed). Of course, you’ll also need to start the Emacs server somewhere in your init files with
(start-server), and to save the above script in
~/Library/Scripts/Applications/Safari. I’ve named it ‘Open in Emacs’, and it appears nicely as an entry in my FastScripts menu.
The last, and most interesting, bit is going in the opposite direction: accessing Safari (or any other Cocoa application, for that matter) from Emacs. Or, put in another one, executing AppleScript snippets within Emacs. One possibility is using Emacs’
shell-command in conjunction with OS X’s
osascript, but there’s a sweeter way: the Elisp function
do-applescript. For instance, the function
jao-as-safari-doc below returns the URL and title of the active page in Safari:
(defun jao-as-tell-app (app something) (let ((res (do-applescript (concat "tell application \"" app "\" to " something)))) ;; the string returned is quoted (substring res 1 -1))) (defun jao-as-safari-doc () (interactive) (let ((url (jao-as-tell-app "Safari" "get the URL of document 1")) (name (jao-as-tell-app "Safari" "get the name of window 1"))) (cons url name)))
This may seem a bit boring at first, but it can be put to good use: when i see a page worth taking a few notes, i open an org-mode buffer, and type a shortcut bound to the following function:
(defun jao-org-insert-safari-link () (interactive) (let ((l (jao-as-safari-doc))) (insert (org-make-link-string (car l) (cdr l))) (message "Link to %s inserted" (car l))))
and (minor) magic happens. I’m sure you can think of many other interesting uses of
do-applescript, can’t you?
When i use Firefox, there’s nothing more annoying than editing a textarea. On a Mac, one has at least some Emacs-like shortcuts. Incomprehensibly, on GTK+ 2.0 Emacs shortcuts are no longer the default, and one has to put something like
~/.gtkrc-2.0 to restore a minimum of sanity.
But still, what i really want is to edit those textareas using the real thing. I just stumbled upon the answer to my prayers: It’s All Text! is a Firefox add-on that provides an edit button on any text box. You click on it and, the first time, you’re asked for your editor of choice. As you’ll notice, the default option is wrong. Change it by
/usr/bin/emacsclient (Emacs 21 users may use gnuclient instead), and don’t forget to start the Emacs server with
(server-start) somewhere in your initialisation file.
Update: Well, things can be even better, as pointed out by Victor below. The Mozex extension lets you not only edit textareas, but also assign shortcuts, view sources or choose the editor for mailto URLs. For some reason, using just
emacsclient -e '(compose-mail "%a" "%s")' didn’t work for me, so i’ve created a simple shell script,
#!/bin/sh exec emacsclient -e "(compose-mail \"$1\" \"$2\")"
and told Mozex to use it. Don’t forget to set the variable
mail-user-agent to something reasonable (for instance, since i use Gnus, i’ve got
(setq mail-user-agent 'gnus-user-agent) in my configuration files).
I’ve spent part of the morning setting up Gnus on my OS X, to replace Mail.app and ease synchronization with my GNU/Linux boxes. I won’t enter in the details of basic Gnus setup (see, for instance, the Gnus tutorial), but offer some customization tips, and mention two of my favourite Gnus-enhancing packages (which are not OS X-specific, by the way).
First thing was to import my Mail.app mailboxes. Easy. Open the mailbox in Mail, select all messages and execute the menu command
Save as... (under
File); in the save dialog, be sure to select
Raw Message Source. The saved file is in mbox format, a Gnus import friendly one. All you need to get those messages into Gnus is to press
B f in the *Group* buffer, providing your mbox file when asked. A new group is created with all your messages. You can, for instance, mark all of them and
B c to copy them in, say, a regular
Setting Gnus to read and send messages via Gmail was also easy following the instructions here. A caveat: you’ll need also the file ssl.el from the Gnus CVS repository to make POP3 work (and of course gnutls, which i installed via the fink package gnutls14; be sure to tell Emacs about your fink path:
(add-to-list 'exec-path "/sw/bin")).
With that, I had a functional Gnus running and could move on to more interesting stuff. I don’t know about you, but i absolutely need random mail signatures, selected from my growing collection. To that end, i’ve been using randomsig.el since i can remember. Just put the file in your load path (or, if you’re on Debian, install the
gnus-bonus-el package), and add to your initialisation files something along the lines of:
(when (require 'randomsig nil t) (define-key message-mode-map (kbd "C-c s") 'randomsig-replace-sig) (define-key message-mode-map (kbd "C-c S") 'randomsig-select-sig) (require 'gnus-sum) ; probably required for `gnus-summary-save-map' (define-key gnus-summary-save-map "-" 'gnus/randomsig-summary-read-sig) (setq randomsig-dir "~/Library/Emacs/Gnus/") (setq randomsig-files '("signatures.txt")) ;; or (setq randomsig-files (randomsig-search-sigfiles)) ;; or (setq randomsig-files 'randomsig-search-sigfiles) (setq message-signature 'randomsig-signature) (setq randomsig-delimiter-pattern "^--"))
As you can see, i put my signatures in a file called signatures.txt. Now, every time i start editing an email, a random signature taken from this file is inserted for me. If i don’t like it, i can request another one typing
C-c s. And if i’m reading anything inside and Emacs buffer and find a new text to add to my
M-x randomsig-message-read-sig will add the region marked in the buffer to my collection. Perfect bliss.
Mail.app comes with a sophisticated search engine based on Spotlight. But i’ve noticed that i never use the 95% of its features, not to mention that it’s annoyingly slow. What i really need is a simple and quick keyword search, and i’ve had it for many years now with Gnus and nnir.el. This package provides an Emacs/Gnus front-end to several indexers, my preferred one being namazu. So, besides getting
nnir.el, you’ll need to install it. In Debian, it’s as easy as installing the corresponding package (namazu2), but, theres no namazu package to be seen in fink. Fortunately, you can compile it from source just by following the instructions: they worked in my MacBook without a glitch. With namazu installed, you can index your extant email in a breeze: create a directory to hold the index files (i use
~/Library/Caches/namazu) and type:
cd ~/Library/Caches/namazu mknmz --mailnews ~/Library/Emacs/Gnus/Mail
(using the appropriate directories, of course), and the index directory will get populated. Now, with
nnir.el in your load path, tell Emacs how to find those index files:
(when (require 'nnir nil t) (setq nnir-search-engine 'namazu) (setq nnir-namazu-index-directory (expand-file-name "~/Library/Caches/namazu")) (setq nnir-namazu-remove-prefix (expand-file-name "~/Library/Emacs/Gnus/Mail/")) (setq nnir-mail-backend (nth 0 gnus-secondary-select-methods)))
(i put this code in my Gnus configuration file). We are all setup: go to the *Group* buffer and type
G G to search your emails. A temporal group containing the messages that match your query will be created for you, for your browsing pleasure. Inside that group,
G T will teletransport you to the original message, in case you need to recover its context. As you’ll notice, namazu searches are at least an order of magnitude faster than Spotlight.
The only bit left is to set an entry in your crontab running
mknmz for you periodically (i’ve got it set up with a period of one hour). Indexing is incremental, i.e., fast. Yeah, it’s not that immediate indexing of Spotlight or Beagle, but, you know, it’s not like i get a mail every five seconds, or that i need engine to find a mail i read twenty minutes ago.
There are many other tricks to play with Gnus, but i’ll save them for future posts :-).
Most of the time, I run Emacs in a single, maximized frame (and, sometimes, just inside a urxvt terminal). Inside that frame i create and destroy windows as needed to contain my buffers . After a while working with Emacs one gets used to a fluid window setup, where windows come and go as need arises.
But I remember that in the early days I felt a bit confused: I started by creating a windows setup (split horizontally, then vertically, open files in each of the windows… that kind of thing) and was non-plussed when any operation (going to a Gnus buffer and reading messages, for instance) destroyed my carefully prepared layout. Over the years i’ve seen many a newbie feeling that way. My advice is usually to let go, and learn how to move around your windows, creating them as needed.
That said, i must admit that, sometimes, i want to recover a certain window configuration in a quick way. A particularly simple one is provided by
winner-mode, a package which is part of Emacs. Winner keeps track of your window configurations, and provides functions to navigate your window configuration history.
As advertised, activating
winner-mode (which is a global minor mode) is as easy as one can get: just type
M-x winner-mode or put this line:
in your .emacs. Use Emacs normally, and whenever one cherished window layout gets destroyed, press
C-c <left> (where <left> refers to the left cursor key) to recover it–actually, this calls
winner-undo, and you can invoke it as many times as you want to visit previous window configurations. As one would expect,
C-c <right> (the default binding for
winner-redo) navigates configurations in the opposite direction . Simple and very useful, as it should be.
But maybe you want to be automatically tele-transported to a previous configuration without traversing the intermediate ones. No problem: Emacs provides functions to store and retrieve window configurations (which, in fact, are the ones
winner-mode is using under the covers). You can store the current window configuration in register ‘a’ (registers are named using single letters or numbers) pressing
C-x r w a, and restore it later by means of
C-x r j a. Again, it’s that easy.
Although there exist several packages providing more sophisticated window management (named configs, save and restore, etc.), in my experience, the simple tricks described above are more than enough (and work out of the box in any Emacs). As always, YMMV.
 For those of you not familiar with Emacs lingo, an Emacs frame is what most window managers would call a window, while an Emacs window is any of the regions displaying a buffer inside a frame. Usual ways of creating windows in Emacs are
C-x 4 f, and so on. See Windows and WindowsAndFrames for more.
 In Emacs 21, the default keybindings start with
C-x instead of
imenu is a very handy command i learnt not long ago. Just type
M-x imenu in a source code buffer (an Elisp one, for instance) and start typing the name of a variable or function (or simply
TAB for name completion). There you go: instant access to definitions in the buffer.
You can also get the list of completions in a menu item calling
imenu-add-menubar-index. Or, if you want the menu to be automatically created for, say, all your C files, you can put something like
(add-hook 'c-mode-hook 'imenu-add-menubar-index)
in your initialisation files.
You can read more about
imenu (which has obsoleted speedbar in my workflow) in the Emacs Wiki, including how to adapt it to your favorite language (if it’s not yet supported).
Oh, and the workaround: if you’re using Emacs 22 on Mac OS X, you may need to add this dummy definition to your .emacs to make
(defun imenu-progress-message (a &optional c d))
(otherwise, you’ll get an error about
imenu-progress-message being an invalid function).