.NN #6: Odds & Ins - Productivity

I’m glad I called this blog series semi-regular. :)  I’ve not had a chance to finish up my latest installment so I thought I’d throw a post out that just had some odds and ins in it about productivity.

As developers we spend a LOT of time typing, writing code and working in applications.  We can increase our efficiency quite a bit by becoming very familiar with the tools we use by learning shortcuts, new features and maybe even generating some macros.  So what follows is a few odds and ins I’ve picked up to help increase my productivity with Visual Studio.

Keyboard Shortcuts

Keyboard shortcuts are a great way of speeding up your development.  I know some people that hardly use the mouse when in Visual Studio, and, on the opposite end of the spectrum, others that use the mouse to do everything but type the code.  I used to only really use the most common keyboard shortcuts (Ctl-S to Save for example) when coding until I found out that I was much more productive by picking up a lot more of them.  Just do a Google search on “Visual Studio keyboard shortcuts” and you’ll get a large list of sites showing various key combinations.  I even had a previous post about Microsoft having downloads of the default short cut keys. 

Here are my favorites (I use the VS.Net default keyboard layout):

  • Alt-Shift-F10 : This opens a displayed smart tag up.  There are numerous times when a smart tag is displayed by VS.Net when coding but the most prevalent one for me is when I add a Type reference that isn’t included in my using namespaces.  When you’re coding and type in a Type that is referenced by the project I’m in, but it’s namespace is not in the using statements above VS.Net will show you a smart tag under the Type.  You use Alt-Shift-F10 to display the options in the smart tag and then choose to add the using statement at the top of the file or add the fully qualified namespace on the Type inline. 
    • Open up a new console application project.  Type “FileInfo” in the Main method and you’ll see a little red line appear under the word FileInfo.  Use Alt-Shift-F10 and select the first choice “using System.IO”.  This will automatically add the using statement to the top of the file and you can keep coding.
    • Other uses include to type a method call in that you haven’t coded the method for yet and you can use the smart tag shortcut to stub the method signature out for you.  Pretty cool!  This works even if you type a method on an object that doesn’t exist yet.  For example, if you have a Customer object and in your business logic code you type myCustomer.SendAlert(“email”); you can use the smart tag to stub a method named SendAlert on the Customer object with a string parameter. F12 : Navigate to definition.  If you put the caret on an object, method reference or variable and use F12 the focus will shift to pull up the location where that object is defined.  So, if the caret was on a variable name it would move focus to where the variable was defined, but if the caret was on a method it would jump to that method.  Note that if the item isn’t defined in the current project/solution then VS.Net will extrapolate metadata from the containing type and show you that generated file instead (note that the tab the code is displayed in will say MetaData and not be editable).
  • Ctl-F5 : Starts the solution/project without attaching the debugger.  I do this a lot actually.  You can always attach the debugger to the process later if you need to debug.
  • Ctl--E : Show the Errors List.  You can then use F8 and Shift F8 to move back and forth through the error list and bring up the corresponding locations in code. 
  • Ctl-Alt-O: Displays the output window.  This is very handy after a build, source control event, or execution of unit tests (if you are using TestDriven.Net that writes the results to the output window).

I highly recommend finding operations or commands you do on a regular basis and turn them into shortcuts if they aren’t already.  I’ve mapped TestDriven.Net’s testing commands to Ctl-T for Test and Ctl-R-T for rerun the previous tests.

Note that some keyboard layout options change the short cut keys.  You can always find out what something is by going to Tools > Options dialog.  Select Keyboard under the Environment node and you can see what is mapped where, change mappings, and even come up with your own layout to fit your needs.  You can search for commands to see if they are tied to a short cut key, or you can use a shortcut and see what is tied to it.  Note that a large percentage of menu options and context menu options in VS.Net are actually commands you can link to with shortcut keys.  If you spend a lot of time coming up with your perfect set of combinations that differ from the defaults I suggest exporting your VS.Net settings so you only go that headache once.

Macros

Check out creating Macros for Visual Studio for multi-step operations you perform often.  I have macros created to automatically bump my text font size up and down for presentations and build related macros.  There are numerous macros out there that people have created to help build connection string, manipulate the windows in the IDE and more.  Take a few minutes and think about anything you do often in VS.Net that is multi-step and see if you can turn it into a Macro.

External Tools

You can add external tools to the VS.Net Tools menu pretty easily.  Just click Tools > External Tools… to bring up the External Tools dialog box.  If you have an executable you use often you can place it in here and then tie a shortcut key to it. 

The following short cut I picked up from Mike Levy and it can be used to open a VS.Net Command window up set to the directory of the currently highlighted file in the Solution Explorer.  To get this to work do the following in VS.Net:

  • Open the External Tool Dialog : Tools Menu > External Tools…
  • Click the Add button
  • Fill in the following fields (note the double quotes in the arguments entry):
    • Title: VS Command Window
    • Command: cmd.exe
    • Arguments: /k ““C:\Program Files\Microsoft Visual Studio 8\VC\vcvarsall.bat”” x86
    • Initial Directory: $(ItemDir) Click Apply
  • Map to any keyboard short cut key you want.  Note that what you map it to is Tools.ExternalCommandX where X is index it appears in under the External Tools dialog.   I use the key combination Ctl-` 

To use this the Solution Explorer has to have the focus.  Select a file and then hit your short cut key.  A VS.Net command window should open up to the directory the file is in.  Very handy!

3rd Party Tools

There are numerous third party tools to help productivity while using VS.Net.  I personally own CodeRush/Refactor! from DevExpress, but I’ve seen ReSharper in action as well.  I think both tools are excellent and recommend that if you don’t use a productivity tool yet, you should try the free trials of these applications.  I’ve only had CodeRush/Refactor! for about two or three months now but I can’t believe how much extra functionality it provides.  The refactorings in this tool (and in ReSharper) make the refactorings in the base VS.Net product look positively weak.  I’m using refactorings and code templates from CodeRush/Refactor! ALL the time as I code.

That’s it for this installment of .Net Nugget.  As usual, please provide feedback on if you like what you’re reading, if you want to add your own tips, or if what I put up is just plain wrong.