← Back to the index page

StyLua is an optimized code formatter for Lua

Update 2024-11-17

In stylua version 2.0 following changes:

Setup

I have discovered an amazing tool called StyLua for Lua’s code formatting. So you do not need to think about formatting anymore. It works like a prettier version of JavaScript or TypeScript, but for Lua.

It is extremely easy to set up. StyLua can be installed using Rust’s cargo or npm. It depends on the packaging tools you prefer; the commands are:

cargo install stylua

or

npm i -g @johnnymorganz/stylua

NeoVim

In my init.lua for neovim, I just added plugin ckipp01/stylua-nvim and

"ckipp01/stylua-nvim"

And BufWritePre hook to format on auto-save.

vim.api.nvim_create_autocmd({ "BufWritePost" }, {
    pattern = { "*.lua" },
    callback = function()
      -- more commands if needed
        vim.cmd([[silent! !stylua %]])
    end
})

My complete init.lua, which is constantly updated, can be checked on GitHub.

VSCode

For VSCode, installation is even easier; StyLua extension is available on the marketplace. Just one click and install.

Configuration

Currently, I am happy with the default settings, except for line width. I prefer 80 or even 78-column width. Let’s fix that. Several options can be adjusted. Let’s stylua.toml somewhere on the system with the contents:

column_width = 78

Now we have 2 options:

Set a flag --config-path in the stylua command

vim.api.nvim_create_autocmd({ "BufWritePost" }, {
    pattern = { "*.lua" },
    callback = function()
        vim.cmd([[silent! !stylua --config-path={path-to-stylua.toml} %]])
    end
})

Set in plugins options

Consider we use lazy for NeoVim.

{
    "ckipp01/stylua-nvim",
    config = function()
        require("stylua-nvim").setup({
            config_file = stylua_cfg,
        })
    end,
    },

For VSCode it should everything much easier, all configurations should be available in the extension settings.

Conclusion

There is no need to think about code style while coding in Lua and concentrate on the tasks; the formatter does its job. For more detailed editors’ integration guides Check out the official GitHub repo of StyLua.

References

Feedback

For feedback, please check the contacts section. Before writing, please specify where you came from and who you are. Sometimes spammers go insane. Thank you in advance for your understanding.

← Back to the index page