Code indentation in Lua Programming

LuaServer Side ProgrammingProgramming

Lua codes are not like Python when it comes to indentation. So, most of the code you will write will work even if it falls on another line, and you don’t necessarily need to have the nested code to be intended by a certain tab size.

The code indentation in lua is more about making the code look much better and more readable. If your entire code is on one line or worse, if it is like multiple lines then I am afraid that your code isn’t very readable.

While we can use the code editor’s indentation packages to do the indentation for us, it is still a good idea to use the indentation that Lua provides.

Lua provides us with a script which we can make use of when we want to improve the indentation of our file, and running that script includes several steps and you must be aware of the parameters that you require that will help you increase the code indentation in your file.

Let’s explore why we need the script. Consider the points listed below about the script −

  • Reduces the Inconsistency of tab sizes.
  • Handles added tabs done by automatic indentation.
  • Makes the code look more like a homogeneous appearance.

What the script does:

  • Removes tab indents and replaces them with spaces.
  • Standardized indentation at 2 spaces per indent.
  • Checks that line lengths don't exceed 80 characters.


lua -f codeformat.lua --file myfile.lua --ts 4 --in 4

In the above usage example, the codeformat.lua is the Lua script file that will do all the work for the code indentation for you, the myfile.lua is the file whose indentation you want to improve, the 4 after the --ts flag tells Lua how much tab size you want to keep, and the last 4 tell Lua about the indentation space you want for your file.

Updated on 20-Jul-2021 13:53:46