Transposing In Emacs

Most Emacs users know about using 【Ctrl+t】 to transpose two letters and 【Meta+t】 to transpose two words but there are two other useful transposition commands.

You can use the sequence 【Ctrl+x Ctrl+t】 to transpose two lines. As with the other transposition commands, it drags the line preceding the point forward so that you can move a line down any number of lines. For example, if you have

Line 1
Line 2
Line 3
Line 4

and the point is at the end of Line 1 or anywhere one Line 2 and you type 【Ctrl+x Ctrl+t】 twice (or give a a prefix of 2) you get

Line 2
Line 3
Line 1
Line 4

The other transposition command is for balanced expressions or sexps. This is a little more useful than you might think—especially if you’re not a Lisp programmer—because of the definition of “balanced expression.” See the explanation of balanced expression in the Emacs manual. To transpose balanced expressions, use the sequence 【Ctrl+Meta+t】.

One final neat feature: if any of these commands is given a repeat count of 0—which nominally makes no sense—then the objects ending after the mark and point are exchanged.

This entry was posted in General and tagged . Bookmark the permalink.