mutewinter / swap-parameters

Swap parameters of a function or a comma separated list with a single command.

Home Page:http://www.vim.org/scripts/script.php?script_id=2032

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Alert: Better Plugin Exists

Just use vim-swap it's so much better. It even has an interactive mode.

Description

Swap parameters of a function or a comma separated list with a single command.

Usage

In normal mode, these key commands will swap parameters:

  • [count]gs Swap the parameter under the cursor with the next one.
  • [count]gS Swap the current parameter with the previous parameter.

Note: [count] defaults to 1.

Installation

Note: Requires vim compiled with python support. To check if you have python support, run vim --version | grep python and see if it says +python.

Vundle

  1. Add Bundle 'mutewinter/swap-parameters' to your .vimrc file.
  2. Restart Vim.
  3. Now run :BundleInstall.
  4. Enjoy.

Customization

You can change mappings by using the g:SwapParametersMapForwards and g:SwapParametersMapBackwards variables:

g:SwapParametersMapForwards = '<leader>s'
g:SwapParametersMapBackwards = '<leader>S'

Examples

Below are examples of what happens after pressing gs (equivalent to 1gs). On each line the left side shows the line before typing gs, and the right 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|)|]
 for |a|, b in some_dict.items()        for b, |a| in some_dict.items()

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(parm2, par|m|1[])                  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)
 for a, |b| in some_dict.items()        for |b|, a in some_dict.items()

The above examples are auto-generated from the tests.

Dot repeats don't work with this binding but pressing gs is quick enough I think.

The column position of the cursor is preserved when you go to the next line after swap. This allows for streamlined swapping of parameters in the case like this:

 fun(arg2, blah)
 fun(arg2, blahble)
 fun(arg2, blahblahblah)

You would put cursor on arg2, and the gsjgsjgs

This script is written in python. Therefore it needs Vim compiled with +python option (:version), as well as Python installed in the system.

About

AuthorKamil Dworakowski <kamil-at-dworakowski.name>
Updated ByJeremy Mack @mutewinter
Version1.2.1
Last Change2012-09-06
URLhttps://github.com/mutewinter/swap-parameters
RequiresPython and Vim compiled with +python option
LicenceMIT Licence

About

Swap parameters of a function or a comma separated list with a single command.

http://www.vim.org/scripts/script.php?script_id=2032


Languages

Language:Vim Script 100.0%