sponsor Vim development Vim logo Vim Book Ad

intermediate Tip #329: A map for swapping words

 tip karma   Rating 174/57, Viewed by 4881 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   September 20, 2002 9:27      complexity:   intermediate
author:   Chip Campbell, Arun Easi, Benji Fisher      as of Vim:   6.0

Put the following map into your <.vimrc>:

  nmap <silent> gw "_yiw:s/\(\%#\w\+\)\(\W\+\)\(\w\+\)/\3\2\1/<cr><c-o><c-l>

Then when you put the cursor on or in a word, press "gw", and
the word will be swapped with the next word.  The words may
even be separated by punctuation (such as "abc = def").

While we're talking swapping, here's a map for swapping characters:

  nmap <silent> gc    xph

This hint was formed in a collaboration between
Chip Campbell - Arun Easi - Benji Fisher

 rate this tip  Life Changing Helpful Unfulfilling 

<< Vim in Microsoft Visual Foxpro | how to stop auto indenting >>

Additional Notes

[email protected], September 20, 2002 11:08
Thought I'd mention some of the reasoning behind the gw-map above.  Sequences such as  dawelp suffer from one or more problems:

* fencepost problems: they don't work correctly either at the beginning of a line
  or at the end of a line

* single-character-word problems: mappings using b, e, or variants don't handle
  single-character words properly.
jean upfrontsystems co za, October 5, 2002 11:03
Looks like this doesn't swap "this" and "that" when line 2 starts with "that", and line 1 ends on "this";
that is a problem for another day :)

Just possibly Vim's newline-matching regexps will be able to cope with this .. but it's probably going
too far.
[email protected], December 2, 2002 7:42
Making vim handle word swapping across a newline is
fairly straightforward:

nmap <silent> gw    "_yiw:s/\(\%#\w\+\)\(\_W\+\)\(\w\+\)/\3\2\1/<cr><c-o><c-l>

Just one extra underscore (at a suitable location, of course :) .
[email protected], September 9, 2003 6:48
A map for swapping paragraphs:

   nnoremap g{  {dap}p{

A pair of maps for swapping a word to-the-left and to-the-right:

    nnoremap <silent> gl
    \"_yiw?\w\+\_W\+\%#<CR>:s/\(\%#\w\+\)\(\_W\+\)\(\w\+\)/\3\2\1/<cr><c-o><c-l>

    nnoremap <silent> gr
    \"_yiw:s/\(\%#\w\+\)\(\_W\+\)\(\w\+\)/\3\2\1/<cr><c-o>/\w\+\_W\+<CR><c-l>
[email protected], November 7, 2003 8:47
A plugin for visual-mode based swapping is now available at

    http://www.erols.com/astronaut/vim/index.html#VimFuncs
     under "Visual Mode Based Swapping"

* Use a visual mode to select some text (V, v, or ctrl-v), then press <ctrl-y>
* Use a visual mode to select alternate text, then press <ctrl-x>

The two text regions will then be swapped.

[email protected] - NOSPAM, October 25, 2004 11:43
The url in the tip-note above should now be:
[email protected] - NOSPAM, October 25, 2004 11:44
The url in the tip-note above should now be:
    http://mysite.verizon.net/astronaut/vim/index.html#VimFuncs
Anonymous, October 23, 2006 11:15
This doesn't work for me if the words have accented characters (Vim 7.0).
[email protected] - NOSPAM, January 25, 2007 6:47
To extend the swapping to include glyphs other than [0-9A-Za-z_], which is what \w means (see :help /\w), then instead of \w and \W, use [glyphs you want here]  for \w and  [^glyphs you want here] for \W.
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.
   
SourceForge.net Logo