How can I reformat code from 80 characters per line to 120 characters per line

I have C# code formatted at 80 characters per line that is impossible to read, due to the nature of .net file structure, a lot of the characters in a line end up as whitespace, and a function call can wrap into 4 to 6 lines. Making 100 lines of code into 400.

I would like to convert this to 120 characters per line.

What do I use to do this? Can Prettier with VSCode do it?
Can CLang formatter do it?
Is there any built in formatting into Visual Studio 2019?

Provide some step by step directions on how to do his please

This process should be entirely automated, I should not have to manually delete any newlines or change spacing

Please advise how to do this the best way.

Thank you

  • Whether you can achieve this automatically depends entirely on the content… Can you reliably determine which newlines should be for new lines and which were used for word wrapping? This might be possible if -say- every line starts with a timestanp and timestamps don’t appear elsewhere in logs.

    – 

1: Open Your Project in Visual Studio: Launch Visual Studio and open the project or solution containing the C# code you want to format.

2: Select the Code to Reformat: You can reformat the entire document or a specific code file. Open the file in the code editor, or right-click the project in the Solution Explorer and choose “Format Document” to format the entire document.

3: Set the Line Length: Go to “Tools” > “Options” > “Text Editor” > “C#” > “Code Style” > “Formatting” > “General.” Here, you can set the “Wrapping (Maximum line length)” option to 120 characters.

4: Format the Code: Once you’ve set the desired line length, you can format the code by pressing Ctrl + K, Ctrl + D. Alternatively, right-click in the code editor and choose “Format Document.”

Keep in mind that code formatting can change the structure of your code, such as line breaks, indentation, and spacing, so it’s important to review and test your code after formatting to ensure it still functions correctly.

If you’re using a different code editor or IDE, it may also offer code formatting options. The process may vary slightly, but the general idea is to set the line length limit and then use the formatting command or shortcut to reformat the code.

Leave a Comment