Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a Visual Studio toolbar button for a command that is only available as a keyboard shortcut

This question relates to this ReSharper YouTrack issue.

In Visual Studio 2010 with ReSharper 7.1.1 installed, if I go to Tools > Options > Environment > Keyboard, there is a command called ReSharper_SilentCleanupCode.

I would like to bind this command to a toolbar button.

This seems to be impossible using Tools > Customize > Commands because the only commands available within this dialog are for actions that already have an associated menu item. The particular ReSharper command I'm interested in (Silent Code Cleanup) doesn't appear in any menu, so it cannot be assigned to a toolbar button using the "GUI".

Is there any other way to bind a keyboard-only command to a toolbar button? (One of ReSharper's programmers thought the "VS script editor" could be used, but I'm not having any luck finding info on this.)

Edit

I should have mentioned this in the first place. While azhrei's macro solution is great for Visual Studio 2010, it will break once I upgrade to VS 2012, because macros are no longer supported. If someone has a solution that will continue to work in VS 2012, that would be preferable. (Or perhaps VS 2012 toolbars don't have the same limitation in the first place?)

like image 482
devuxer Avatar asked Mar 13 '13 19:03

devuxer


People also ask

How do I add keyboard shortcuts to Visual Studio?

On the menu bar, choose Tools > Options. Expand Environment, and then choose Keyboard. Optional: Filter the list of commands by entering all or part of the name of the command, without spaces, in the Show commands containing box. In the list, choose the command to which you want to assign a keyboard shortcut.

How do I add a toolbar in Visual Studio?

Add, remove, or move a toolbarOn the menu bar, choose Tools > Customize. The Customize dialog box opens. On the Toolbar tab, perform one of the following sets of steps: To add a toolbar, choose the New button, specify a name for the toolbar that you want to add, and then choose the OK button.

How do I get keyboard shortcuts for Visual Studio Code?

You can view the currently active keyboard shortcuts in VS Code in the Command Palette (View -> Command Palette) or in the Keyboard Shortcuts editor (File > Preferences > Keyboard Shortcuts).

How do I run a shortcut key in Visual Studio?

In VS Code, you only need to use a shortcut to run your code. That shortcut is Ctrl + Alt + N. There are a few more ways to run code. Pressing F1 and then choosing “Run Code” also works.


1 Answers

Add a macro that executes the command, then add the macro to a toolbar.

This works because it makes the keyboard-only command appear in the Macros menu in the Customize Commands dialog.

Details

Add a macro which does this:

    Sub _ReSharper_SilentCleanupCode()
        DTE.ExecuteCommand("ReSharper_SilentCleanupCode")
    End Sub

Put this macro in a module which appears in Customize..Commands..AddCommand..Categories..Macros, such as Samples or MyMacros.RecordingModule, but not MyMacros.Module1 (the default when using the macro IDE).

Go to Tools..Customize..Command and select the Toolbar you want.

Now Add Command... and select the Macros category.

Select your Macros.Samples._ReSharper_SilentCleanupCode macro.

Click Modify Selection and change the name to #-) or whatever text makes you think ReSharper Silent Code Cleanup without being too long for your toolbar. :-)

I tried this with Visual Studio 2010 and ReSharper 7.1.2.

Edit

Visual Commander is a apparently way to get this going on VS2012 as well - see comments below for more.

like image 190
azhrei Avatar answered Sep 24 '22 01:09

azhrei