Visual Studio Tip of the day – Format Document
Time and again we write code and our brackets get out of visual sync, i.e. they no longer appear as a coherent set even though they may be.
In Visual Studio, there is a feature known as Format Document which will align the code systematically.
It can be invoked by the key combination of Ctrl K + Ctrl D
Suppose you code looks like
namespace LogFileCheck
{
class Program
{
static void Main(string[] args)
{
TextReader sr = new StreamReader(“mb20051116_05000600_BAYTRARPT03_k.msn.com_w3svc10000.log”, Encoding.UTF8);
TextWriter writesr =
new StreamWriter(“mb20051116_05000600_BAYTRARPT03_k.msn.com_w3svc10000_csResult.log”,
false, Encoding.UTF8);
while (sr.Peek()
!= -1)
{
string line = sr.ReadLine();
if (Regex.IsMatch(line, “&di=78”) && Regex.IsMatch(line, @”([^,]*,){19}66″))
writesr.WriteLine(line);}
sr.Close();
writesr.Close();
}
}
}
Press the magic keys Ctrl K + Ctrl D and voila, all your code looks pretty organized as under:
namespace LogFileCheck
{
class Program
{
static void Main(string[] args)
{
TextReader sr = new StreamReader(“mb20051116_05000600_BAYTRARPT03_k.msn.com_w3svc10000.log”, Encoding.UTF8);
TextWriter writesr = new StreamWriter(“mb20051116_05000600_BAYTRARPT03_k.msn.com_w3svc10000_csResult.log”, false, Encoding.UTF8);
while (sr.Peek() != -1)
{
string line = sr.ReadLine();
if (Regex.IsMatch(line, “&di=78”) && Regex.IsMatch(line, @”([^,]*,){19}66″))
writesr.WriteLine(line);
}
sr.Close();
writesr.Close();
}
}
}
Want to format only a small selected section of the dirty code? Select the area you want to format and press Ctrl K + Ctrl F.
I love this little command, but for some reason it stopped working in my IDE. I type ctrl K ctrl D and nothing happens… I recently imported the visual studio settings from idesign.net, probably screwed something up. Any idea what setting would affect this?
Is it possible to change the style of this command?
say in the example above, it looks like
class name
{
//blah
}
can i change this to
class name{
//…
}
?
Make sure the syntax is OK before you use Ctrl K + D, otherwise it doesn’t work.
pjW6ew toojqbgauvvr, [url=http://tdwgfmtonvjc.com/]tdwgfmtonvjc[/url], [link=http://cxbknalvhirn.com/]cxbknalvhirn[/link], http://tddxragdpbzt.com/
I would like to create rules to format, do you have any idea how can I do that?
Thanks for sharing this powerful information.That’s very helpful and interesting command.
Go to edit -> advanced -> Format the document
You’ll be able to read the shortcut.
Yes, you can change the options of how this does the formatting.
Go to Tools -> Options -> Text Editor -> C# (or VB) -> Formatting -> New Lines