(Print this page)

Top 10 Visual Studio Tips and Tricks
Published date: Sunday, December 20, 2009
On: Moer and Éric Moreau's web site

Earlier this month, I presented a session at TechDays Canada. This session was titled “Tips and Tricks for Visual Studio”. I received many good comments on the content of this session (presented first at TechEd by Scott Cate).

Because so many of these tips and tricks are very little known, I have decided to recycle 10 of those “obscure” tips and to introduce them to you. In fact, you will find more than 10 because I grouped some under a single section. Those tips are made of keyboard shortcuts, registry hacks, and macros.

All those tips and tricks have one thing in common: to provide you ways to become more efficient with Visual Studio.

Demo code

There is no demo code to download this month, only tips and tricks.

Requirements

Those tips are guaranteed to work in Visual Studio 2008 but many also work in previous and/or future releases of Visual Studio.

Most tips and tricks explored here are independent of the set of settings you initially chose when you first start Visual Studio after installation. In fact, do you remember what your answer was? Probably not and nothing in the IDE helps you remember. Tip #9 will show you how to remember and modify that selection.

For this article, I assume that you are using the General Settings.

Tip # 10: Exploring related resources

I won’t be able to present you every single tip available in the VS2008 IDE. It would take an entire life! I will focus on 10.

This first tip provides some resources in case you want to continue to become more and more efficient with your favourite tool (no not Notepad, the other one!).

 

All the tips presented here were extracted from Sara Ford blog. She is a Microsoft employee working in the team in charge of testing the VS IDE. She published the tip of day from July 2007 to December 2008 for a grand total of 384 tips. She also published a book containing 251 of them. All the profits from the book are sent to a scholarship fund for anyone living in the author’s hometown of Waveland, Mississippi, which was destroyed during Hurricane Katrina in 2005.

Figure 1: Sara Ford’s book

Scott Cate has started to publish webcast about those tips. He created already about 70 of those short webcasts. If you find a trick number from Sara, you can try this URL to see if the webcast is available: http://ScottCate.com/Tricks/000 (replacing the 000 with the tip number from Sara’s blog).

Another great resource you should keep close to your monitor(s) is the key binding reference poster. These posters are available for major languages (as some shortcut combinations vary from one language to another). These posters are available from here for VB.Net and from here for C#.

When you start learning new tricks, don’t try to read them all in a single day, you will probably forget 90% of them. Instead, use some learning tricks like:

  • Reading 2 or 3 every day and try to use them during that day (which is not always possible)
  • Stick 5 to 10 post-it on your monitor as a reminder of what are the shortcut combinations. When you master one, remove it and replace it with a new one.
  • Add the shortcut combinations to the tooltip displayed by the Visual Studio IDE. To enable them (because they are off by default), right-click on your toolbar and select “Customize” (the last item), ensure you are in the “Toolbars” tab and check the “Show shortcut keys in ScreenTips” as shown in figure 2. Every time Visual Studio will show a tooltip, the shortcut keys for this action, if any, will also be displayed to help you remember.

Figure 2: Enable show shortcut keys

Tip #9: Remembering and resetting settings

Portion of this content is from Sara Ford tips #229, and #231.

Some shortcut combinations vary according to the default settings you selected when you launched Visual Studio the first time. For example, the “Step Into” is very different depending on that setting.

If you want to remember it, you can wear your white gloves and open the registry (with RegEdit.exe). Navigate to HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0\Profile and check the value of the LastReadSettingsFile (as shown in Figure 3).

Figure 3: Last loaded settings file

Now that you know which default settings and want to change them or simply to backup your current settings to restore on another PC (when you buy a new one for example), Visual Studio gives you the tools required to that.

It as simple as opening the “Import and Export settings” item from the Tools menu. When you open this dialog, you will be offered 3 options:

  • Export selected environment settings
  • Import selected environment settings
  • Reset all settings

Each option well you guide you through the step required. The nice thing to note here is that if you select the “Reset all settings” option, you will be given the choice of selecting your default settings again just like you were asked the first time you started Visual Studio.

Tip #8: Block-style selection & blank line copy & Clipboard ring

Portion of this content is from Sara Ford tips #2, #50, and #51.

Copy and paste is a very usual operation. Personally, I might be the king!

Everybody knows that you can highlight just about anything (a character, a word, portion of a line, a couple of lines) by using the Shift key and the arrow key to enlarge your selection or by using the mouse. This is called the “Stream selection mode”.

Do you know that without highlighting anything, you can press CTRL-C and the full line will be copied to the clipboard? But what happen if your cursor is positioned on a blank line? A blank line is copied to clipboard. I sincerely find it pretty useless. You can change that behaviour to stop Visual Studio to send blank lines to the clipboard by opening Tools->Options->Text Editor->All Languages->General. Uncheck the “Apply cut or Copy commands to blank lines when there is no selection” and blank lines won’t be copied to the clipboard anymore.

But there is also the “Box selection mode” which works the same way but requires you to press the ALT key before your regular action (ALT-SHIFT-Arrows or ALT-Mouse select). This will let you select a box or a block. This selection, just like the regular one, can be copied or cut. Nothing is new here. Visual Studio 2010 will even let you type to override your selection (Scott Cate made a nice screen cast demoing this new feature).

Finally, Visual Studio offers a clipboard ring (just like Office) except that there is no dialog to show the content of the clipboard. Instead of the plain old CTRL-V to paste the latest item sent to the clipboard, try the SHIFT-CTRL-V keys continuously. This will let you cycle through the latest 20 items. This can be really handy when you need to copy a couple of non-contiguous lines from one place to another.

Tip #7: Incremental search and other search tips and tricks

Portion of this content is from Sara Ford tip #1.

We are always searching for code, at least I do. Visual Studio offers many ways of searching for code.

The first one, and surely the most known is the famous CTRL-F. So when you press this shortcut, a dialog is displayed letting you enter a value. After you pressed Enter, the first occurrence is highlighted and the dialog remains on the screen for you to search for the next occurrence. I was always pressing the ESC key to close that box until I discovered this tip. If you open Tools->Options->Environment->Find and Replace, you will find an option called “Hide Find and Replace window after a match is located”.

You might wander what I do if the Find dialog is closed and I want to continue searching for the next occurrence? I just press F3 (or SHIFT-F3 for the previous one). So there is really no need to have the Find dialog remain on the screen.

If the expression you want to search is visible on the screen, just highlight it and press CTRL-F3 (or SHIFT-CTRL-F3 for previous). This shortcut will automatically search for the next (or previous) occurrence without displaying any dialog. One thing you really need to be aware about this shortcut is that it will always do a case sensitive search.

Finally, there is an incremental search feature available in Visual Studio. To try it, press CTRLF-I (or SHIFT-CTRL-I for previous) and start typing something. Automatically, the mouse cursor will be changed to something really different indicating that you are in the incremental search mode. As long as you will keep typing letters, the editor will position itself to the next occurrence if any. You can even press Backspace to remove some characters. The status bar (bottom-left) also shows the search string. When you are done, just press ESC to exit the incremental search mode.

Tip #6: Customizing search results

Portion of this content is from Sara Ford tip #363.

I hope you are also aware about the “Find in Files” feature (or SHIFT-CTRL-F) that lets you search for something throughout the current document, or the current project, or the currently open files, or even the entire solution. The nice thing about this search feature is that the results are displayed in a dialog so you can easily see where the “something” you were looking for is actually found.

An easy way to navigate in this list is to use F8 (or SHIFT-F8 for reverse) and the editor will automatically move to the correct line of code.

In my opinion, one bad thing about the results is that the full path of the file is actually shown which can take about half of the screen width in some projects. But you can modify it to something more readable. Once again, you need to open the registry and add a value. Open the “HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0\Find” section and add a new string value named “Find result format” (yes with spaces) and set its value to “$f$e($l,$c):$t\r\n” (without the quotes). This esoteric value stands for “file.extension (line, column): found text and newline”. If you want to see the full list of the available tokens, visit Sara’s blog post.

Tip #5: Using the “Magic box”

Portion of this content is from Sara Ford tip #91.

What I call my “magic box” can be found as the “Go to find box” in Visual Studio but it does so much more than just finding something but it doesn’t appear in any menu, it can only be found in the toolbar! I even saw some brand new installation without it.

Figure 4: The Magic box

So the first thing I will show you is how to add this wonderful feature to a toolbar so you can (ab)use from it. First, open Tools->Customize->Toolbars and create a new toolbar and call it “Find”. An empty toolbar should now appear on your screen. Now open the Commands tab, select the Edit category and find the “Go To Find Combo” in the list of commands. Drag it to your new toolbar you created. You can also add the “Find in Files” item (introduced in tip #6) to the same toolbar if it is missing from your current set of toolbars.

You can finally dock this toolbar with all of its friends at the top of your screen.

From now on, to quickly access it, you just have to press CTRL-D.

So where is the magic?

To warm up a bit, press CTRL-D and type a word you can find in your current document and press Enter. You just found yet another way to search for text. I can hear you say that there is no magic here! But wait, I am not done on this topic.

Press CTRL-D again, type a line number and press CTRL-G (not enter otherwise it will search for this number). The editor will move to that line in the current document.

Now ensure that the document currently opened in the editor is a valid class. Press CTRL-D again but this time type in the name of one of your methods (C# is case sensitive here) and press F9. That will place a breakpoint on the method signature (the method does not have to be visible on the screen for this tip to work).

Let’s move on. Now close all your documents, press CTRL-D and type the name of one of the file in any project of your solution and press SHIFT-CTRL-G. The file will open. If your solution contains more than one file with the same name, only (and always) the first one will be opened.

If you are not sure about the file name, you can always ask for help. Press CTRL-D again and this time type “>File.OpenFile“ (yes with a > in front which indicates that you want to switch to command mode which remembers me the old DOS prompt). After you press the space bar, you will see the list of all the files available in your current solution. Start typing the name you want and the list will filter (as expected). Select a name and press Enter. The selected file will open. In addition to the File.OpenFile, notice that you have access to the complete list of features in the DTE (Development Tools Extensibility).

Tip #4: “Fake” code snippets

Portion of this content is from Sara Ford tip #191.

This tip is excellent for presenters who want to prepare some section of code but not add them to a ready-made solution (or not rely on Notepad to hold them).

Do you know that you can use the toolbox even when you are editing code? When editing code, the toolbox displays to the General tab which is empty by default.

Highlight a section of code (it can be a full block of code – multiple lines) and drag it to this General tab. Voilà, you just created something you can re-use. Go elsewhere in your code and drag this snippet back to the editor. The nice thing about this General tab is that it is not tied to a project so if you copy something in there, close Visual Studio and re-open it, you will retrieve your snippets whatever projects you open.

When dragging a block of code to the toolbox, it will automatically be named the same thing as the text you pasted. It rarely makes sense. You can right-click on your snippet and select the “Rename item” option to give your snippet a meaningful name (or at least something that can be read easily).

Notice that there is no way of modifying the content of the snippets. If you have to do it, paste your snippet to a code editor, remove it from the toolbox, modify your code, move your snippet back to the toolbox and finally, rename it.

Instead of putting all the snippets under the General tab, you can create your very own tab to categorize your snippets more easily. To create a new tab, right-click anywhere on the toolbox and select the “Add tab” option and give it a name.

I am pretty sure that you did all the previous steps using your mouse. It is not a bad thing but you can achieve the same thing using your keyboard. First, highlight some code (SHIFT-Arrows) and copy it to the clipboard (CTRL-C). Then press CTRL-ALT-X to focus the toolbox. You can now select the tab you want (using the arrows) and finally press CTRL-V to paste your snippet. It may seems longer but believe me, it is a lot faster once you get use to the shortcuts.

You can also use the keyboard to paste a snippet. Place the cursor in your code editor exactly where you want the code to be pasted, then focus the toolbox (CTRL-ALT-X), select the required snippet (arrows) and finally press Enter.

Tip #3: Real snippets

Portion of this content is from Sara Ford tip #234.

There are also pre-built snippets ready to use in Visual Studio. This feature was first introduced in VS2005. These snippets are more flexible than the one provided in tip #3 where they can also contains placeholder that can be easily replaced when inserted.

For example, type “Property” (in VB or “prop” in C#) and press Tab-Tab. You might wonder why the Tab-Tab? The answer is quite simple. The first one is for the Intellisense to complete its job and the second one is to launch the snippet wizard. Once the wizard has started (see figure 5), you can see the placeholders that you normally replace with your real values. You will notice that every instances of the same placeholder will be replaced when you change one. For example, if you change the type from String to Integer in the property snippet, the setter will also have its type changed (not as obvious with the C# example but believe me, it is working).

Figure 5: The snippet wizard in action

The real problem with snippets is not to use them, it is to discover them! VB Intellisense is not really good at showing them (whereas C# is).

One way to navigate them is to press CTRL-K-X which starts the “Insert snippet” wizard (or ?-Tab in VB only). Then you can navigate through the list (and notice that the VB and the C# list greatly varies). Even that wizard is not providing much help if you want to discover them.

You can also open the Code Snippets Manager (CTRL-K-B or under the Tools menu) which will show you some help about each of them (much the same help Intellisense is showing you already). But that doesn’t show the snippet code! From the manager, copy the text under the Location as shown in figure 6.

Figure 6: Copy the location from the Code Snippets Manager

Now that you have the full path of the source of the snippets, can you correctly guess which method I will tell you to use that file? The Magic Box is the way to go! You can go review Tip #5 if you already forgot that one. So press CTRL-D (to focus the magic box) and type >File.OpenFile “XX” (pasting the content of your clipboard instead of the XX). The double quotes around the filename are to correctly open the file in case the full path contains a space. Now that you have the snippet source code open into the editor, you can modify it, save it and start using your newer customized version.

I would like to provide you 2 great links about snippets. The first one is a site that provides more snippets. This site is maintained by Michael Palermo offering free snippets. You can also contribute. You will find this site at http://gotcodesnippets.com/default.aspx.

The second link is a tool to replace the useless Code Snippet Manager. This new free tool written by Bill McCarthy offers in place editing of the snippets. can get this wonderful tool from http://www.codeplex.com/SnippetEditor.

Tip #2: Throw-away projects

Portion of this content is from Sara Ford tip #158.

I often create projects just to demonstrate something to a colleague or during presentation. Before discovering this tip, I was always sending those dummy projects to the c:\Temp folder but always forgot to destroy them after the test/demonstration was complete. So I was having projects named something like WindowsApplication34.

Visual Studio as an option that lets you create what they call a throw-away project. You will find this option under Tools->Options->Projects and Solutions. Uncheck the “Save new projects when created” option (as shown in figure 7).

Figure 7: The throw-away project option

The next time you will create a new project (that is not part of an existing solution), you will only be asked for its name (no path and/or source control question). This new project will behave like any other project meaning that you can add controls to forms, add classes to the project, run it, debug it ... Nothing differs so far. The only difference will be when you will try to close the application. It will ask you what you want to do with it. You have 3 options: Save, Discard, or Cancel. Selecting the save options will ask you for a folder where you want to save your new project. Discard will simply delete every files related to that dummy project.

Tip #1: Opening containing folder and tabs order

Portion of this content is from Sara Ford tips #125 and #331.

While doing our job, we often need to access the folder containing the project. Sure we can use the “Show all files” from the solution explorer but that’s not always suited for the task we have to do. Sure we can also open Windows Explorer and start drilling our huge hard disk to find the folder containing our project but there is an easier way to do it and that’s what I want to show you here.

If you right-click a folder in your solution, or right-click the project itself, you will find an option in the context menu named “Open folder in Windows Explorer”. Clicking this item will execute just what the title says. This is interesting when you quickly want to open the bin folder.

If you right-click a file, you won’t see that option (unless like me you have some 3rd party tools like MZ-Tools). Instead, you will find this option if you right-click the tab of that file opened in the editor (this time called “Open Containing Folder”). Notice that you will also find a “Copy Full Path” option if you need that feature.

Speaking of the tabs, you can also re-organize in a “most recently used” manner by changing a registry key.

To be honest, I am so used to the tabs remaining at their place when I click on them that I don’t like them to move around when I select them. But you might love that. This key, as shown in figure 8, is a DWord value named UseMRUDocOrdering saved under HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0.

Figure 8: Tabs ordered as MRU

Tip #0: Quickly increasing and decreasing font size

Portion of this content is from Sara Ford tip #242.

Last but not least, here is a tip that all presenters will love because every time we plug our laptop to a projector, we have to crank up to our font so that the people in the room can see what the code. But it is not only for presenters, I often have colleagues coming to my desk and standing by me and wanting to see something on my screen. So that tip is also good in those occasions.

Sure you can open the “Font and Colors” section from the options but we all know that this feature takes a long time to load because every font installed on your computer is loaded into memory.

Instead, you can use 2 macros that have been written by Sara Ford herself which are installed with Visual Studio. To use it, press ALT-F8 to open the Macro Explorer (or open it through Tools->Macros), open Samples and then Accessibility. In that folder, you will find 2 macros named DecreaseTextEditorFontSize and IncreaseTextEditorFontSize. You can right-click them and select Run from the Context menu and you will see your Font grows or shrink.

But I hear you saying that it is not shorter to open the Macro Explorer then to open to the Font of the Options. You are right but I haven’t told you everything yet!

Because it is a macro, you can assign a shortcut combination to those macros and only use those shortcuts when you need them. That’s nice. As shown in figure 9, you can open Tools->Options->Environment->Keyboard to assign your own shortcut to commands. Because we know that the macros contain “fontsize” in their name, you can type this keyword into the “Show commands containing” textbox to filter the long list of commands. When you select one of the commands, you see the already assigned shortcut under “Shortcuts for selected command”. If none is already assigned, give the focus to the “Press shortcut keys” textbox and do a combination you will remember. If that combination is already used, you will see the command under “Shortcut currently used by”. Otherwise, just click the Assign button and that shortcut will be yours forever.

Figure 9: assigning shortcuts to macros

I have seen some computers which were not offering the macros in the Macro Explorer. In fact, the real problem is that the macros are not loaded. Each time the problem was the same and was easy to fix. First, open Visual Studio. Then open Tools->Macros->Macro Explorer (or press ALT-F8). In there, the samples folder should appear empty. Right-click Macros and select “Load macro project”. Navigate to the “C:\Users\{your account name goes here}\Documents\Visual Studio 2008\Projects\VSMacros80\Samples” folder and select the Samples.vsmacros file. Finally, click the Add button. You should now be good.

Conclusion

Many small things can make you more efficient in your everyday work. I hope you learned a couple of new tricks here.

Here is a very last tip before leaving. You might know that F12 is the shortcut for “Go To Definition” (to quickly jump to the member or method declaration). But do you know the shortcut to come back where you where before? Try CTRL- (CTRL-Minus).

I hope you appreciated the topic and see you next month.


(Print this page)