sponsor Vim development Vim logo Vim Book Ad

swap parameters : swap parameters of a function (requires python)

 script karma  Rating 64/26, Downloaded by 2186  Comments, bugs, improvements  Vim wiki

created by
Kamil Dworakowski
 
script type
utility
 
description
Requires python and Vim compiled with +python option

Basic Usecase: Place the cursor inside the parameter you want to swap
with the next one, and press gs

It is a versatile script to swap parameters of a function
or, generally speaking, elements in any coma seperated list.

key bindings (normal mode):
[count]gs -- where count defaults to 1 -- swap the argument under
             the cursor with the [count] next one
[count]gS -- swap with the previous one


Below are exaples of what happens after pressing gs (equivalent to 1gs).
Prettier version can be found here:
http://blog.kamil.dworakowski.name/2007/09/swap-parameters-script-for-vim.html.
On each line the lefthand side shows the line before typing gs, and
the righthand side shows the effect. The cursor position is depicted
with || symbols. par|m|1 means that the cursor is on the character m.

fun(par|m|1, parm2) ==> fun(parm2, parm|1|)
fun(par|m|1(), parm2) ==> fun(parm2, parm1(|)|)
fun(parm1(|)|, parm2) ==> fun(parm2, parm1(|)|)
fun(parm|1|(arg,arg2), parm2) ==> fun(parm2, parm1(arg,arg2|)|)
fun(parm1|(|arg,arg2), parm2) ==> fun(parm2, parm1(arg,arg2|)|)
fun(parm1(arg,arg2|)|, parm2) ==> fun(parm2, parm1(arg,arg2|)|)
fun(parm1(arg, arg2|)|, parm2) ==> fun(parm2, parm1(arg, arg2|)|)
fun(arg1, ar|g|2, arg3) ==> fun(arg1, arg3, arg|2|)
array[a|r|g1, arg2] ==> array[arg2, arg|1|]
fun(par|m|1[], parm2) ==> fun(parm2, parm1[|]|)
fun(parm1[|]|, parm2) ==> fun(parm2, parm1[|]|)
fun(par|m|1, array[]) ==> fun(array[], parm|1|)
fun(|a|,b) ==> fun(b,|a|)
[(p1, p2|)|, p3] ==> [p3, (p1, p2|)|]


The following lines demonstrate using gS (swap with previous).

fun(parm2, par|m|1) ==> fun(|p|arm1, parm2)
fun(parm2, par|m|1()) ==> fun(|p|arm1(), parm2)
fun(parm2, parm1(|)|) ==> fun(|p|arm1(), parm2)
fun(parm2, parm|1|(arg,arg2)) ==> fun(|p|arm1(arg,arg2), parm2)
fun(parm2, parm1|(|arg,arg2)) ==> fun(|p|arm1(arg,arg2), parm2)
fun(parm2, parm1(arg,arg2|)|) ==> fun(|p|arm1(arg,arg2), parm2)
fun(parm2, parm1(arg, arg2|)|) ==> fun(|p|arm1(arg, arg2), parm2)
fun(arg1, ar|g|2, arg3) ==> fun(|a|rg2, arg1, arg3)
fun(arg1, arg2, ar|g|3) ==> fun(arg1, |a|rg3, arg2)
array[arg2, a|r|g1] ==> array[|a|rg1, arg2]
fun(par|m|1[], parm2) ==> fun(|p|arm1[], parm2)
fun(parm2, parm1[|]|) ==> fun(|p|arm1[], parm2)
fun(array[], par|m|1) ==> fun(|p|arm1, array[])
fun(b,|a|) ==> fun(|a|,b)

A useful, however unexpected by the author, feature of this script is that
on pressing j to move cursor to the line below, the cursor is restored to
the column position before the swap. This allows for a streamlined
swaping of parameters in the case like this:

fun(arg2, arg1)
fun(arg2, arg1)
fun(arg2, arg1)

You would put cursor on arg2, and type gsjgsjgs
 
install details
To install put the script file into plugin directory.
 

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
swap_parameters.vim 1.1.3 2008-03-07 7.0 Kamil Dworakowski now it is even more robust
swap_parameters.vim 1.1.2 2008-02-07 7.0 Kamil Dworakowski A yet another defect fixes release.
swap_parameters.vim 1.1.1 2008-01-29 7.0 Kamil Dworakowski Now also swap imports:
from os import |p|open, path --> from os import path, pope|n|
swap_parameters.vim 1.1 2008-01-19 7.0 Kamil Dworakowski Now the script will also cope with coma separated list that is not enclosed in brackets, which is common in Python code.

for |a|, b in some_dict.items()  -->    for b, |a| in some_dict.items()

Moreover, I have made the code (with full suite of tests) available on code.google.com. Link is on my blog: http://blog.kamil.dworakowski.name/2008/01/smarter-swap-parameters-for-vim.html.
swap_parameters.vim 1.0 2007-09-28 7.0 Kamil Dworakowski Initial upload
ip used for rating: 3.142.96.146

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