Here is another command (from library misc-cmds.el
) that does pretty much what you request. Just bind it to a single key (C-M-t
or whatever). Use it to move a sexp both forward and backward repeatedly.
A negative prefix arg transposes backward, like transpose-sexp
, but it also leaves point ready to do another backward transposition. And when repeating, a negative prefix arg just flips the direction.
So if you bind the command to, say, C-o
, then C-o C-o C-o
C--
C-o C-o
C--
C-o
moves the sexp that is to the left of point to the right three times, then to the left twice, then to the right once.
A numeric prefix arg has an effect only for the first use, i.e., not when repeating - when repeating, the movement is always one sexp at a time.
(defun reversible-transpose-sexps (arg)
"Reversible and repeatable `transpose-sexp'.
Like `transpose-sexps', but:
1. Leaves point after the moved sexp.
2. When repeated, a negative prefix arg flips the direction."
(interactive "p")
(when (eq last-command 'rev-transp-sexps-back) (setq arg (- arg)))
(transpose-sexps arg)
(unless (natnump arg)
(when (or (> emacs-major-version 24)
(and (= emacs-major-version 24) (> emacs-minor-version 3)))
(backward-sexp (abs arg)) (skip-syntax-backward " ."))
(setq this-command 'rev-transp-sexps-back)))
(Note: The behavior of transpose-sexps
changed in 24.4, hence the version test here. Prior to 24.4, point ended up in the right place to continue (e.g. repeat). Starting with 24.4, point is in the wrong place for that. Whether that is a regression or adds a useful feature or fixes some other bug is presumably in the eye of the beholder. ;-) I filed Emacs bug #20698 for this, just now.)