bundle_alphabetically
bundle_alphabetically is a Bundler plugin that keeps your Gemfile organized by alphabetizing gem declarations within each group.
-
Automatic mode: after
bundle add/bundle install, it rewrites yourGemfileso gems are sorted alphabetically inside each group. -
Manual mode: run
bundle sort_gemfilewhenever you want to clean things up (with an optional--checkfor CI).
Installation
Install the plugin from RubyGems:
bundle plugin install bundle_alphabeticallyOr add it to your Gemfile and install:
plugin "bundle_alphabetically"bundle installFor development, you can install from a local checkout:
cd /path/to/bundle_alphabetically
bundle plugin install bundle_alphabetically --path .Once installed, Bundler will load plugins.rb and register the command and hooks.
What it does
- Sorts
gementries alphabetically by name within each context:- top-level (no group)
- each
group :name do ... endblock, separately
- Preserves:
- non-gem lines like
source,ruby,plugin,path, etc. - group block structure and indentation
- comments and most surrounding blank lines
- multi-line
gemdeclarations (e.g. options hashes split across lines)
- non-gem lines like
It operates on the current Bundler.default_gemfile and rewrites it in place.
Automatic sorting (hook)
The plugin registers an after-install-all hook:
- After a successful install (including
bundle add), Bundler calls intobundle_alphabetically. - The plugin sorts the
Gemfileand prints a short message like:Gemfile gems alphabetized by bundle_alphabetically
If the Gemfile is already sorted, it does nothing.
If it encounters an error parsing the Gemfile, it raises a Bundler::BundlerError and reports a message via Bundler.ui.error, but leaves the file unchanged.
Manual command
The plugin adds a bundle sort_gemfile command via Bundler::Plugin::API:
bundle sort_gemfile- Sorts the current
Gemfilein place using the same rules as the hook. - Prints
Gemfile already sortedif no changes were needed.
For CI, you can use --check (or -c) to verify sorting without modifying the file:
bundle sort_gemfile --checkIn --check mode:
- Exit successfully if the
Gemfileis already sorted. - Raise
Bundler::BundlerError(non-zero exit) if changes would be required.
Limitations
- Designed for conventional
Gemfiles:- top-level
gemcalls andgroup ... doblocks - straightforward multi-line
gementries
- top-level
- It does not attempt to fully evaluate arbitrary Ruby or heavy metaprogramming inside the
Gemfile.
If you have an unusually dynamic Gemfile and hit issues, you can temporarily uninstall the plugin with:
bundler plugin uninstall bundle_alphabetically