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

image

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

image

Figure 3

image

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

image

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

image

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

image

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

image

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

image

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

image

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

image

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.

Discussion

  1. avrashow says:

    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.

  2. Great tips!

    Did not know the one about copy and paste!

  3. Elam says:

    Nice Tips , Thanks :)

  4. http:// says:

    Valuable tips! Thanks!

  5. http:// says:

    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.

  6. Stephen says:

    I totally dig the “prob + tab + tab” one, that’s going to be a huge time saver!

  7. http:// says:

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

    (disclaimer: I do not work for JetBrains)

  8. Matt says:

    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.

  9. Kevin Dente says:

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

  10. Brij Mohan says:

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

  11. http:// says:

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

  12. Brij Mohan says:

    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 ?

  13. 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

  14. Uwe says:

    You forgot the one and only important tip: Use ReSharper http://www.jetbrains.com/resharper

  15. http:// says:

    VALUABLE TIPES

  16. anas says:

    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

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

  18. http:// says:

    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.

  19. Hi There,

    Its a very nice useful tip.Thanks

  20. HeartattacK says:

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

  21. Hi,

    This is really a nice and useful tips.

    Thanks

  22. http:// says:

    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 :)

  23. http:// says:

    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 ?

  24. http:// says:

    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.

  25. Is this for VS 2008 ? or 2005?

  26. My most used shourtcut (working with ugly formatted HTML pages and XML) is CTRL+K CTRL+D

    This shortcut formats the current document.

    Very nice.

  27. 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.

  28. 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.

  29. http:// says:

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

  30. http:// says:

    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…

  31. FransBouma says:

    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.

  32. Guy Harwood says:

    The first few i have never seen before – thanks!

  33. asp.net says:

    good article, thx :)

  34. http:// says:

    Really a nice information. thanks for sharing with us

  35. http:// says:

    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

  36. http:// says:

    Tip #2 rather confused me until I realised that the “.” wasn’t the end of sentence marker…

    CTRL-[period] would be a bit clearer

  37. http:// says:

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

    commented:
    /** /
    … code block …
    /**/

    works almost in any programming language

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

  39. http:// says:

    I personally like the “CTRL-K-K” to add bookmark and then “CTRL-K-N” to go to the next bookmark. It is really usefull when you need to navigate through many c#/aspx pages.

    Also, in C# when you want to rename a variable, use the refactor shortcut key (F2). It is very usefull when you need to update a variable in a function.

  40. http:// says:

    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
    {
    }
    }

  41. PHenry says:

    I didn’t know about the cutting of the line (I always used CTRL+L). Here are some other tips’n’tricks.
    http://www.pchenry.com/…/Default.aspx

  42. http:// says:

    Nice One. Thanks for putting it together.
    Most of them works for VS 2005 as well – exception is ‘Organize Using’.

  43. http:// says:

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

  44. Lee says:

    It’s worth mentioning that prop + tab + tab also works for for, if, foreach etc. I find the remove and sort using statements a life saver. Also check out http://code.msdn.microsoft.com/PowerCommands it allows you to remove and sort usings across a whole project.

  45. http:// says:

    That is Really awesome

  46. Will says:

    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.

  47. http:// says:

    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.

  48. Kjetil Myhre says:

    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.

  49. Shakeel Ali says:

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

  50. Shakeel Ali says:

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

  51. Bill Forney says:

    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.

  52. http:// says:

    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.

  53. Lasr says:

    Another great tip is to use the new Xml Visualizer v.2. for visual Studio: http://www.codeplex.com/XmlVisualizer

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

  55. http:// says:

    Nice post.
    Really useful
    Thankx

  56. http:// says:

    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.

  57. http:// says:

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

  58. http:// says:

    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.

  59. http:// says:

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

  60. 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!

  61. http:// says:

    Really nice Post, a shortcut suggestion really usefull: CTRL+”-” to navigate backward and CTRL+SHFT+”-” to navigate forward

  62. http:// says:

    very useful tips. thx for sharing

  63. http:// says:

    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.

  64. deap says:

    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.

  65. Marcel says:

    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?

  66. http:// says:

    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

  67. http:// says:

    so cool!

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

  69. Keith says:

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

  70. Bjørn says:

    What worked 4 “prop” also works 4 “try”.

    try “try + TAB + TAB” and u’ll get a try-catch block.

  71. Bjørn says:

    What worked 4 “prop” also works 4 “try”.

    try “try + TAB + TAB” and u’ll get a try-catch block.

  72. Bjørn says:

    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.

  73. Bjørn says:

    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.

  74. hulu says:

    点击上一行名字的链接就可以查看VisualStudio 你不得不知道的操作秘笈中文增补版。地址是:hi.baidu.com/…/ddf018f4acc0ae69dcc47463.html

  75. http:// says:

    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

  76. http:// says:

    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.

  77. http:// says:

    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?

  78. http:// says:

    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

  79. Love the tip on cleaning up the using statements, that one is going to be my new best friend :D

  80. Jesenko says:

    Very nice posting Stephen, thank you! Within our internal tutorial Wiki I will definitely add a link to your page.

    You might find “Visual C# 2008 Keybinding Reference Poster” useful. Google for its name or get it here if the link remains valid.

    http://www.microsoft.com/…/details.aspx

    Additionally, CTRL + } mentioned above takes you back and forth through the region opening and closing line.

    Shakeel Ali
    I am not sure I followed Ctrl + >

  81. Jooma says:

    Thank for the tips. Anyone know to outline a section of code. I tried to follow this msdn.microsoft.com/en-us/library/td6a5x4s.aspx but it never works. I want to outline my IF block and some other sections too.

  82. deo says:

    thanks, really good.

  83. Masoom Ali says:

    Rename the existing class,method or property through (ctrl + R,R)

  84. I discovered how easy it was to dock and undock (pin and unpin) the tool windows in VS 2005 (and beyond?). I have posted it as a comment to a codeproject article here: http://www.codeproject.com/…/vstogglewindow.aspx

  85. Great Tips! These tips will help me a lot. Thanks for sharing.

  86. Thanks for providing very useful tips.

  87. magine that you make the following browser request against an ASP.NET MVC web application

  88. Thank you for sharing the this code.

  89. free games says:

    very useful tips. thx for sharing

  90. NikeAir says:

    Nice article, very helpful. Thanks!—
    Nike ,Jodon ,kappa shoes are selling on the way..Come on
    …………….Nike shoes ! Addidas ! crazy buying ———————– Nike Shoes || air yeezy

  91. 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.