English | 简体中文
VS Code extension for formatting any file using external command.
Usage
- Install from marketplace: https://marketplace.visualstudio.com/items?itemName=lxl66566.anyformatter-vscode
- Set anyformatter as default formatter for the languages you want:
"[toml]": {
"editor.defaultFormatter": "lxl66566.anyformatter-vscode"
},
"[nix]": {
"editor.defaultFormatter": "lxl66566.anyformatter-vscode"
},
- Then: see below ↓↓↓ for all changes, the extension must be restarted to take effect.
stdin and stdout
If the external formatter accepts stdin and outputs to stdout:
"anyformatter": {
"toml": { "command": "taplo fmt -" },
"nix": { "command": "nixfmt -" }
},
That's simple.
file
If your external formatter cannot use stdin/stdout, and must be called with a file path:
"anyformatter": {
"toml": {
"command": "taplo fmt",
"file": true,
}
"[toml]": {
"editor.formatOnSave": false // important!
},
},
the command will be executed as taplo fmt "<file_path>"
.
Note that the formatOnSave
setting must be set to false
, because we need to save first, then format, but the vscode formatOnSave
will trigger format first, and then save.
If you still want to format on save, you can use the format_on_save
option provided by this extension:
"anyformatter": {
"toml": {
"command": "taplo fmt",
"file": true,
"format_on_save": true
}
"[toml]": {
"editor.formatOnSave": false
},
},
So that you can format on save. Now the key("toml"
) matches the file extension.