Create Your Own Shortcuts

Create Your Own Shortcuts

Visual Studio lets you do a lot with the keyboard configuration. You can customize the keyboard mapping and even copy these mappings from one machine to another.

Keyboard shortcut keys are a big part of working with Visual Studio. Throughout this book, we have identified a number of different shortcuts that are in Visual Studio by default, but in this hack you will learn how to create new shortcuts as well as edit the existing shortcut keys. Visual Studio provides an excellent interface to edit and add new shortcuts. Everything in Visual Studio works off the idea of commands. Although this is a fairly common pattern, with Visual Studio you see it much more, since you can create shortcuts for various commands and can also see commands when using the command window [Hack #46] .

Visual Studio includes hundreds of different commands. A small portion of these commands already have shortcut keys, but through the Options screen (Tools Options), you can add a shortcut for any of these commands. You can also remove shortcuts that you never use, freeing up more key combinations for shortcuts you might use more often.

Adding a Custom Keystroke

In this example, you will create a shortcut for the Build.RebuildSolution command, which does not normally have a shortcut assigned to it. The first thing you need to do is go to Tools Options and select the Keyboard item under the Environment folder. This brings up the interface (Figure) you will use to edit and create shortcuts.

Keyboard mapping options

Visual Studio does not allow you to make changes to the default keyboard mappings. You need to first create a copy of the default keyboard mappings for yourself. Start with whatever keyboard mapping you are currently using, then click the Save As button. This will prompt you to name your new scheme. After you create a name, Visual Studio will save the scheme and set it as your selected scheme. Now you are ready to modify shortcuts.

The next step is to find the command that you want to create a shortcut for. This is done in the long listbox of commands. Thankfully, you can enter part of the command and the list will find it for you as you type. In this example, you are going to select the Build.ReBuildSolution command from the list. To add the shortcut:

  1. Click in the Show Commands Containing box and type Build.Re. The list will narrow to just a few commands. Click once on Build.ReBuildSolution to select it.

  2. Next, you need to decide at what scope you want your shortcut to operate. This is configured using the drop-down list under the "Use new shortcut in:" heading. Leave the scope set to Global so the shortcut will work everywhere, but for more specific shortcuts, you would limit the scope to a particular designer or editor. (For instance, you might have a shortcut that is specific to ASP.NET so you would want it to be valid only while using the HTML Editor Design View.)

  3. Your last step is to decide what shortcut key you want to assign to the command. This is more difficult than it sounds. Because of the number of commands and current shortcuts, it can be a task to find an empty shortcut. You can test shortcut keys by typing them into the Press Shortcut Key(s) box. The box below will then be populated with any current mappings for this shortcut key combination. My initial instinct for this rebuild command was Ctrl-Shift-R, since Ctrl-Shift-B is used to build a solution, but Ctrl-Shift-R is already used to record a macro. You could eliminate one of the current mappings by simply overwriting it, but instead I am going to use the unused Ctrl-K, Ctrl-R.

Visual Studio supports keystroke chords . Similar to musical chords, keystroke chords are the combination of two or more key combinations in quick succession. The Ctrl-K, Ctrl-R shortcut is performed by pressing Ctrl-K and then Ctrl-R immediately after it. This is a testament to the number of shortcuts already existing in Visual Studio that they came up with this solution. Ctrl-K is the default first key for chords, but you can create chords using any keys that you want. The only thing to remember is that after a key is used as the first part of a chord, it can no longer be an individual shortcut key because Visual Studio will be listening for the second part of the chord.

In this book, chords will be shown separated by a comma, as in Ctrl-K, Ctrl-R.

Figure shows your Options screen right before you click Assign to add your new shortcut key.

Adding a new shortcut key for Build.RebuildSolution

Once you click the Assign button, your new shortcut will be added to your keyboard mapping and you can start using it right away. Creating shortcut keys is something that you should do over time as you notice yourself accessing something over and over again through the menu structure. Everything is faster with the keyboard, especially when writing code. Developing your own set of keyboard shortcuts over time is a great way to improve the speed of your development.

Hacking the Hack

So now that you have spent time to create your own set of shortcut keys, tailored to your own style of development, what happens when you switch machines? While there is a way to save and move keyboard settings manually, it is much easier and less troublesome to use the excellent power toy VSTweak to move the settings for you. VSTweak provides an interface to export and then import keyboard mapping schemes quickly and easily.

The VSTweak power toy is one of the more useful power toys for Visual Studio and is the subject of a number of different hacks in this book. The VSTweak power toy can be downloaded from

After you have downloaded and installed the VSTweak power toy, look for the section dealing with keyboard mapping schemes—it's on the first tab, as shown in Figure.

VSTweak keyboard mapping schemes

VSTweak provides an easy-to-use interface with which you can export your keyboard bindings by selecting them in the drop-down list and then clicking the Export button. VSTweak will save a .vsk file to the location of your choosing. You then need to copy this file to your other system, fire up VSTweak, then use the "Import and select keyboard bindings" button to find and import that file.

VSTweak makes this process relatively easy and painless, but if you wanted to do it manually, you could. The .vsk file is stored in your own profile located in the following directory: \Documents and Settings\<username>\Application Data\Microsoft\VisualStudio\<7.1>. You can take this .vsk file and copy it to another machine. When you restart Visual Studio, you will see the new scheme in the Keyboard Options window and you can select it.

     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows