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).