Visual Studio (2005,2008,2010) Tips and Tricks.
#18 Format Document, and Format Selection
Thursday, January 22 2009
Visual studio wants you to be happy :) If you like breaks before braces, and after; if you like self closing <br /> tags, if you don’t want self closing <br /> tags. Whatever your fancy, you can automatically have Visual Studio apply with a very quick keyboard combination. This is extremely helpful when you are looking at source code created by someone else. Maybe on your current team, or just an old project.
Snippets / Notes:
[CTRL]+[K],[D] = Format Document
[CTRL]+[K],[F] = Format Selection
Visual Studio Tip / Trick #018 English Transcription:
Today’s Visual Studio trick of the day trick number 18 was originally posted by Sara Ford August 2007.
The trick is Format Document and Format Selection.
So we are looking at C# file and everybody has their preference of whether or not they like the open brackets on end or a new line for C#. Or may be inside of ASP.net file you like your break tags to start on a new line and also be indented. Well the keyboard shortcut [CTRL]+[K],[D] will format your document and the keyboard shortcut of [CTRL]+[K],[F] will format what you have selected.
I would like to take the blog post just a little bit further if you go to tools then options you can drill down to text editor. The language of choice; in this case we are looking at an HTML file and then you can go into the format and select a specific tag option.
Now inside here we have literally every tag known and if the tag you are looking for is not known you can add it.
So here we select the BR tag; and we'll say that it’s a self closing tag, line breaks before and after the tag, you can change that, let’s change that to just after tag. And then we'll say ok.
So thats the formatting rules for our break tag. So if we just put all these back on one line and then [CTRL]+[K],[D] to format the document; You can see that we have got line breaks on appropriate places.
Back on the C# side, we go back to tools, options, text editors, C#, then formatting. And particularly looking at new lines you can see you have the ability to control where those new lines get placed and there is a nice little demo of what the code will look like.
So depending on how you have these rules is what that format document will use for you so let’s say we want open braces on new lines and now if we move this back up to the top by pressing [DELETE], then [CTRL]+[K],[D] will move that down to a new line for us. So the formatting of documents is very very flexible. All the way down to a per tag basis inside of HTML and C# and Visual Basic and all the programming languages you have ability to do class level, method level, variable level; I mean it extremely flexible.
0 comment(s)