rails_server_hotkeys
Keyboard shortcuts for bin/rails server.
Press r to restart the server. No more touch tmp/restart.txt in another terminal.
=> Booting Puma
...
› Press r to restart, h for help
Install
# Gemfile
gem "rails_server_hotkeys", group: :developmentThat's it. Run bin/rails s and press a key.
| Key | Action |
|---|---|
r |
restart the server (same as bin/rails restart) |
c |
clear the terminal |
o |
open the app in the browser |
h / ?
|
list hotkeys |
q |
quit the server |
Custom hotkeys
# config/initializers/hotkeys.rb
if defined?(RailsServerHotkeys)
RailsServerHotkeys.register("m", "run db:migrations") { system("bin/rails db:migrate") }
RailsServerHotkeys.register("t", "run custom task") { system("bin/rails myapp:do_something") }
RailsServerHotkeys.register("e", "run system echo") { system("echo 'custom message'") }
endRegistering an existing key replaces it; RailsServerHotkeys.unregister("o") removes one.
Configuration
# config/environments/development.rb
config.hotkeys.enabled = false # turn it off
config.hotkeys.url = "https://myapp.test" # URL opened by `o` (default: localhost on the port the server is listening on)Or disable for a single run: RAILS_SERVER_HOTKEYS=false bin/rails s.
Color
Messages are light blue by default. Change it in an initializer:
# config/initializers/hotkeys.rb
if defined?(RailsServerHotkeys)
RailsServerHotkeys.color = :green # black, red, green, yellow, blue, magenta, cyan, white,
# or light_* of any of them (the default is :green)
RailsServerHotkeys.color = 208 # 256-color palette (0-255)
RailsServerHotkeys.color = "#ff8800" # true color (hex)
RailsServerHotkeys.color = nil # no color
endAn invalid value raises ArgumentError at boot. Colors are skipped when the output isn't a terminal or NO_COLOR is set.
When is it active?
Only for rails server, in the development environment, when stdin is a terminal. Console, rake, tests, jobs and non-interactive runs (Docker without -it, CI) are untouched.
Caveats
-
Your
config.rumust callRails.application.load_server. Rails has generated it since 6.1; apps created on older versions may lack it, and then the gem does nothing. Add it afterrun Rails.application. -
Restart needs Puma's
tmp_restartplugin. It's in theconfig/puma.rbgenerated by Rails; if yours lacks it, addplugin :tmp_restart. -
bin/dev(foreman) doesn't forward stdin, so hotkeys can't work there. Runbin/rails sdirectly, or use overmind andovermind connect web. -
Unix/macOS only (uses
stty). On Windows the gem does nothing.
Development
bundle install
bundle exec rake testRails and Puma versions
rake test:matrix boots a real bin/rails server in a pseudo-terminal for each Rails/Puma combination in gemfiles/ and checks the banner, r and q:
bundle exec rake test:matrix
bundle exec rake test:matrix GEMFILES=rails_8.1 # only matching gemfilesThe first run installs each bundle, so it takes a while. To add a combination, copy one of the files in gemfiles/.
Trying it in a dummy app
The unit tests run without Rails, but hotkeys only show up for real under rails server in a terminal. To try your changes by hand, create a throwaway app next to the gem:
# Needs the rails and bundler gems (`gem install rails bundler`)
cd ..
rails new hotkeys-dummy --minimal --skip-git
cd hotkeys-dummy
# Point the app at your local copy of the gem
echo 'gem "rails_server_hotkeys", path: "../restart-rails-hotkeys", group: :development' >> Gemfile
bundle install
bin/rails sAdjust the path: if your clone has a different folder name.
You should see this in green, right after Puma's Use Ctrl-C to stop:
› Press r to restart, h for help
Things to check:
-
rprints* Restarting...and the server comes back up -
hlists the hotkeys,cclears the screen -
oopens the browser on the port the server is listening on (trybin/rails s -p 4000) -
qor Ctrl+C stops the server, and your shell echoes what you type again -
bin/rails s < /dev/nullboots normally with no banner (no TTY, hotkeys off)
Because the gem is loaded through path:, edits to it take effect on the next restart: press r instead of stopping the server. You don't need to run bundle install again unless you change the gemspec.
Acknowledgments
Inspired by the keyboard shortcuts in Expo CLI.
A note to the Rails team
If you find this useful, feel free to bring the idea into Rails itself.
License
MIT