sponsor Vim development Vim logo Vim Book Ad

transpose-words : Swap two words as M-t (transpose-words) in Emacs or bash.

 script karma  Rating 43/12, Downloaded by 1257  Comments, bugs, improvements  Vim wiki

created by
fermat 618
 
script type
utility
 
description
Swap two words. Simulate the M-t (transpose-words) in Emacs or Bash.

Usage:
  Just press <M-t> (Alt-t) in insert mode, normal mode or command-line.
  count is also supported. 8<M-t> will push the word befor the cursor right 8 times
  repeat.vim (vimscript #2136) is also supported. When it present, press dot
  (.) in nornal mode will repeat the this function.

Note:
In gvim, <M-t> may open menu, this can be disabled by
:set winaltkeys=no
In the console, <M-t> may just doesn't work, you can enable it by
:exec "set <M-t>=\<Esc>t"
In some terminal such as gnome-terminal, <M-t> may open the menu of the
terminal, this can be disable in the configuration of the terminal.

or you may change the <M-t> to other keys such as <Leader>t
:nmap <Leader>t <Plug>Transposewords
:imap <Leader>t <Plug>Transposewords
:cmap <Leader>t <Plug>Transposewords
Typically, <Leader> is slash '\', you can press \t to do the same work.
Put these in startup file as you like.

This script act more liky M-t in Bash than M-t in Emacs.

I have checked http://vim.wikia.com/wiki/VimTip47 and swap (vimscript #3250.)
In these solutions, either changes cannot be undo nicely, or the search history
are changed, and many other difference from the M-t in bash.

Advantages:
  1. `foo| != ba|r`: when the cursor is between two bar ('|'), and M-t
     pressed, `foo` and `bar` will be transposed. (same as in bash)
  2. `bar foo|`: when the two words are transposed, the cursor will be
     right after the second word `foo`. (same as in bash)
  3. Correctly undo the cursor position.
  4. Does not change the search ('/' or '?') history.
  5. Also work on command-line.
     Note: On a input() command-line with '=' prompt, it does not work.
  6. The pattern which match the words are customizable. (default '\k\+',
     set g:transwrd_wordpattern in startup file.)
 
install details
Just put it in to the plugin directory. (e.g. ~/.vim/plugin)
 

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
transwrd.vim 1.1 2011-07-07 6.0 fermat 618 count is supported now. 8<M-t> will do the right thing.
repeat.vim (vimscript #2136) is supported.
transwrd.vim 1.0 2011-07-06 6.0 fermat 618 Initial upload
ip used for rating: 3.144.250.169

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