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.

Published Tue, Jan 24 2006 14:35 by Vipul Patel
Filed under: ,

Comments

# re: Visual Studio Tip of the day - Format Document

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?

Friday, July 28, 2006 9:53 AM by Tim

# re: Visual Studio Tip of the day - Format Document

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{

 //...

}

?

Thursday, October 19, 2006 8:06 AM by hewell

Leave a Comment

(required) 
(required) 
(optional)
(required)