Skip navigation

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)
))
Advertisements

2 Comments

  1. Nice. Following your lead I’ve done similar, but I’ve set it to Meta-Shift-T.

    (global-set-key (kbd “M-T”) (lambda () (interactive) (transpose-words -1)))

    I particularly like transpose-words because it works perfectly for shifting items in comma-separated lists.

    • Glad you liked it Ed! ,and thanks for sharing your variation.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: