2014-08-02 Converting zeros to hyphens

One of the things I’m a bit ashamed of is that I’ve never learned to touchtype. (I will, someday. Surely. Well, maybe.) Despite that, I can type pretty quickly (or at least I hope so) and without too much typos. There is, however, one mistake I tend to commit every so often: somehow when I want to type a hyphen, I very often hit zero. This is especially annoying when M-x-ing a long command name, but is as bad when typing some Elisp code. Well, now if there’s something I can’t make myself do when using Emacs, why don’t make Emacs do the right thing for me? Of course, binding - to 0 is a bad idea, but I came with the following function:

;;; Correct last occurrence of zero to a hyphen

(defun zero-to-hyphen (count)
  "Change COUNT last occurrences of zeros to hyphens.
With prefix argument, do it that many times (do nothing for
negative argument).  Acts only on the current and/or previous
line, in order not to be too devastating in case of being
mistakenly invoked."
  (interactive "p")
  (save-excursion
    (let ((limit (line-beginning-position 0)))
      (dotimes (i count)
	(when (search-backward "0" limit t)
	  (delete-char 1)
	  (insert "-"))))))

(global-set-key (kbd "C-=") 'zero-to-hyphen)

(Notice the funny thing about C-=: it’s unused in stock Emacs - how handy!)

And having written that, somehow it seems that I finally learned to type hyphens on my keyboard, so it will probably get kicked out of my .emacs someday.

CategoryEnglish, CategoryBlog, CategoryEmacs