A fast, opinionated Ruby formatter written in Rust.
- Try it online → rubyfmt.run
Pronunciation: (en) "Ruby Format", (jp) ルビーフォーマット
# Install
brew install rubyfmt
# Format a file in place
rubyfmt -i myfile.rb
# Format and print to stdout
rubyfmt myfile.rbbrew install rubyfmt- Make sure you have Cargo installed
- Run
cargo build --release - Copy
target/release/rubyfmt-mainto somewhere on your path asrubyfmt
| Command | Description |
|---|---|
rubyfmt file.rb |
Output formatted code to stdout |
rubyfmt -i file.rb |
Format file in place |
rubyfmt -c file.rb |
Show diff of changes |
cat file.rb | rubyfmt |
Read from stdin |
You can also pass directories to format multiple files at once.
For the full command line interface, see rubyfmt --help.
Control formatting on a per-file basis with header comments:
rubyfmt --header-opt-in- Only format files with# rubyfmt: trueat the toprubyfmt --header-opt-out- Skip files with# rubyfmt: falseat the top
Create a .rubyfmtignore file in your project root to exclude files from formatting. It uses the same syntax as .gitignore.
By default, rubyfmt also respects your .gitignore. Use --include-gitignored to format those files anyway.
The popular ruby-lsp extension has a rubyfmt add-on. Install the extension:
# Add to project
bundle add ruby-lsp-rubyfmt-formatter --group development
# Install globally
gem install ruby-lsp-rubyfmt-formatterAnd then add the following to your .vscode/settings.json:
{
"[ruby]": {
"editor.defaultFormatter": "Shopify.ruby-lsp",
"editor.formatOnSave": true
},
"rubyLsp.formatter": "rubyfmt"
}Rubyfmt is also supported by the (now-deprecated) VSCode Ruby extension. Add the following to your settings.json:
{
"ruby.useLanguageServer": true,
"ruby.format": "rubyfmt",
"[ruby]": {
"editor.formatOnSave": true
}
}The null-ls plugin supports rubyfmt out of the box:
null_ls.setup({
sources = {
null_ls.builtins.formatting.rubyfmt,
},
})See the null-ls documentation for more details.
- Run
cargo build --release - Add
source /path/to/rubyfmt.vimto your~/.vimrc - Add
let g:rubyfmt_path = /path/to/target/release/rubyfmt-mainbeneath the source line
- Install the File Watchers plugin
- Go to File | Settings | Tools | File Watchers
- Import
watchers.xmlfromeditor_plugins/rubymine/ - Optionally set Level to Global for all projects
See the File Watchers documentation for more details.
Install the rubyfmt plugin via Package Control.
Files format on save or with Cmd + ; (macOS) / Alt + ; (other). Settings:
{
"ruby_executable": "ruby",
"rubyfmt_executable": "rubyfmt",
"format_on_save": true
}Install the rubyfmt package from Settings > Packages.
Files format on save or with Cmd + ; (macOS) / Alt + ; (other).
For usage with Rubocop, see the rubocop-rubyfmt gem.
Please check out our contributing guide.
The original author (fables-tales) is no longer working on open source in their spare time.
Other contributors work regularly on rubyfmt, and contributors with commit access are welcome to merge changes that pass CI.