Learn Vim at your own pace with my self-study Core Vim Course.

Learn more

Learn Vim at your own pace with my self-study Core Vim Course.

Swapping two regions of text with exchange.vim

#65

Run time:

Swapping two regions of text is a common task, which normally requires that we make two separate changes to the document. Tom McDonald’s exchange plugin offers an elegant alternative, by providing an operator that swaps two regions of text in one go.

Shownotes

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

Comments

Level-up your Vim

Training

Boost your productivity with a Vim training class. Join a public class, or book a private session for your team.

Drew hosted a private Vim session for the shopify team that was one of the best workshops I have ever attended.

John Duff, Director of Engineering at Shopify

Publications

Make yourself a faster and more efficient developer with the help of these publications, including Practical Vim (Pragmatic Bookshelf 2012), which has over 50 five-star reviews on Amazon.

After reading it, I've switched to vim as my default editor on a daily basis with no regrets. ★★★★★

Javier Collado

Learn to use Vim efficiently in your Ruby projects

In association with thoughtbot, one of the most well respected Rails consultancies in the world, I've produced a series of screencasts on how to make navigating your Ruby projects with Vim ultra-efficient. Along the way, you’ll also learn how to make Ruby blocks a first-class text object in Vim. This lets you edit Ruby code at a higher level of abstraction. Available to buy from thoughtbot..