sponsor Vim development Vim logo Vim Book Ad

SwapText : Mappings to exchange text with the previously deleted text.

 script karma  Rating 13/4, Downloaded by 1536  Comments, bugs, improvements  Vim wiki

created by
Ingo Karkat
 
script type
utility
 
description
DESCRIPTION
Swapping text areas when there's other text in between (e.g. function
arguments separated by other arguments) is done frequently, yet cumbersome.
One way to do this is by deleting A, selecting B, pasting over it, then going
back to where A used to be and pasting the original B.

This plugin lets you exchange the current selection / line / text covered by a
motion with the previously deleted text, with a short and simple mapping. The
swap can be undone as a single action.

SOURCE
Based on vimtip #470: Map to quickly swap/exchange arbitrary text by Piet
Delport and an enhancement by ad_scriven@postmaster.co.uk.
    http://vim.wikia.com/wiki/Swapping_characters,_words_and_lines

SEE ALSO
- The LineJuggler.vim plugin (vimscript #4140) provides [E / ]E mappings to
  swap lines / the selection with the same amount of visible lines located
  [count] above / below.
- The LineJugglerCommands.vim plugin (vimscript #4465) provides swapping of
  ranges as an Ex :Swap command.

RELATED WORKS
- visswap.vim (http://www.drchip.org/astronaut/vim/vbafiles/visswap.vba.gz)
  uses a visual selection, <C-y>, another selection, <C-x> to swap the two.
- swapstrings.vim (http://www.drchip.org/astronaut/vim/#SWAPSTRINGS) can swap
  all instances of two strings in a range.
- swap.vim (vimscript #3250) can swap around a pivot (e.g. ==) or to WORDs to
  the left / right with <Leader>x / <Leader>X.
- exchange (https://github.com/tommcdo/vim-exchange) defines a cx{motion}
  operator that has to be used twice to exchange the first with the second
  one.

USAGE
First, delete some text (using any normal Vim command, such as "daw",
{Visual}x, or "dt"). Then, visually select some other text, and press
<Leader>x, or use the custom operator <Leader>x{motion}. The two pieces of
text should now be swapped.

{Visual}<Leader>x       Swap the visual selection with the just deleted text.
<Leader>x{motion}       Swap the characters covered by {motion} with the just
                        deleted text.
[count]<Leader>xx       Swap the current [count] line(s) with the just deleted
                        text.
[count]<Leader>X        Swap the characters under the cursor until the end of
                        the line and [count]-1 more lines with the just
                        deleted text.
 
install details
INSTALLATION
This script is packaged as a vimball. If you have the "gunzip" decompressor
in your PATH, simply edit the *.vmb.gz package in Vim; otherwise, decompress
the archive first, e.g. using WinZip. Inside Vim, install by sourcing the
vimball or via the :UseVimball command.
    vim SwapText*.vmb.gz
    :so %
To uninstall, use the :RmVimball command.

DEPENDENCIES
- Requires Vim 7.0 or higher.
- Requires the ingo-library.vim plugin (vimscript #4433), version 1.005 or
  higher.
 

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
SwapText-1.02.vmb.gz 1.02 2016-09-19 7.0 Ingo Karkat - "E790: undojoin is not allowed after undo" may also be raised in SwapText#Operator(); ignore it.
- BUG: When deleting at the end of a line, and swapping with a longer text before it, the swap location is off by one. The EOL position isn't properly detected, because the virtual line length after the paste is used in the condition. Save the deleted virtual line length in deletedVirtLen, and pass that on to s:WasDeletionAtEndOfLine(). Thanks to Marcelo MontĂș for the bug report.
SwapText-1.01.vmb.gz 1.01 2014-07-22 7.0 Ingo Karkat - BUG: <Leader>X includes the newline unless :set selection=exclusive. Thanks to Enno Nagel for reporting this.
SwapText-1.00.vmb.gz 1.00 2014-06-24 7.0 Ingo Karkat Initial upload
ip used for rating: 18.118.2.15

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 the maillist. Help Bram help Uganda.
   
Vim at Github