How to format irb command prompt
Asked Answered
L

7

17

Previously I was using Ruby 1.8 and my irb command prompt used to look like this:

Air ~: irb
>> a = 1
=> 1
>> b = 2
=> 2
>> a + b
=> 3

I installed rvm (and Ruby 1.9.2) and now my irb command prompt looks like this:

Air ~: irb
ruby-1.9.2-p180 :001 > a = 1
 => 1 
ruby-1.9.2-p180 :002 > b = 2
 => 2 
ruby-1.9.2-p180 :003 > a + b
 => 3 

Is there a way to remove the ruby-1.9.2-p180 :001 from the command line?

Lexicographer answered 18/5, 2011 at 2:37 Comment(2)
Have a look at this: rvm.beginrescueend.com/workflow/irbrc and ruby-doc.org/docs/ProgrammingRuby/html/irb.html (linked from the first link)Orthogenesis
for whatever reason adding a ~/.irbrc like most people are suggesting isn't working for me. any other tips?Lexicographer
Z
24

The irb man page has a section on "Customizing prompt". Here's mine for example:

IRB.conf[:PROMPT][:CUSTOM] = {
  :PROMPT_I => ">> ",
  :PROMPT_S => "%l>> ",
  :PROMPT_C => ".. ",
  :PROMPT_N => ".. ",
  :RETURN => "=> %s\n"
}
IRB.conf[:PROMPT_MODE] = :CUSTOM
IRB.conf[:AUTO_INDENT] = true

To use this, add it to your ~/.irbrc file (creating it if it doesn't exist.)

Zhukov answered 18/5, 2011 at 7:0 Comment(2)
official link has more information.Antipodes
Been using pry exclusively for a while now.Zhukov
C
20

In your ~/.irbrc, simply add

IRB.conf[:PROMPT_MODE] = :SIMPLE
Camarena answered 18/5, 2011 at 3:42 Comment(1)
doesn't seem to work Air ~: more .irbc IRB.conf[:PROMPT_MODE] = :SIMPLE Air ~: irb ruby-1.9.2-p180 :001 > exitLexicographer
A
9

When you would usually run the irb command, try running irb --simple-prompt instead. That greatly shortens the prompt and makes it easier to understand.

Apex answered 18/5, 2011 at 2:49 Comment(0)
T
4
irb --simple-prompt

saw this in Lynda.com

Tarsier answered 20/12, 2016 at 9:37 Comment(1)
Is able to initialization --simple-prompt on ~/irbc ?Stickpin
P
2

To avoid giving the prompt you wish on the command line all the time, you can configure the prompt via the ~/.irbrc config file:

$ echo "IRB.conf[:PROMPT_MODE] = :DEFAULT" > ~/.irbrc
$ irb
irb(main):001:0> quit
$ echo "IRB.conf[:PROMPT_MODE] = :SIMPLE" > ~/.irbrc
$ irb
>> quit
$ 
Plovdiv answered 18/5, 2011 at 3:44 Comment(0)
A
1

Whoever want to add a prompt timestamp, this isn't possible yet (check "special strings" section), so I implemented it in a monkey-patchy way:

module IrbTimePrompt
  def prompt(prompt, ltype, indent, line_no)
    # I used %T as time format, but you could use whatever you want to.
    # Check https://apidock.com/ruby/Time/strftime for more options
    p = prompt.dup.gsub(/%t/, Time.new.strftime('%T'))
    super(p, ltype, indent, line_no)
  end
end

module IRB
  class Irb
    prepend IrbTimePrompt
  end
end

Now, add this to your lib/ project folder (in case is a Rails project, ensure lib/ is part of config.autoload_paths in config/application.rb) or in a more aggresive way (not recommended), look for lib/irb.rb file in your local ruby instance and in def prompt method, add a new when condition to the method, like:

    when "t"
      Time.now.strftime('%-d-%-m %T%Z')

then in your .irbrc file (it could be located in your home folder or root project folder) you could modify your prompt. I'm adding my current prompt, but please adjust it to your needs:

def rails_prompt
  # This is my base prompt, displaying line number and time
  def_prompt = '[%01n][%t]'
  # Maybe you're only running as `irb` an not `rails console`, so check first
  # if rails is available
  if defined? Rails
    app_env = Rails.env[0...4]
    if Rails.env.production?
      puts "\n\e[1m\e[41mWARNING: YOU ARE USING RAILS CONSOLE IN PRODUCTION!\n" \
           "Changing data can cause serious data loss.\n" \
           "Make sure you know what you're doing.\e[0m\e[22m\n\n"
      app_env = "\e[31m#{app_env}\e[0m" # red
    else
      app_env = "\e[32m#{app_env}\e[0m" # green
    end
    def_prompt << "(\e[1m#{app_env}\e[22m)" # bold
  end

  IRB.conf[:PROMPT] ||= {}
  IRB.conf[:PROMPT][:WITH_TIME] = {
    PROMPT_I: "#{def_prompt}> ",
    PROMPT_N: "#{def_prompt}| ",
    PROMPT_C: "#{def_prompt}| ",
    PROMPT_S: "#{def_prompt}%l ",
    RETURN: "=> %s\n",
    AUTO_INDENT: true,
  }
  IRB.conf[:PROMPT_MODE] = :WITH_TIME
end

rails_prompt

Then start irb or rails console and check the awesomeness:

[1][13:01:15](deve)> 'say hello to your new prompt'
=> "say hello to your new prompt"
[2][13:01:23](deve)>
Albertinaalbertine answered 31/1, 2020 at 0:5 Comment(0)
F
0

See this note about IRB prompt in RVM.

Note that you can create a .irbrc file in your home folder for various settings for IRB. For example, see "Configuring the Prompt" in this document

You can also puts IRB.conf[:PROMPT_MODE] or puts IRB.conf to see all the various settings currently in effect. For example, the :PROMPT_MODE is probably set to "RVM" in your case.

Frit answered 18/5, 2011 at 3:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.