From time to time I write an Emacs function that I believe others might find useful as well. I then try to post it on identi.ca, which requires me to trim it down to 140 characters minus the Emacs group tag. (The hardest part of that is that (Emacs) Lisp function and variable names tend to be long and descriptive, by the way.) Here are the ones I've posted so far, with comments and slightly improved formatting.
Save old buffer contents when reverting
This will save your life, or at least pieces of work that you put your
soul in, at least once. Before a buffer is reverted, it puts a copy
in the kill ring, so you can get it back with C-y. I found this
especially useful with vc-mode, where updating a file to whatever
version would perform a revert.
This takes up some memory, of course, though I haven't noticed that becoming a problem.
(add-hook 'before-revert-hook
(lambda ()
(kill-new (buffer-string))
(message "Previous buffer text saved to kill ring")))
Delete processes with an interactive command
The Emacs function delete-process is not interactive, which means
that you have to use M-: and type some magic to kill a certain
"process" (which in Emacs might be either a shell command or a network
connection). Here is a function that lets you choose the process to
delete with tab completion:
(defun delete-process-i(p)
(interactive `(,(completing-read"Kill proc: "
(mapcar 'process-name(process-list))()t)))
(delete-process p))
This one suffered the most from compression. The name was supposed to
be delete-process-interactive, but that was the first thing to go.
I also found that () is a handy alias for nil, especially as it
doesn't require spaces around it.
Get track info in EMMS playlist buffer
It shouldn't come as a surprise to anyone that there is a music player for Emacs. Well, probably more than one, but the one I use is EMMS, the Emacs Multimedia System.
Skipping ahead a bit, suddenly you're in the playlist buffer with a track that's being displayed only with its filename, instead of name, artist, album etc. EMMS is supposed to go through the playlist and get that information for each track, but sometimes you need to prod it. Thus this function, which forces EMMS to reread information for the track at point:
(defun my-emms-get-info()
(interactive)
(let(emms-info-auto-update)
(emms-info-really-initialize-track(emms-playlist-track-at))))
Change Eshell working directory
For running shell commands within Emacs I prefer Eshell to the
alternatives (M-x shell and M-x term). Eshell is implemented in
Emacs Lisp, and in my experience the friction between the editor and
the shell is lesser than when running Bash in an Emacs buffer.
If Eshell hasn't been started yet, M-x eshell starts it in the
current buffer's current directory. If Eshell has been started
already, M-x eshell jumps to the Eshell buffer without changing
directory. I wanted something better and ended up with ecd: it
prompts for a directory, defaulting to the directory of the current
buffer, and starts Eshell if needed and switches to the directory you
specify. (For extra enlightenment, use
ido-mode when
selecting the directory.)
(defun ecd (d)
(interactive
(list (expand-file-name (read-directory-name "cd: " nil nil t))))
(eshell)
(eshell/cd d))
(Actually it always starts Eshell. I found during "compression" that it doesn't matter.)
From time to time I write an Emacs function that I believe others might find useful as well.