Skip navigation

Tag Archives: keyboard macro

When it comes to swap words, emacs has the command “transpose-words“. Pressing the key binding M-t the word will shift forward, many times if you keep pressing those keys. Now, in order to do it backwards, if we want it shifted several times -as if it were “bubbling” back across the paragraph-, a negative argument is not practical to type every time. Hence, here’s my handy binding for a macro that adds the argument “-1” to transpose-word to do just that when pressing M-C-t.

;; to transpose words backwards without having to type the negative argument
(global-set-key (kbd "C-M-t") "\C-u\ -1\ \M-t")

;;reset the original key binding (transpose-sexps) for when in emacs-lisp and lisp-mode
(add-hook 'lisp-mode-hook
(lambda()
(local-set-key (kbd "C-M-t") 'transpose-sexps)
))

(add-hook 'emacs-lisp--mode-hook
(lambda()
(local-set-key (kbd "C-M-t") 'transpose-sexps)
))