Thanks to Tim Stewart, I stumbled across the anchored-transpose package. You can think of it as a generalization of the other transpose functions such as transpose-chars, transpose-words, transpose-sexps, and the other less used transposition functions. Instead of transposing around the point, anchored-transpose lets you specify an arbitrary anchor region to pivot around.
Here’s an example of its use: suppose I have
First DO this and then do that.
and I want
First do that and then DO this.
To do that
- Select a region that covers the things you want to swap and everything in between.
- Call
anchored-transpose. - Select the anchor region. That is everything in between what you want to transpose.
- Call
anchored-transposeagain.
For our example, we first select the region
DO this and then do that
and call anchored-transpose. Then select
and then
and call anchored-transpose again.
You can also swap two disjoint regions by selecting one, calling anchored-transpose, selecting the other, and calling anchored-transpose again. You can specify the two regions in any order. You can also select the entire phrase and anchor phrase in the first example in either order.
I often want to swap parts of a sentence or even several sentences so anchored-transpose is a real win for me. I’ve mapped it to 【Hyper+t】 to make it easy to call.
You can get anchored-transpose from Melpa.