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 1726  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 [email protected].
    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
The code is hosted in a Git repo at
    https://github.com/inkarkat/vim-SwapText
You can use your favorite plugin manager, or "git clone" into a directory used
for Vim packages. Releases are on the "stable" branch, the latest unstable
development snapshot on "master".

This script is also 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.042 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.03.vmb.gz 1.03 2024-11-13 7.0 Ingo Karkat - Adapt: Plugin broken since Vim 8.2.4242 (put in Visual mode cannot be repeated); need to use v_p instead of v_P command now.
- Adapt: Detection of deletion at the end of the line broken since Vim 8.2.0324; implement workaround to handle all Vim versions. *** You need to update to ingo-library (vimscript #4433) version 1.042! ***
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: 142.132.191.50

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