Change the prompt in the Rails console
Mar 04 ’09
At work, I frequently find myself in the situation of having two or three or more console windows open, working on multiple apps in different environments. After almost getting myself into trouble on several occasions, I decided that I'd rather have the Rails console tell me what environment I'm running in.
Today I did a little bit of googling, and a little bit of code/docs spelunking, and figured out how to change the prompt. Put the following in your .irbrc:
if ENV['RAILS_ENV']
# Set up the prompt to be slightly more informative
rails_env = ENV['RAILS_ENV'].downcase[0,3]
current_app = Dir.pwd.split('/').last
IRB.conf[:PROMPT].reverse_merge!(:RAILS_ENV => {:PROMPT_I=>"#{current_app} #{rails_env} >> ", :PROMPT_N=>"#{current_app} #{rails_env} >> ", :PROMPT_S=>nil, :PROMPT_C=>"?> ", :RETURN=>"=> %s\n"})
IRB.conf[:PROMPT_MODE] = :RAILS_ENV
end
Now my console prompt looks like this, in development:
app_name dev >>
And like this in production:
app_name pro >>
You can go hog-wild, with this technique, customizing your prompts to your heart's desire.
And I no longer worry about accidentally dropping all of my production db records.
Conversation in progress…
Join the conversation
* indicates a required field









On March 11th John Hinnegan said:
Nice tip
I changed it to use Rails.env which is always set rather than ENV['RAILS_ENV'] which might not be.