swap parameters : swap parameters of a function (requires python)
script karma |
Rating 64/26,
Downloaded by 2532 |
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. |
|
script versions (upload new version)
Click on the package to download.
ip used for rating: 18.222.218.204
|