Lua Beautifier

Indent a Lua module by its block keywords so every function, loop and conditional lines up with its matching end.

Lua source
Formatted Lua
Runs locally in your browser

About the Lua Beautifier

Lua marks blocks with words rather than braces, so this beautifier counts keywords. A line that contains function, then, do or repeat opens a level, and end or until closes one. Because the counting is per line, a one liner such as local f = function(x) return x * 2 end nets out to zero and stays flat, exactly as you would want.

Continuation keywords are handled separately. A line starting with else, elseif, end or until is printed one level out from the body it follows, and elseif ... then immediately opens the next branch again. Table constructors written with curly braces add a level too, so a long configuration table nests properly.

Long strings and long comments in the [[ ... ]] and --[==[ ... ]==] forms are captured as single units with any nesting level of equals signs. Their contents are copied out unchanged, which matters because Lua data files often embed whole blocks of text that contain the word end.

Line breaks are preserved rather than invented, so this is a re-indenter and not a full reprinter. It cannot split a densely written line into statements, since Lua has no statement terminator to split on. Scripts for Roblox, Love2D, Neovim configuration and Redis all format the same way. For a language with explicit braces try the C Beautifier.

How to use

  1. Paste your .lua file into the left pane, or press Sample to load a small queue module.
  2. Pick an Indent width. Two spaces is the most common convention in Lua projects.
  3. Read the result on the right. If a block ends up too deep, look for a missing end just above that point.
  4. Press Download to save the file, or Copy to paste it back into your editor.

Common questions

Why is one section indented far too deeply?
That is the signature of a missing end. Scan upward from where the drift starts; the block that never closed is usually the one just above.
Does it understand goto labels written as ::continue::?
They are copied through as ordinary tokens and printed at the current block level. No special dedent is applied to them.
Are comments containing the word end a problem?
No. Comments are recognised before keywords are counted, so text inside a -- line or a long comment never changes the indentation.
Can it reformat minified Lua that is all on one line?
Not usefully. Lua does not require semicolons, so there is no safe place to split. Run it through a Lua aware editor first, then use this page to fix the indentation.