124

In IntelliJ I can easily rearrange the order of statements (or whole methods for that part) by pressing + Shift + (or + Shift + ).

I was wondering whether there's a shortcut to change the order of method parameters as easily, so that

public void sth(String a, String b) {...}

will become

public void sth(String b, String a) {...}

with the stroke of a keyboard shortcut (assuming my cursor is positioned on one of the parameters).

It would be enough for me, if IntelliJ would just reorder the parameters. That is, this need not trigger a whole Refactor > Change method signature thing.

7
  • 1
    How should this behave when having three or more parameters? :) Feb 12, 2016 at 9:03
  • 2
    Say my cursor is on the second parameter: Shifting it up, it should become the first. Shifting it down, it should become the last. Feb 12, 2016 at 9:05
  • 2
    One parameter moved up or down at a time! I'd love to be able to do this featue! Feb 12, 2016 at 9:06
  • 1
    Ctrl+F6 and then Alt+Up or Alt+Down to move a parameter? Feb 12, 2016 at 9:12
  • 1
    Yeah, more often than you would think. When I start to flesh out a method I usually Alt-Enter to add additional parameters when needed. When I'm done I then rearrange the parameters to be in a more logical order. Plus, I think it would be really nice to have this shortcut for method calls as well. (Man, how often did I get the order wrong when working with legacy APIs with more than 4 or 5 parameters of the same type! uhg!) Feb 12, 2016 at 9:41

3 Answers 3

233

Since IDEA 16 EAP, there's an action for that. It's in the menu:

Code | Move Element Left/Right

Keyboard shortcuts are Alt+Ctrl+Shift+Left/Right (Opt+Cmd+Shift+Left/Right for OSX).

See http://blog.jetbrains.com/idea/2016/01/intellij-idea-16-eap-improves-editor-and-vcs-integration/ for more details.

6
  • 6
    Good, but Lyubomyr Shaydariv answer: stackoverflow.com/a/35359882/4024146 is much better, because it do refactor and change a dependencies on changes of parameters. This just switch parameters for this methos, but not method calls from another methods. You don¨t need make macro for it, just CTRL+F6 and use ALT + ↓
    – mtrakal
    Jul 19, 2017 at 14:45
  • 2
    @mtrakal they're two different things. When I'm composing a method, I don't need the heavyweight (slow) refactoring - I just want to easily move parameters. The real problem with this one is that I often have parameters on separate lines, so this doesn't work. (I really want a move-previous and move-next, not a left and right.) Oct 11, 2017 at 15:58
  • 1
    If you have multiple cursor on several parameters of different methods, they can be moved together.
    – WesternGun
    Oct 4, 2018 at 7:33
  • 1
    NOTE: This does NOT change the order of any usage of the parameter! Be very careful.
    – SMBiggs
    Mar 16, 2020 at 18:39
  • 1
    (add-on to @WesternGun comment) vars on multiple lines can be selected with options for Multiple cursors and selection ranges
    – cellepo
    Aug 21, 2023 at 17:15
28

I'm not sure if it's possible in IDEA, but you might try to use editor macros for two-parameter methods:

  • Editor -> Macros -> Start Macro Recording
  • Record your macro:
    • Ctrl+F6 to open the Change Signature dialog
    • Alt+ to swap the first and the second parameter
    • Tab to deactivate the Parameters tab and let the Refactor button get the focus
    • Enter to close the dialog at perform refactoring
  • Editor -> Macros -> Stop Macro Recording and give a name to your macro, let's say "Swap method parameters 1 and 2".

And then access your macro via the Editor -> Macros menu or assign a custom shortcut to it in Settings -> Keymap -> Main menu/Edit/Macros/Swap method parameters 1 and 2. It looks a very dirty and context-free way to me, but maybe it could help you a little.

1
  • This fix the issue for me to refactor around 500~ methods. Nov 5, 2021 at 14:26
21

On Ubuntu 18.04 the default shortcut is Ctrl+Alt+Shift+Super+Left/Right to move an argument left or right in position.

This is as it is setup to not clash with some of the new OS level shortcuts

2
  • 3
    thanks, even though it requires finger gymnastics, this is so much better than needing to bind a separate key.
    – gMale
    Jun 15, 2019 at 21:41
  • Thanks. It's shown as C+A+S+left in Code menu, but grayed out. I was wondering why until seeing this.
    – WesternGun
    May 10, 2021 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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