FormatterHQ

Lua Beautifier & Formatter

Format Lua scripts: consistent indentation for functions, loops and conditionals, spacing around operators and one statement per line.

Input LUA
Output LUA
Ready.

How to use the Lua Beautifier

  1. Paste your Lua code into the Input editor, or use Sample, Upload file or Load URL to load a .lua file.
  2. Choose the Indent: 2, 3, 4 or 8 spaces, or tabs.
  3. Press Format. Parse errors are shown with the line and column.
  4. Use Copy or Download above the output panel to take the formatted Lua with you.

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 ... until and do ... end blocks.
  • 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, for or do block is missing its end. Count your end keywords 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 + 1 on 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.

Frequently asked questions

Does the Lua beautifier support Lua 5.4?
Most Lua 5.4 code formats fine, but the new variable attributes <const> and <close> are not supported by the parser and cause an error.
Can I format Roblox (Luau) scripts?
Scripts that use only standard Lua syntax work. Luau-specific features such as type annotations, compound assignment (+=) and string interpolation are not supported.
Why were my single quotes changed to double quotes?
The formatter prefers double quotes for consistency. Strings that contain a double quote keep single quotes so no escaping is needed. Both forms are equivalent in Lua.
Will formatting change how my script runs?
No. Only whitespace, line breaks, quote style and trailing table commas change, none of which affect execution.
Why does it say end expected?
A block such as function, if, for, while or do is missing its closing end. The error position is where the parser reached the end of input or the next unexpected token.

Last updated September 26, 2026

Related tools