How to: Assign a Keyboard Shortcut to a Refactoring
- 2 minutes to read
You can use the ExecuteRefactoring command to run a CodeRush code action (refactoring or code provider) without invoking the Code Actions menu. If two or more code actions are available in the current code context, this command invokes the Code Actions menu where you can choose the desired code action.
Follow the steps below to bind the Caps+K shortcut to the ExecuteRefactoring command:
Choose CodeRush | Shortcuts… from Visual Studio’s menu to open the Shortcuts configuration page.
Note
To use the Caps Lock key as a modifier, ensure this feature is enabled in the Setup Wizard, Quick Setup or IDE | Caps as a Modifier CodeRush options page. To engage/disengage the Caps Lock key, double-tap it in Visual Studio. See the following topic for details: Caps as a Modifier.
Click Add a new folder to create a new folder for custom shortcuts.
In the “New Folder” dialog, specify the folder name, check the “Make this a top level folder” checkbox to create a folder at the root hierarchy level, and click OK.
Select the newly created folder and click Add a new shortcut to create a shortcut.
Specify the Caps+K shortcut in the “First shortcut” text box. Click the ellipsis button to add the Caps modifier to this shortcut.
Choose ExecuteRefactoring from the “Command” combo box.
Type code action names, separated by semicolons, in the “Parameters” text box. If only one of the specified code actions is available in the code context, the ExecuteRefactoring command runs this action. Otherwise, the command invokes the Code Actions menu.
Note
You can also type the “*” character in the “Parameters” text box to include all available CodeRush code actions in this command.
In this example, type the following code actions in the “Parameters” text box:
- The Make Explicit and Remove Type Qualifier refactorings.
- The Declare Method and Declare Parameter code providers.
You can find names of supported code actions (refactorings and code providers) on the Editor | Language | Code Actions | Code Actions Catalog CodeRush options page.
Click OK to apply the changes and close the Shortcuts options page.
Open the code editor and press the Caps+K shortcut key to run the specified code actions for the ExecuteRefactoring command.