What the Lua beautifier does
Lua scripts written quickly, in game mods, configuration files or embedded systems, often end up with several statements on one line and inconsistent indentation. This Lua beautifier parses your code with lua-fmt (built on the luaparse parser) and prints it again from the syntax tree: one statement per line, blocks indented, spaces around operators, and consistent string quotes. Because it parses the code, it also tells you when a script is syntactically invalid.
Example
local Account={}Account.__index=Account
function Account.new(owner,balance) local self=setmetatable({},Account) self.owner=owner self.balance=balance or 0 return self end
function Account:deposit(v) if v<=0 then error('invalid amount') end self.balance=self.balance+v end
for _,name in ipairs({'ada','linus'}) do local a=Account.new(name) a:deposit(10) print(a.owner,a.balance) end
local Account = {}
Account.__index = Account
function Account.new(owner, balance)
local self = setmetatable({}, Account)
self.owner = owner
self.balance = balance or 0
return self
end
function Account:deposit(v)
if v <= 0 then
error("invalid amount")
end
self.balance = self.balance + v
end
for _, name in ipairs({"ada", "linus"}) do
local a = Account.new(name)
a:deposit(10)
print(a.owner, a.balance)
end
Lua does not need semicolons or newlines between statements, which is why minified or hastily written Lua can pack an entire function onto one line. After formatting, the class-like pattern with setmetatable and the method defined with : are easy to follow.
Formatting rules applied
- Indentation of
function,if/elseif/else,for,while,repeat ... untilanddo ... endblocks. - One statement per line, and multiple blank lines reduced to one.
- Spaces around binary operators (
..,==,~=, arithmetic) and after commas. - Double quotes for strings, unless the string contains a double quote, in which case single quotes are kept to avoid escaping. Long strings
[[ ... ]]are untouched. - Trailing commas in table constructors are removed:
{1, 2, 3,}becomes{1, 2, 3}. - Comments, both
--line comments and--[[ ]]block comments, are kept.
Supported Lua versions
The parser accepts Lua 5.1, 5.2 and 5.3 syntax, including goto and labels, integer division // and bitwise operators. Lua 5.4 variable attributes such as local x <const> = 5 are not supported and produce a parse error; remove the attribute to format the rest of the file. LuaJIT code that sticks to 5.1 syntax works. Luau (Roblox) type annotations like local x: number are not standard Lua and are not supported.
Indentation conventions
There is no official Lua style guide, but two conventions dominate. Many Lua projects use 2 spaces (the default here), while many game engines and editors default to 4 spaces or tabs. Whatever you pick, apply it to the whole project so diffs stay clean.
Common Lua errors reported
- "'end' expected near <eof>" – a
function,if,forordoblock is missing itsend. Count yourendkeywords from the reported line upward. - "unexpected symbol near ..." – often a typo such as
!=instead of Lua's~=, or a+=operator, which Lua does not have. - "'=' expected" – a statement that is only an expression, like
x + 1on its own line; Lua requires statements to be assignments or calls.
To shrink a script for embedding, use the Lua Minifier, which strips comments and whitespace. For configuration data, JSON Formatter handles the JSON side. Formatting runs in your browser.