You probably know ddp command in Vim to exchange two lines. But what if you want to exchange only parts of these lines?">

Macro to excange two values in Vim

Created: 2013-01-09 — modified: 2013-01-09 — tags: vim

You probably know ddp command in Vim to exchange two lines. But what if you want to exchange only parts of these lines?


For example, you have two parameters:
Parameter=Value
Another parameter=some other value
And you want to exchange their values to get this:
Parameter=some other value
Another parameter=Value

If you might need to do it more than once and your level of laziness (a.k.a. urge to optimize stuff) is somewhat close to mine, you might find this Vim command useful:
^f=l"qDj^f=l"wD"qpk^f="wp
It does exactly that.

To use it:
  1. Copy-paste above line into Vim
  2. Copy it into an internal Vim register ("xdd to cut current line to register x)
  3. Put cursor somewhere in the first line and execute macro stored in your register (@x)
As you probably know, Vim uses same registers both for copy-paste operations and for macros. Also it saves them between sessions in viminfo file, nice!