4

The only times I've ever found a use for transpose-words or transpose-chars is when I accidentally hit one of them unintentionally, in which case a backward-word followed by transpose-words again would undo the problem.

It seems like the use case is something along the lines of accidentally typing food dog while meaning to type dog food, in which case transposition is helpful. For characters, I've always found backspace twice + retyping the two characters just as fast and less error-prone.

However, I have never, ever had this "word dyslexia" problem in my history of using Emacs. Given the relatively "important" key bindings that transpose commands get, it seems like I may be missing an important use case. Am I?

  • When revising text is often useful to be able to swap words around easily. – Andrew Swann Mar 26 '17 at 9:13
5

While transpose-chars is useful for typos in any kind of buffer, I suspect transpose-words is not particularly useful when writing prose but might be useful in programming or dealing with other kinds of text.

Note that transpose-words preserves punctuation. For example (with | indicating point):

  • Starting with [1][2][|3], use M-t to get: [1][3][2|]

  • Starting with foo += |bar, use M-t to get: bar += foo|

  • Starting with one|, two, three, four,, use M-3 M-t to get two, three, four, one|,

The last example uses a prefix argument to jump a word forward 3 places.

Also useful is the 0 prefix argument (M-0 M-t) to exchange the word at point with the word at mark. This can be a faster way to swap a couple words at arbitrary locations then using kill/yank. For example, type:

John, Ringo, George, and Paul|

That looks wrong, so use this sequence to fix it:

C-r rin    ; jump back to Ringo, leaving mark where you started
RET        ; quit the search
M-0 M-t    ; swap word at point (Ringo) with word at mark (Paul)

And voilà:

John, Paul, George, and Ringo|
3

In addition to what other have said:

  1. transpose-sexps (C-M-t) is very handy. I use it quite often in Emacs-Lisp mode.

  2. Think keyboard macros. Any command such as transpose-words can be useful from a macro or from Lisp code. IOW, you might not find it useful to use it only once, as a one-off, but you might find it useful if you have to carry out hundreds of similar transpositions as part of a text or code transformation.

0

I use transpose-chars when fixing bad TeX code (I am an editor for mathematical publications). Authors will often include, for instance, a comma inside math, eg:

So we have $x=y,$ which implies that...

For technical reasons we don't want the comma inside the dollar signs, so rather than killing the two characters and retyping them I place point on the second $ and use transpose-chars. For letters it might not make much difference but it's easier to hit C-t than to type $,.

Admittedly this is a rather specialized use case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.