Skip to content

Instantly share code, notes, and snippets.

@OnesimusUnbound
Last active December 17, 2025 12:43
Show Gist options
  • Select an option

  • Save OnesimusUnbound/4e00724716c73c57e9ebcf936b5daa35 to your computer and use it in GitHub Desktop.

Select an option

Save OnesimusUnbound/4e00724716c73c57e9ebcf936b5daa35 to your computer and use it in GitHub Desktop.
micro editor format command

Overview

format command changes the content of the file based on the file type and the formatter command associated with the file type.

How to Set Up?

  1. Open ~\.config\micro\init.lua. Create new file when non-existent
  2. Modify init.lua. Refer to init.lua in this gist.
  3. Update ~\.config\micro\settings.json to add the formatters. I've tested only python, lua and html. Others may work 🙏

How to Add Other Formatters

  1. For other formatter not listed, refer to the documentation of the cli of the formatter.
  2. To add the other formatter, open ~\.config\micro\settings.json, add the follwing key, where %f refers to the path of the current file: "formatter.filetype.<target filetype>": "cli formatter and its arguments"
  3. To replace the formatter for a file type, update the existign entry in settings.json or run the command set formatter.filetype.<target filetype> "cli formatter and its arguments". For an example, to use ruff as python formatter, run the following command: set formatter.filetype.python "ruff format %f"

Note

For some reason, micro does not properly reload files that uses dos line ending. You can check the file format by running this command: show fileformat. Changing the file format to unix fixes the issue. In case there's no problem with changing the line ending from dos to unix, run command setlocal fileformat unix before running the format command. If it's not possible to change from dos to unix line ending, you may exit micro and reload. Not ideal though 🥲

-- add the following in the `init.lua` file. Adapt this to existing custom code in your `init.lua` file when needed.
local micro = import("micro")
local config = import("micro/config")
local shell = import("micro/shell")
local filepath = import("path/filepath")
function init()
config.MakeCommand("format", run_format, config.NoComplete)
end
function run_format(bp)
bp:Save()
local ft = bp.Buf:FileType()
local file = bp.Buf.Path
local absFile = filepath.Abs(file)
local cmd = config.GetGlobalOption("formatter.filetype." .. ft)
if cmd == nil or cmd == "" then
micro.InfoBar():Error("No formatter configured for filetype: " .. ft)
return
end
cmd = cmd:gsub("%%f", absFile)
local _, err = shell.RunCommand(cmd)
if err ~= nil then
micro.InfoBar():Error(err)
return
end
-- INFO: reopen fails for files that uses dos (windows) line ending
bp.Buf:ReOpen()
end
{
"formatter.filetype.angular": "npx prettier --write %f",
"formatter.filetype.css": "npx prettier --write %f",
"formatter.filetype.flow": "npx prettier --write %f",
"formatter.filetype.go": "goimports -w %f",
"formatter.filetype.graphql": "npx prettier --write %f",
"formatter.filetype.html": "npx prettier --write %f",
"formatter.filetype.javascript": "npx prettier --write %f",
"formatter.filetype.json": "npx prettier --write %f",
"formatter.filetype.jsx": "npx prettier --write %f",
"formatter.filetype.lua": "stylua %f",
"formatter.filetype.less": "npx prettier --write %f",
"formatter.filetype.markdown": "npx prettier --write %f",
"formatter.filetype.python": "python -m black %f",
"formatter.filetype.scss": "npx prettier --write %f",
"formatter.filetype.typescript": "npx prettier --write %f",
"formatter.filetype.vue": "npx prettier --write %f",
"formatter.filetype.yaml": "npx prettier --write %f"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment