Cookies This site keeps only what it needs to work. There is no analytics and no advertising on it today, and nothing here follows you to other sites. Read the privacy policy

Choose what to allow
+92 312 8299940Free Quote
EN – Language: English
Free tool, nothing to install

Code Beautifier
Any File, Five Languages, One Indent

This code formatter takes a source file read in the same manner as the language itself, and reformats the layout. Choose one of five modes, adjust the indentation and the type of braces, and finally copy the reformatted text into your editor. All changes made will be stored locally within your browser.

  • FreePrice
  • 5Languages
  • NoneSign-up
  • NoneUploads

Code Beautifier

Reindent Java, C, C++, ActionScript or a crontab, and see the line numbers of anything unbalanced

Ready. Paste a file, or press one of the sample buttons.

Your browser does all of the work here, in this tab. We upload nothing, store nothing and log nothing.

Run it on a test page before the live one

Keep a copy of your original code and try the result on a test page before you replace the live file. No automatic tool can see every way your site uses its code.

Add a link to this tool on your own site

Copy the code below and drop it into your page. It places a link to this free tool.

The short version

What A Code Beautifier Does To A File

Layout and meaning are two distinct aspects of source code. As such, a code beautifier may alter each space in the code without altering its function.

That is exactly what we say we’ll do, and that’s exactly what we deliver. When this code beautifier reads your files, it determines where each line begins and ends (i.e. blocks of code), then outputs those lines with the correct indentation based upon how deeply nested the block of code was.

The single editor that sits behind all five modes of the code beautifier — Java, C, C++, ActionScript, and crontab — uses essentially the same reader for the first four, due to their shared brace-based syntax. But each of the five modes gives its own specific rule set on top of the common reader.

All the work takes place within this tab. This allows you to organize your files under a confidentiality agreement without having them uploaded to a server.

The final product is a document which can be read by a reviewer. A diff will illustrate the entire change in the way it looks, so you have complete control over what gets returned to the repository.

How A Code Beautifier Reads A File

The format process does not just count spaces. It processes the text one time and maintains the current state.

Diagram of five Java lines and the indent depth after each; braces inside strings and comments leave it unchanged
Four states, one counter, and an authorization for the counter to increment only if the user is now executing code.

The walker knows four states: ordinary code, a string literal, a character literal and a comment. Because a brace means one thing in code and nothing at all inside a string, the state decides everything.

Code beautifier re-indenting a flat Java class two spaces per level, with braces inside a string and a comment
The Java example, before and subsequent to, along with the number of items that the reader discovered below.

The one thing that distinguishes a search and replace tool from a beauty tool is that rule. If you have a regular expression that indents at each open brace, it will also indent the code within the quotes if there is a brace inside those quotes.

Code beautifier on a broken Java file, with notes naming the line of a stray closing brace and an unclosed string
A broken file, and the notes that name the line each fault sits on.

The walker measures a line instead of rewriting it. It is the depth of the walker at each newline that determines the indentation of the following line.

When walking through the document, the depth increases each time we see an opening brace and decreases each time we see a closing brace. But when we walk past a brace that is part of either a comment or a string, our depth does not change.

Why A Brace Inside A String Matters

A line that creates a message from an isolated curly bracket or a regular expression that is created as a string will be viewed by a counter as being at the beginning of a block.

At that time, the indentation was off throughout the entire rest of the file. But this error went unnoticed because the file continued to compile correctly. Unfortunately, the file appeared unreadable after the first incorrect indentation.

The walker avoids counting within a literal. A brace within a string is text, a brace within a comment is text; only a brace within the code changes the depth.

The walker deals with a backslash-escaped quote the exact same way. Since a backslash within a literal allows the walker to remain in that literal, a quote placed at any point within the literal will not cause the literal to terminate.

The same state machine explains three of the common failures listed further down this page. Hence it is worth reading twice.

Indent And Brace Style, Side By Side

Two settings do most of the visible work in a code beautifier. The first is the indent, and the second is where the opening brace sits.

Code beautifier with braces on their own line and a four-space indent, moving four opening braces onto new lines
Choosing the brace style, and the note that says exactly which lines moved.

Indent width may be expressed by a series of from two to eight spaces, or a single tab. Since this selection represents a "house style" and not a correct vs incorrect decision, any of these options are acceptable as long as the document remains uniform.

The brace style has three common answers, and the disagreement is old enough that Wikipedia lists the named variants. In the first, the brace opens at the end of the line that needs it. In the second, the brace opens on a line of its own at the same indent. In the third, the brace opens on its own line indented one level in.

Because mixing these two things in a single file will make the file difficult to read through, the setting is applied to the entire file all at once and the rest of the file will be formatted according to the new setting.

The opening bracket determines where the closing bracket will be indented. This single principle is the reason why long methods are readable. It allows the reader’s eyes to locate the end of a block of code easily without having to count each opening bracket.

What Crontab Formatting Adds

The reader will then switch to a different type of job because a crontab file isn’t a brace language. Rather than use nested structures, the reader will check that every line in the file has the correct amount of fields.

Code beautifier in crontab mode, lining up the five schedule fields of a nine-line file and leaving the @reboot line alone
Crontab mode spaces the five columns out and leaves the command, and any at-sign line, untouched.

Five fields form a schedule: minute, hour, day of the month, month, and day of the week, which the cron reference describes. Following these five fields will be the command to run, and its spacing should remain unchanged.

So, the program will find each field, name those which are missing, and point out a value that cannot make sense. Both 26 hours and 61 minutes are examples of impossible entries, and both can be typed quickly.

A step value and a range get the same treatment. Because five slash two and nine dash seventeen are the two shapes people mistype most, the tool checks each against its own limits.

Comments that begin with a ’#’ will remain in their present location. As a result, the file will be returned with all of your comments and column alignment intact.

Video guide

How the Code Beautifier Works, Step by Step

This brief video shows each step from start to finish. Choose your language below, and switch on captions if they help.

Step by step

Tidying Messy Code With The Code Beautifier

You only have to take four steps, and there isn’t a single step where you have to register. The code beautifier will complete the process.

  • Pick the language

    So, use either Java, C, C++, ActionScript, or crontab. This is because the reader selected will depend upon the mode you have chosen, and this will be the only factor used to work out how the software interprets your input.

  • Paste the file

    Then you can either put in the entire file or just one method. Since the walker reads the text and not the compiled version, you don’t need to put in anything more than a single method as long as the brackets match up.

  • Set the indent and the brace style

    After picking how many spaces (two, four or eight), or if you use a tab; then select where the open brace will go. This way you’re using the "house style" and not what’s default for your editor.

  • Check the notes, then copy the result

    Work through the notes below the preview. After doing so, either copy the generated content back or save the file with the appropriate extension for your selected mode.

Why this page

Reasons To Use This Code Beautifier

Five reasons, and each one saves a round trip.

  • It reads strings and comments

    The walker keeps track of where it is in the code. A brace that is contained within a literal or a comment will always remain at the same indentation level. So, a file with many braces in the form of a message should be returned without any issues.

  • Five modes, one editor

    You can easily move from Java to crontab, and vice versa, without having to open an extra tab. Combined, these two options give access to almost all of the files in your build directory.

  • Nothing uploads

    In this tab, you can read the original text and then rewrite it. Because of this, files with confidentiality clauses are treated the same as files available to the public.

  • Each note points to its line

    Since the notes find the line of a non-matching brace, finding a file that can’t be resolved is simple. As such, the tool also serves as a rapid sanity check.

  • Your code keeps exactly the same meaning

    Only the whitespace changes. So, a token, a string, or a comment will retain all of its original characters and the only thing that will change in a review is how they are laid out.

Rules of thumb

Habits That Keep Formatted Code Readable

Six habits that decide whether a formatted file stays readable.

  • Format the file, not the fragment So run the whole file through at once. A method formatted on its own can land at a different depth from the class around it, and the join shows.
  • Keep one indent per repository The quantity is not as important as maintaining consistency, since a "mixed" repository will make each of your diffs appear very noisy. So, select one of these options and use it within your editor configuration.
  • Format before the review, not during it Put the layout change into its own commit. A reviewer will then see a whitespace-only diff, and the logic change can be read separately.
  • Leave long string literals alone A beautifier will change the content of a literal when wrapping. So, when you have a literal, such as a URL or an SQL statement, do not wrap the line(s) containing that literal.
  • Check the crontab columns by eye The five fields will be read once after being formatted. A value that the tool can process may cause the job to run at an incorrect time, and the file contains evidence of this.
  • Watch the first line count Finally, check the line counts before and following the process. A formatter only changes the position of white space, so the line count will be the same. If there is a change, something else must have occurred.
Careful here

Six Ways A Formatter Surprises You

Each of these looks fine in the preview and costs time later.

  • Formatting a file that does not compile

    Be careful. If there is a brace that does not match, then the program will display the rest of the file on the next line. This causes the preview to appear shifted and the error message states the exact line where the error occurred.

  • Expecting a preprocessor to be understood

    A "hash line," such as a #define or #include, is read by the reader as a line of text, not as part of the code. As a result, when you use a macro to open a brace, it throws off the depth count and the tool lets you know.

  • Pasting two files at once

    The two files (translation units) contained within a single pane will use the same depth counter. This means that when the first file is completed, the second file will begin at the position where the first file left off.

  • Trusting an indent while a string stays open

    A quote that isn’t closed will keep the walker inside of a literal until the end of the file. As a result, the walker can go past every single brace that follows the unclosed quote, and the indentation will no longer change.

  • Reformatting a file that is under review

    A layout change on a branch somebody else is reading makes the real diff hard to find. So agree on the moment, or wait for the merge.

  • Assuming the language mode is a compiler

    The choice of mode merely determines what kind of reader will be used to process the input. This type of reader does not build a parse tree so it does not report syntax errors such as "wrong type" or "missing semicolon."

Questions

Code Beautifier Questions

Is this code beautifier free?

Yes. You never sign up, never hit a cap and never meet a paywall. So you format as many files as you like.

Which languages does it format?

The five programming languages that are written with braces are Java, C, C++, ActionScript, and crontab. Each of the brace languages has a common reader but each adds their own extra rules.

Does it change what my program does?

No. Only the position of whitespace is moved. All tokens, strings and comments retain their original character sequence, and so the resulting code will compile exactly the same as the input.

Will it indent a brace inside a string?

No, when the walker enters a literal state, he will pass through all of the content that is within that state. As a result, if you enter a message or a regular expression with braces, those braces will be ignored by the walker for indentation purposes.

Will it repair syntax errors?

No, it tells you about the unbalanced brace and gives you the line number. This is helpful for finding the error. You’ll still have to fix the code yourself, though.

Can I choose the indent width?

Yes. Two, four, or eight spaces, or a single tab. These settings apply to the entire file rather than a portion.

Which brace styles can I choose?

Three. The opening brace at the end of the line, on its own line at the same indent, or on its own line indented one level in. Allman and K&R are the two you have probably met.

Does it collapse blank lines?

Yes, it will. One run of blank lines within a method becomes one; the single blank line separating two methods remains.

Can it format a crontab file?

Yes, that’s the fifth mode. It aligns the five schedule fields, keeps comment lines where they are, and flags a value outside its range.

Will the beautifier run my code?

No, it doesn’t run anything, build anything, or calculate anything. It simply processes the file as plain text and produces plain text.

Does the code I paste ever reach a server?

No. Both the reading and the rewriting take place within this browser window. As such, a document that falls under a confidentiality agreement remains on your device.

Is it possible to paste a full file?

Yes, a file with thousands of lines can be formatted within seconds on your personal computer; so, there is no limitation from a server.

Does it handle a header file?

Yes, a C or C++ header has the same grammar rules and will format in the same manner. Macro heavy parts of the file may need to be examined again as mentioned previously.

What line endings come out?

The ones the input used. Because a file with carriage returns keeps them, pasting the result back does not turn every line into a change.

Can I save the tidied code as a file?

Yes. The downloaded file will have the same extension that the chosen mode indicates, thus, if you download a Java program, it will arrive as a text file instead of being displayed as a web page.

Does it sort imports or reorder anything?

Lines always remain in place. Reordering lines can alter the meaning of a document. So, the application will only move white space.

Will it minify code too?

We did not select this option for a reason. In addition, minification also removes comments from code, so it is a separate choice. The minifiers used at our company do a good job of removing comments.

Why is my file still indented oddly?

Usually, this happens because of a quotation mark that wasn’t closed correctly or a brace that was not closed correctly before that point in the file. The notes at the bottom of the preview will tell you exactly which line of code stopped being matched.

Does it also work on a phone, not only a computer?

Yes. The panes are stacked, and the settings are wrapped around. A simple fix can be done off-site, but a full file is much easier to work on with a keyboard.

Will the beautifier keep my comments?

Yes. Line comments and block comments are preserved; so, the depth counter continues past them. So, a comment will not affect the appearance of code near the comment.

Is the formatted code mine?

There are no rights being claimed on your file, so it’s yours.

Is an account or a plugin required?

No, there is no need for you to sign-up, install a plug-in or add an extension. Once your file has been organized correctly, you can close the browser tab.

Keep going

Handy Companion Tools For This Job

Four pages the same job usually leads to.

  • CSS minifier

    Once you have finished laying out your stylesheet, remove the blank space. Since every comment and new line will be paid for in the file that you send, this is the final step before uploading.

  • JavaScript minifier

    Then shrink a script the same way. So the file a browser downloads is smaller than the file a reviewer reads.

  • JSON formatter

    A JSON document deserves the same treatment as a source file. Because a nested object on one line hides its own shape, this page unfolds it.

  • CSS selector tester

    When a rule in the stylesheet matches nothing, this page names it. Since a selector that matches nothing fails silently, that is worth a check.

Reindent The File And Get On With It

Pick the language, paste the file, set the indent and the brace style, then read the notes. Nothing uploads, nothing executes, and only whitespace changes. Most files are readable again before the kettle boils.

Prefer Denwix on GoogleReview Denwix on Google