sponsor Vim development Vim logo Vim Book Ad

intermediate Tip #470: Map to quickly swap/exchange arbitrary text

 tip karma   Rating 47/22, Viewed by 1322 

created:   May 5, 2003 10:31      complexity:   intermediate
author:   Piet Delport      as of Vim:   5.7

When editing text, it's not uncommon to need to swap two bits of text.  In
Vim, it's easy to swap adjacent characters, words and lines (see vimtip #47),
but to swap non-adjacent text (such as comma-separated words, variable
assignments, and function parameters), you usually have to resort to a tedious
delete/move/put/delete/move/put sequence.

Mappings such as the one in vimtip #329 can help in some cases, but for a more
general solution, try this mapping:

vnoremap <C-X> <Esc>`.``gvP``P

To use it:  First, delete some text (using any normal Vim command, such as
daw, {Visual}x, or dt,).  Then, visually select some other text, and press
CTRL-X.  The two pieces of text should now be swapped.

For example, to swap "apple" and "orange" in the line below:

        int apple, lemon, orange;

1. Delete "apple", using (for example) diw or <double-click>d
2. Visually select "orange"
3. Press CTRL-X

The mapping is not limited to single words, though, and will work with
anything you can delete/select (even lines and blockwise selections).

 rate this tip  Life Changing Helpful Unfulfilling 

<<The use of  %< instead of % | Bridging the worlds: putting your rodent to work for vim in xterms >>

Additional Notes

[email protected], May 6, 2003 1:27
Hi I've needed this about a billion times!! zzapper

How it works (AFAIK)

vnoremap <C-X> <Esc>`.``gvP``P

<ESC> disappears visual selection
`.          return to exact spot of last modification (the deleted text)
``          jump back to where you were (exactly)
gv         re-highlight last visual text
P         Put/paste last deleted text over visually selected text
``         Toggle to where text was deleted
P         Visually Selected Text now in default register so just paste it.

Notes:
Note use throughout of BACK tip `
When you initially delete text , don't delete the trailing space
<C-X>  can still be used for decrement
[email protected], May 7, 2003 3:18
This is a nice map. However it breaks in the case where the
first word you delete is on the same line as the one you
highlight and is further to the right (vim shifts the `.
mark about). Here is a horrible fix. I'm sure someone else
can do better. Replace ^V with ctrl-V.

vnoremap <silent> <C-X> <Esc>`.``:exe line(".")==line("'.") && col(".") < col("'.") ? 'norm! :let c=col(".")^V^V^V^V<CR>gvp```]:let c=col(".")-c^V^V^V^V<CR>``:silent call cursor(line("."),col(".")+c)^V^V^V^V<CR>P' : "norm! gvp``P"<CR>

Antony
[email protected], May 7, 2003 7:38
Good catch.  The problem doesn't seem to be caused by Vim moving the `. mark
about, though, but rather by the fact that Vim *doesn't* move it.

IOW, when you change a line by inserting/deleting characters, any marks to the
right of the change don't get adjusted to correct for the change, but stay
pointing at the exact same column as before the change (which is not the right
place anymore).

It seems there's already a todo-item for this (line 2998 in todo.txt):

7   Try to keep marks in the same position when:
    [...snip...]
    - inserting/deleting characters in a line.
[email protected], May 7, 2003 12:32
Yeah, that's what I mean't! The mark stays at the same
column (it moves relative to the text, in my mind). Didn't
realise it was in todo.txt. Same thing happens with visual
selection, because the `< and `> stay put relative to the
screen.

Makes this sort of thing tricky. You can't (as far as I can
see in this context) insert markers in the text to find the
correct place with / because this makes the `. mark wrong.

Antony
[email protected], June 4, 2003 14:48
Why is it that mapping never works for me :((((
I tried doing this stuff but nothing happened.
I am using 6.1. Any HELP?
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
Sponsored by Web Concept Group Inc. SourceForge Logo