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.

8 Responses to “Visual Studio Tip of the day – Format Document”

  1.   Tim
    July 28th, 2006 | 9:53 am       Reply

    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?

  2.   hewell
    October 19th, 2006 | 8:06 am       Reply

    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{
    //…
    }
    ?

  3. September 18th, 2009 | 7:33 am       Reply

    Make sure the syntax is OK before you use Ctrl K + D, otherwise it doesn’t work.

  4.   opcucwt
    October 6th, 2009 | 6:21 pm       Reply

    pjW6ew toojqbgauvvr, [url=http://tdwgfmtonvjc.com/]tdwgfmtonvjc[/url], [link=http://cxbknalvhirn.com/]cxbknalvhirn[/link], http://tddxragdpbzt.com/

  5.   Rachel
    October 29th, 2009 | 11:06 am       Reply

    I would like to create rules to format, do you have any idea how can I do that?

  6. November 19th, 2009 | 12:38 am       Reply

    Thanks for sharing this powerful information.That’s very helpful and interesting command.

  7.   Michael Courcy
    January 14th, 2010 | 3:11 pm       Reply

    Go to edit -> advanced -> Format the document

    You’ll be able to read the shortcut.

  8.   Tommy
    August 6th, 2010 | 4:18 pm       Reply

    Yes, you can change the options of how this does the formatting.
    Go to Tools -> Options -> Text Editor -> C# (or VB) -> Formatting -> New Lines

Leave a Reply