How to bind multiple actions to a shortcut

2019-04-28 20:48发布

问题:

Each time I finished editing a Java class, I want to : Organize imports, Reformat code and Rearrange code. I have to hit 3 keyboard shortcuts.

Is there a way to assign a chain of actions to a single shortcut ?

I am using IntelliJ 14.1.5.

回答1:

You can record a macro consisting of the actions you want to perform and assign a shortcut to that macro.

The process is described here and here. For the sake of completeness, here are the steps from these online guides (credit goes to Jetbrains):

To record a macro

  • On the main menu, choose Edit | Macros | Start Macro Recording. From that moment on, all your recordable actions are recorded.
  • When you are done with the procedure, choose Edit | Macros | Stop Macro Recording.
  • In the Enter Macro Name dialog, specify the name of the new macro, and click OK. If the macro is intended for temporary use only, you can leave the name blank.

To bind a macro with a keyboard shortcut

  • Open the Settings dialog and click Keymap.
  • Create a new keymap Run/Debug Configuration: PHP Script or select an editable keymap from the list of keymaps.
  • Expand the Macros node and select the macro for which a keyboard shortcut should be created.
  • Right-click on the macro and choose Add Keyboard Shortcut in the context menu.
  • In the Enter Keyboard Shortcut dialog, press the keys to be used as a shortcut. The keystrokes are immediately reflected in the First Stroke field. Optionally, select the Second stroke check box and specify the second stroke. As you press the keys, the Preview field displays the keystrokes you pressed, and the Conflicts field displays warnings, if the keystrokes are already in use.
  • Click OK using the mouse pointer to create a shortcut and bind it with the macro.
  • Click Apply to save the settings.