The plugin transwrd.vim allows to to easily swap two words by pressing <A-t> while having cursor on the latter word. For example: two words becomes words two.

I think the pattern which designates a word is contained in the g:transwrd_wordpattern variable and is (as default) the following: \k\+

Is it possible to define an advanced pattern which allows to be more programming language savvy (while still working for normal words)? Eg.

param[0], param[1]  -->  param[1], param[0]
int call1()         -->  call1() int

This way it could also be used to swap function arguments (related to this other question).


Yes, if you can come up with a regular expression that will, alone, capture everything you consider to be a "word." The plugin uses the pattern stored in g:transwrd_wordpattern to identify words by finding two matches and then swapping the characters in the matched ranges.

Doing let g:transwrd_wordpattern='[a-zA-Z0-9\[\]\(\)]\+' will, for example, let it match both of your examples.

  • Good! Thinking about it, I included in the list between [ and ] after the 9, the char _ in order to consider as "word" identifiers like foo_bar – Giovanni Maggiorani Feb 10 '15 at 20:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.