FormatterHQ

Lua Minifier

Shrink Lua scripts by removing comments and unnecessary whitespace. Strings, long strings and the shebang line are kept exactly as written.

Input LUA
Output LUA
Ready.

How to use the Lua Minifier

  1. Paste your Lua code into the Input editor, or use Sample, Upload file or Load URL.
  2. Press Minify (auto-updates as you type).
  3. Read the before and after size in the status bar.
  4. Use Copy or Download above the output panel to take the minified Lua with you.

What the Lua minifier does

This Lua minifier tokenizes your script and prints the tokens back with as little whitespace as Lua's grammar allows. Line comments (--) and block comments (--[[ ... ]], including the --[==[ forms) are removed. Spaces are kept only where two tokens would otherwise merge, for example between local and a name, or between end and the next keyword. Lua does not need newlines or semicolons between statements, so a whole program can become one line.

Example

-- Fibonacci with memoization
local cache = {}
--[[ multi-line
     comment ]]
local function fib(n)
  if n < 2 then return n end
  if cache[n] then return cache[n] end
  local value = fib(n - 1) + fib(n - 2)
  cache[n] = value
  return value
end
print("fib(30) = " .. fib(30), [[long  string  kept]])
local cache={}local function fib(n)if n<2 then return n end if cache[n]then return cache[n]end local value=fib(n-1)+fib(n-2)cache[n]=value return value end print("fib(30) = "..fib(30),[[long  string  kept]])

The status bar reports 301 → 207 bytes (31% smaller). The long string [[long string kept]] keeps its double spaces, and so does every quoted string.

What is preserved

  • Single- and double-quoted strings with their escapes, and long strings in [[ ]] or [=[ ]=] form.
  • A #! shebang line at the top of the file, needed to run scripts directly on Unix systems.
  • Number formats, including hexadecimal and exponents, and the separation needed so that 1 .. and a - -b keep their meaning.

What it does not do

Local variable names are not shortened, so the savings come from comments and whitespace. That keeps the output predictable and makes error messages still mention real names. It also means the minifier never changes behavior that depends on names, such as debug.getlocal. The minifier does not check that your code is valid Lua beyond tokenization; format it with the Lua Beautifier first if you want a full syntax check.

When to minify Lua

Minified Lua is useful when scripts are embedded in other files or sent over a network: Redis EVAL scripts, game mods with size limits, scripts stored in configuration or databases, and code for microcontrollers running NodeMCU or similar firmware where flash space is tight. Keep the commented original for development. For plain text rather than code, the Text Minifier collapses whitespace in the same way. Processing happens in your browser.

Checking the minified script

Because the minifier only tokenizes, it is worth confirming that the output still parses before you ship it. The standard Lua distribution includes a compiler that can check syntax without running anything:

luac -p script.min.lua   # prints nothing when the file is valid

Two side effects are easy to forget. Error messages and debug.getinfo now report line 1 for almost everything, so keep the original file for debugging. And comments are gone completely, including type annotations such as ---@param used by editors and language servers, so minify a copy rather than your working source.

Frequently asked questions

Does the Lua minifier rename variables?
No. It removes comments and whitespace only. Names stay the same, which keeps error messages readable and avoids changing behavior that relies on names.
Are long strings and block comments handled correctly?
Yes. Long strings with any number of = signs are kept verbatim, and block comments of the same form are removed completely.
Can I minify Redis Lua scripts?
Yes. Redis scripts are standard Lua, and minifying them reduces what you send with EVAL or SCRIPT LOAD.
Why did I get an Unterminated string error?
A quoted string reaches the end of a line without a closing quote. In Lua, normal strings cannot span lines; use a long string [[ ]] for multi-line text.
Does minified Lua run faster?
Not noticeably. Lua compiles the source to bytecode before running it, and comments and whitespace do not survive that step. Minifying mainly saves storage and transfer size, with a slightly shorter parse time.

Last updated September 26, 2026

Related tools