Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to configure the IRB prompt to change dynamically?

Tags:

ruby

prompt

irb

I'd like to navigate around the filesystem in IRB and have the prompt change to reflect the current working directory, but I can't figure out how to make the prompt update after each command. Ultimately I'd like to use IRB in day to day work a lot more and let bash slip away. I tried this in my .irbrc:

require 'fileutils'
include FileUtils

IRB.conf[:PROMPT][:CUSTOM] = {
    :PROMPT_N => "\e[1m:\e[m ",
    :PROMPT_I => "\e[1m#{pwd} >\e[m ",
    :PROMPT_S => "FOO",
    :PROMPT_C => "\e[1m#{pwd} >\e[m ",
    :RETURN => ""
}
IRB.conf[:PROMPT_MODE] = :CUSTOM

But the IRB prompt is not updated:

julianmann@mango:~ > irb
/users/julianmann > puts pwd
/users/julianmann
/users/julianmann > cd 'dev'
/users/julianmann > puts pwd
/users/julianmann/dev
/users/julianmann > 

I'd really like the prompt to change.

like image 783
Julian Mann Avatar asked May 20 '11 17:05

Julian Mann


People also ask

What is IRB command used for?

IRB stands for “interactive Ruby” and is a tool to interactively execute Ruby expressions read from the standard input. The irb command from your shell will start the interpreter.

What is Irbrc?

The . irbrc file is nothing else than a Ruby file that gets evaluated whenever we start the console with irb or rails c . We can place it in a home directory ( ~/. irbrc ) or in the project directory (to scope it per project). But only one of these files will take effect, and the global one has precedence.


1 Answers

Here's a quick hack to get the working dir. It's sort of fragile, but it worked on ruby 1.8.7 and 1.9.2.

Set your prompt string to something like this:

"%N(%m):%03n:%i %~> ".tap {|s| def s.dup; gsub('%~', Dir.pwd); end }

The "%~" directive is not understood by irb itself, so I used it to do the replacement. This hack relies on the fact that irb calls dup to generate the prompt.

like image 183
Kelvin Avatar answered Sep 22 '22 13:09

Kelvin