05Mar

Vi’s “paren bounce” feature for Emacs

Posted by Elf Sternberg as Uncategorized

I need to make clear that I did not invent the code below. I’m not sure who did, but the topic of making vi’s “paren bounce” feature work in Emacs came up in a conversation I had the other day, and I’m posting the chunk of code that’s lived in my .emacs file since, oh heck, must have been back in my days at F5 Networks.  This code creates a new function, “paren-bounce”, and binds it to the key sequence “CTRL-%” (I know, that’s probably not a real control code, but Emacs Doesn’t Care).

(defun paren-bounce ()
  (interactive)
  (let ((prev-char (char-to-string (preceding-char)))
	(next-char (char-to-string (following-char))))
    (cond ((string-match "[[{(<]" next-char) (forward-sexp 1))
          ((string-match "[\]})>]" prev-char) (backward-sexp 1))
          (t (error "%s" "Not an expression boundary.")))))

(global-set-key [(control ?%)] 'paren-bounce)

Similar solutions can be found here and here. I suspect the latter is where I got mine from– there are minor differences, but his page hasn’t been updated since 2001, so that’s about right.

Comment Form

Subscribe to Feed

Categories

Calendar

March 2010
M T W T F S S
« Feb   Apr »
1234567
891011121314
15161718192021
22232425262728
293031