Essential Visual Studio Tips & Tricks that Every Developer Should Know

In this blog entry, I list the essential tips and tricks that every developer who uses Visual Studio 2008 should know. I wanted to keep this list brief. I also wanted to focus on only those tips and tricks that I use on a daily basis. Almost all of these tips and tricks are just as useful regardless of whether you are building an ASP.NET Web Forms or ASP.NET MVC application.

Tip #1 – You don’t need to select a line to copy or delete it

I always cringe whenever I see someone select an entire line of code in the Visual Studio code editor before copying the line or deleting the line (see Figure 1). You don’t need to do this.

Figure 1

clip_image002

If you want to copy a line of code then you can simply press CTRL-c to copy the line and press CTRL-v to paste the line. If you want to delete a line, don’t select it first, just press CTRL-x. You’ll be surprised how much time this one tip will save you.

Tip #2 – You can add a namespace automatically by pressing CTRL-.

In the old days, before Visual Studio 2008, if you used a class in your code that was not a member of any of the existing namespaces imported in your code then you had no choice but to look up the class in the documentation and enter a using statement to import the new namespace.

Visual Studio 2008 is smart enough to import namespaces for you automatically. If you type the name of a class that inhabits a namespace that has not been imported then Visual Studio displays a red bar beneath the class in the editor (see Figure 2). You can press CTRL-. to display a dialog box for picking the right namespace to import. Finally, just press the ENTER key to select a namespace (see Figure 3).

Figure 2

clip_image004

Figure 3

clip_image006

Tip #3 – Never create properties by hand

Never type property declarations by hand. It takes forever! Instead, just type prop + TAB + TAB. When you type prop + TAB + TAB, you get a code snippet (template) for entering a property. Use TAB to move between the template parameters. Press the ENTER key when you are finished creating the property (see Figure 4). This tip has saved me from many days of tedious property typing.

Figure 4

clip_image008

Tip #4 – You can remove and sort unnecessary using statements

Whenever I finish creating a class, I always clean up the list of using statements that appear at the top of the class file. I like to remove any unused using statements to reduce the amount of visual clutter in my classes. You can remove using statements that are not required by your code by right-clicking the top of your code file, selecting the menu option Organize Usings, and select Remove and Sort.

Figure 5

clip_image010

Tip #5 – Use CTRL-k+c to comment out code

If you need to temporarily disable a block of code, or a section of an ASP.NET page, then you can comment out the region by pressing CTRL-k+c. I always do this when I want to rewrite an existing section of code, but I am afraid to delete the old code before writing the new code.

For example, Figure 6 illustrates commented out code in the code editor.

Figure 6

clip_image012

You can use the very same key combination to comment out code almost anywhere. For example, you can comment out code in ASP.NET pages, web.config files, and JavaScript files (see Figure 7).

Figure 7

clip_image014

You can perform the opposite operation, and uncomment out code, by using the keyboard combination CTRL-k+u.

Tip #6 – You can close all documents except the current one

After working in Visual Studio for an extended period of time, I end up with a lot of open documents. I like to quickly switch between open documents by hitting the keyboard combination CTRL-TAB (you can also use CTRL-TAB to shift focus to different tool windows). If you have too many documents open, using CTRL-TAB becomes more difficult because you must hunt through the set of open documents.

There are two ways that you can close open documents. You can use the menu option Window, Close All Documents. Better yet, you can right click a tab that corresponds to an open document and select the menu option Close All But This. Selecting this latter option closes all open documents except the document corresponding to the tab (see Figure 8).

Figure 8

clip_image016

Tip #7 – You can open a database by double-clicking the database file in App_Data

After you add a user instance of a SQL Express database to a project (a RANU database), you can quickly connect to the database by double-clicking the .mdf file in the App_Data folder. Double-clicking the database opens the Server Explorer/Database Explorer window and expands the objects in the database automatically.

Tip #8 – You can copy a file or folder into a project by dragging and dropping

I’m always composing new Visual Studio projects from bits and pieces of previous Visual Studio projects. For example, I might need to add a folder from a previous project or a set of files. You can add existing files to Visual Studio by right-clicking in the Solution Explorer window and selecting the menu option Add, Existing Item. But, this method of adding files is slow. Furthermore, you can’t add folders using this method.

The best method of adding files and folders to Visual Studio project is to just drag the files or folder into the Solution Explorer window (or copy and paste the files or folder). For example, I am constantly adding my MoviesDB.mdf movies SQL Express database to new projects. I keep this file on my desktop and drag it into the App_Data folder whenever I need the database in a new Visual Studio project.

Tip #9 – Use CTRL-SPACE to perform statement completion

This tip is particularly relevant for developers who build applications by using test-driven development. When doing test-driven development, you write a unit test first and then write code that satisfies the unit test. When writing the unit test, you often have to fight with statement auto-completion.

There is an easy solution to this problem. Disable automatic statement completion by selecting Tools, Options, Text Editor, All Languages and uncheck the Auto list members checkbox (see Figure 9).

Figure 9

clip_image018

After you disable auto list members, you can still display suggestions for statement completion by using the keyboard combination CTRL-SPACE.

Tip #10 – Add new items by pressing CTRL-N or CTRL-SHIFT+A

In general, using the mouse to perform an action in Visual Studio is slower than entering a keyboard combination. The fastest way to add a new item into a Visual Studio project is to hit the keyboard combination CTRL-n or the keyboard combination CTRL-SHIFT+A. The first keyboard combination works in ASP.NET Websites and the second keyboard combination works in both Website and ASP.NET MVC Web Application projects.

This keyboard combination opens the Add New Item dialog box (see Figure 10).

Figure 10

clip_image020

You can use the TAB and arrow keys to select an item from the dialog box and navigate to the Open button. Press the ENTER key to invoke the Open button.

Tip #11 – You don’t need to type file extensions when adding a file

This tip is related to the previous one. After using the TAB key to navigate to the Name textbox in the Add New Item dialog box, you can enter the name of the new item. When typing the name of a new item, you don’t need to include the file extension. Visual Studio can determine the file extension from the selected template.

For example, when adding a new Web Form named MyPage.aspx, you can simply type MyPage. When adding a new Master Page named Site.master, you can simply type Site.

You might think that avoiding typing a couple of characters would not matter. But, if you add dozens of files to a Visual Studio project, the number of characters that you can avoid typing quickly adds up.

Summary

If you have a tip or trick for Visual Studio 2008, please share it in the comments. However, nothing obscure please. I want to focus on only those tips and tricks that matter on a daily basis.

65 Comments

  • great tips. we keep doing what has worked for us all along, and often aren't aware of new or quicker ways to do repetitive actions. thanks.

  • Great tips!

    Did not know the one about copy and paste!

  • Nice Tips , Thanks :)

  • Valuable tips! Thanks!

  • ctrl-x, if I'm not mistaken, should be a cut. If you want to delete the whole line, I believe shift-delete will do it, and that way you aren't overwriting your clipboard. Maybe I'm wrong, but I don't think so.

  • I totally dig the "prob + tab + tab" one, that's going to be a huge time saver!

  • If you install Resharper, you get a ton of good stuff like this.

    (disclaimer: I do not work for JetBrains)

  • CTRL-X with nothing selected does cut the current line into the clipboard. If you just want to delete the current line (if, for example, you have something in the clipboard you want to keep), you can use CTRL-SHIFT-L.

  • One addendum - Ctrl-"." is for more than just adding usings - it works for any SmartTag. Great for Generate Method Stub, Implement Interface, etc.

  • Great Tips, specially first 5 was very new to me and good time saver.

  • Stephen,
    Do you know how to let the templates to be listed in the order of the template name?

  • Great tips, and huge time savers, Stephen, I just have a quick query related to Tip#3, we can do the same by right click of mouse on code window and select 'Insert Snippet..' and 'Surround With..' both look to me somewhat similar, so what is the differences why this is created as two different Menus ?

  • Hi Stephen.

    Just a question regarding Tip #8. I'm wondering how to enable the drag/drop feature because my VS2008 (Team Suite) won't let me do that. I try to drag/drop files into my apps and it's like a disabled feature, it even shows the blocked mouse cursor (like the no parking sign).

    Can you help me get to it?

    Thanks.
    Jose Guay

  • Tip# :
    Hit Ctrl-k + d to Re-format the code .
    Tip# : When working with aspx code , sometimes the autocomplete list doesn't appears , to solve this ,
    press Backspace then press Space , this will make it apears again.


    Thanks

  • Tip #2 (CTR-.) doesn't work for me (maybe it does't work on laptops, huh). I use ALT-SHIFT-F10 instead.

  • If you want to encapsulate instance variable into a property you just set the marker on it and press Ctrl + R and hit OK on the dialog.

  • You can also use ctrl+L to cut a line of text. For some reason, I use that one rather than ctrl + x :)

  • Hi,

    This is really a nice and useful tips.

    Thanks

  • When having multiple pages open i found it easy if you re assign ctrl + w, to the action File.Close
    this way it works the same as when you want to close a tab in firefox or IE

    the only problem is that you have to remove another keyboard shortcut which is already assigned to ctrl+w but I never used this.. so now if I want to close a couple of tabs you just hit ctrl+w until you feel good :)

  • Thanks for the useful tips. Is there any key comibination to Increase/Decrease Indent. This is one feature I have searched for a long time and can come in very handy if available. Anybody ?

  • Thanks a lot. Personally I rewrite the hot key in tips#5 to Ctrl+/,/ to comment section and Ctrl+Shift+/,/ to uncomment it. It feels a lot easier.

  • Is this for VS 2008 ? or 2005?

  • The tips are quite basic really, but nice post anyway.

    @Mic
    Shift + Delete will actually cut the line, not just delete it :)

    @Kashif
    Select the text (one or more lines) and press Tab to increase the indenting, and Shift + Tab to decrease it. Of course, if it's just one line you don't need to select it to increase the indenting.

  • Nice But almost all are same as of VS2005 except one or two [like remove redundant using statements]

  • You can easily create accessors using refactoring:

    When your mouse cursor blinks over the property name just press CTRL + R, E and then another quick enter...

  • I always find it annoying that cntrl-c without anything selected copies the current line, so I switched that off in the settings. (if you did, the first tip doesn't work for you, but that's what you want ;). Shift-del is indeed a welcome helper, though I didn't know about cntrl-shift-L, as I sometimes want to avoid the clipboard destroying of shift-del.

  • The first few i have never seen before - thanks!

  • good article, thx :)

  • Really a nice information. thanks for sharing with us

  • Some others I find useful:

    Ctrl+K+F to format selection (Ctrl+K+D formats entire file)
    Ctrl+Shift+V (repeatedly) to cycle through clipboard "ring"
    Ctrl+Shift+8 to return to last place F12 was pressed

  • Tip #2 rather confused me until I realised that the "." wasn't the end of sentence marker...

    CTRL-[period] would be a bit clearer

  • And the most useful fast comment/uncomment ever:
    uncommented:
    /**/
    ... code block ...
    /**/

    commented:
    /** /
    ... code block ...
    /**/

    works almost in any programming language

  • Excellent a lot helpful tips are available.
    thanks for sharing.

  • If you want to go to closing curly brace[}] then press crtl + } , which will take directly to the end of the namespace or method.

    namespace TestNS
    {
    class test
    {
    }
    }

  • Nice One. Thanks for putting it together.
    Most of them works for VS 2005 as well - exception is 'Organize Using'.

  • Nice, an easy to follow short list with some very helpful little tips. Thanks!

  • That is Really awesome

  • Nice couple of tricks, I try to avoid using the mouse when programming, that's why I knew most of them. (the prop one seems really useful and I didn't knew it).

    Besides those, I find the following really useful:

    Ctrl+Alt+L Takes the focus to the solution explorer, really useful for opening files that aren't already opened.

    F12/Shift+F12 Go to declaration/Find usages... (couldn't live without them) :P

    Ctrl+- To move to the previous position (really useful when navigating through the code with F12/Shift+F12). The "-" must be the one by the 0, not the one in the numeric keyboard.

    Ctrl+M+M Expand/collapse definitions (for Html tags, methods, classes, regions).

    Ctrl+F4 To close the current tab (I prefer this to configure the Ctrl+w as Leon sugested, although sometimes I get confused bc Notepad++ uses Ctrl+w to close tabs).

    Definitely the shortcuts are great, and save a LOT of time, and I really prefer not to configure custom ones to avoid confusion when working @home or at the office.

  • As others have pointed out... Use CTRL+L for cutting current line. The real beautiful part of it is that you dont have to preselect the entire line!

    Can also be used to delete entire lines, for ex empty lines.

  • Excellent post. Especially tip#1, as moving lines is something I do quite often. I'll try to remember not to select it, from now on. :)

    The prop tips was also a good one. I remember reading about how to write your own code snippets, but I never got around to actually check it out. Now you reminded me, as well as giving a tip about the already existing prop snippet. Thanks.

    For those of you discussing Shift+Delete, it *will* overwrite your clipboard.

    Shift+Delete is the same as Ctrl+x (cut)
    Shift+Insert is the same as Ctrl+v (paste)

    It's just a matter of preference, really.

  • To add a namespace for the class press Ctrl + >.

  • By pressing Ctrl+Tab and then holding the Ctrl key we can borwse all the Active Tool Windows and Active Files.

  • Tip #9: You should use Ctrl-J instead of Ctrl-Space... J recalculates the autocomplete, plus I kept running into issues with space actually inserting space in some places. Ctrl-J will give you intellisense even if you've typed after the . which is nice.

  • Get coding window in full screen: Alt+shift+enter

    Great for when you're just coding away, no need for solution explorer, error window and whatnot.

  • I use Ctrl+M, O a lot - it collapses all methods so that I can get a high level view of the class.

  • Nice post.
    Really useful
    Thankx

  • Apparently there are several ways to make auto-completion appear. In addition to Ctrl+Space and Ctrl+J, you can use Alt+Right Arrow. FYI Ctrl+Space is sometimes used as a shortcut key to switch input languages, as is the case on my system.

  • Hold Alt+Shift and select any portion of your code using mouse. That is really helpful sometimes...

  • I agree, I'm pained watching anyone code without Resharper. This whole post pains me thinking about life before it. I don't work there, just trying to spread the love.

  • My keyboard and my hands are thankful of this awesome post!

  • Hi Stephen,

    My team and I have really enjoyed reading this and thought this is very useful for every developer. I'd like to ask for your permission to republish your article in our publications. We're a online tech publishing community aim to serve the information needs of software developers, architects and designers.

    Please shoot me an email at weiling@dzone.com

    Many Thanks! Also, planning to attend PDC 2008? Check us out at booth 1201!

  • very useful tips. thx for sharing

  • Hi, does some body knows the visual studio tip that moves a selected block of code from up to down or down to up. It's very easy to do with the mouse. You select the block of code and keep the right mouse button clicked over the block of code selected and then drag the text to where you want.

  • When intellisense is in the way of some code you want to see, hold Ctrl to make it transparent.

    When lines get very long, swith to full screen with Alt+Shift+Enter. The same for switching back.

  • As tip #2 and #4 don't seem to work in VB, does anyone know if there are other ways to get this functionality in VB?

  • You can insert any snippet using key combination CTL - K, X.
    It will open snipped insertion window. Select the snippet and hit insert. You can also get some very good and handy code snippets from MSDN
    msdn.microsoft.com/en-us/vs2005/aa718338.aspx

    You can also create your own snippets using Open source snippet designer

    blogs.msdn.com/matt/archive/2008/09/21/the-snippet-designer-is-released.aspx

    Thanks and Regards,
    Mahin Gupta

  • I AM A BEGNER LEARNER ABOUT ASP.NET AND I WANT TO LEARN BASIC OF ASP.NET

  • My fav is highlight some text then ctrl + f3 to search down and ctrl + shift + f3 to search up. no need for search dialogue

  • What worked 4 "prop" also works 4 "try".

    try "try + TAB + TAB" and u'll get a try-catch block.

  • Keith's trick is nice ...

    Here's what i first didn't notice.
    Once defined the keyword with "CTRL+F3" / "CTRL+SHIFT+F3" u simply can use "F3" / "SHIFT+F3" to search down/up.

  • DPack is excellent free plug-in (use keys to navigate/open any file and to navigate to any method in current file among others). http://www.usysware.com/DPack/Default.aspx

  • I have all of the checkboxes for statement completion unchecked for both All Languages and for C#. Why do I still get a statement completion dropdown after I type the override keyword and start typing the next word? I really can't stand statemnent completion, please help me turn off this feature.

  • Tip #8 (drag and drop) doesn't work for me if I'm running Visual Studio as Administrator on Vista. However, it works fine if I start Visual Studio normally. Is this a bug or a "security feature" since Explorer is not running as Administrator?

  • Regarding Tip #1, just found an enhancement - if you want to copy a procedure, minimize it with the outline plus sign and then press Ctrl+C

  • I read your book Asp.Net unleashed and was very impressed with the book.

    I am sure these new tricks will help me in becoming a more efficient coder.

    Thanks!

Comments have been disabled for this content.