In episode 54, I covered a technique for swapping two regions of text using the Visual mode put command. It’s a neat workflow, but there’s room for some improvement. This technique requires that we make two changes to the document, and consequently we have to press undo twice if we want to revert the swap.

For an alternative approach, check out exchange, by Tom McDonald. This is a little gem of a plugin! It provides one extra operation, which is mapped to cx by default.

Used in pairs. FIRST USE: define the first region of text to be exchanged. Any valid {motion} can be used. SECOND USE: define the second region of text and perform the exchange.

One pleasing consequence to this approach is that we can revert the change by pressing undo one time.

On the default mappings

I like the default mappings for exchange.vim. I think of it as a back-to-front mnemonic for Ex-Change, and that seems appropriate considering what the mapping does!

In the video, I began by demonstrating {Visual}cx , then went on to demonstrate the cx{motion} operator. There were some problems with using cx as a Visual mode mapping. These have now been resolved by using X as the Visual mode mapping.

Previously, I recommended setting this up by putting this in your vimrc:

let g:exchange_no_mappings = 1 nmap cx < Plug >( Exchange ) vmap X < Plug >( Exchange ) nmap cxc < Plug >( ExchangeClear ) nmap cxx < Plug >( ExchangeLine )

This snippet of Vimscript is no longer necessary - you can get this behaviour by installing the latest version of exchange.vim.

Further reading