是否可以将 IRB 提示配置为动态更改?
我想在 IRB 中的文件系统中导航并更改提示符以反映当前工作目录,但我不知道如何在每个命令后更新提示符。最终,我希望在日常工作中更多地使用 IRB,让 bash 消失。我在我的 .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
但是 IRB 提示未更新:
julianmann@mango:~ > irb
/users/julianmann > puts pwd
/users/julianmann
/users/julianmann > cd 'dev'
/users/julianmann > puts pwd
/users/julianmann/dev
/users/julianmann >
我真的希望更改提示。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是获取工作目录的快速技巧。它有点脆弱,但它可以在 ruby 1.8.7 和 1.9.2 上运行。
将提示字符串设置为如下所示:
irb 本身无法理解“%~”指令,因此我用它来进行替换。此 hack 依赖于 irb 调用 dup 来生成提示的事实。
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:
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.
另一种选择是使用 fresh。它基于 irb 替代方案 ripl 并且还显示当前目录作为提示:]
Another option is to use fresh. It's based on the irb alternative ripl and also shows the current directory as its prompt :]
您必须像这样运行(别名)irb
或者将
IRB.conf[:PROMPT_MODE] = :CUSTOM
添加到您的.irbrc这不是您问题的准确答案。但您可以尝试使用 RUSH。
它没有当前工作目录的概念,但很容易配置。
You have to run(alias) irb like so
Or alternatively add
IRB.conf[:PROMPT_MODE] = :CUSTOM
to your .irbrcP.S. This isn't an EXACT answer to your question. But you might try using RUSH.
It doesn't have the concept of a current working directory, but it is easily configurable.
虽然它有点静态
但看看,它可能对你有帮助
在 Linux ( Ubuntu 14.04) 中,
您只需按照一些简单的步骤即可更改 irb 控制台的恼人提示
打开终端
,转到位置 /home/leapfrog/.rvm/scripts
打开文件“irbrc.rb”,使用超级用户权限覆盖
你可以看到这样的一部分代码。将前者替换为后者代码
只需保存文件并重新启动 irb 控制台
更多模式你可以看这个链接
https://cbabhusal.wordpress.com/ 2014/12/22/ruby-rvm-change-prompt-of-irb/
Its kind of static though
but have a look, it may help you
IN Linux ( Ubuntu 14.04)
You can change the irritating prompt of the irb console just by following some simple steps
Open your terminal
goto the location /home/leapfrog/.rvm/scripts
Open the file ‘irbrc.rb’, use superuser power to over-write the
You can see a portion of code like this. Replace the former with latter codes
Just save the file and restart the irb console
Further mode you can see this link
https://cbabhusal.wordpress.com/2014/12/22/ruby-rvm-change-prompt-of-irb/
做出贡献,尽管为时已晚:可以在 IRB 环境初始化后更改提示,例如通过
IRB.conf[:MAIN_CONTEXT]
上的某些值如果它可以对讨论
c = IRB.conf[:MAIN_CONTEXT]
的绑定影响提示格式的字段可能包括以下c.prompt_c
c.prompt_i
c.prompt_n
c.prompt_s
c.return_format
c.auto_indent_mode
c.prompt_mode
直接更新
prompt_i
字段的示例:在 IRB 模块文档 (3.0.0) 它可能不受支持 -目前 - 在 IRB 中使用显示提示时将计算的表达式。每个提示字符串可以简单地用作文字格式字符串。
尽管有一些限制,但在 IRB 初始化后可以更新 IRB 提示。
免责声明:这不能保证更新
IRB.conf[:MAIN_CONTEXT]
下与提示相关的所有状态值If it may serve to contribute to the discussion, albeit belatedly: It may be possible to change the prompt after the IRB environment is initialized, such as via some values on
IRB.conf[:MAIN_CONTEXT]
For a binding of
c = IRB.conf[:MAIN_CONTEXT]
the fields affecting prompt formatting may include the followingc.prompt_c
c.prompt_i
c.prompt_n
c.prompt_s
c.return_format
c.auto_indent_mode
c.prompt_mode
An example in updating the
prompt_i
field directly:Outside of the set of formatting specifiers documented in the IRB module documentation (3.0.0) it may not be supported - at present - to use an expression that would be evaluated when the prompt is displayed, in IRB. Each of the prompt strings may be used as simply a literal format string.
With some limitations albeit, but it may be possible to update the IRB prompt after IRB is initialized.
Disclaimer: This is not guaranteed to update all state values related to the prompt, under
IRB.conf[:MAIN_CONTEXT]
Kelvin 聪明的“快速破解”答案依赖于
dup
,但它不再适用于最新版本的 IRB(现在是v1.11.0
)。这是允许提供动态 IRB 提示的替代方案,它利用了
main.to_s
在 IRB 提示配置中作为%m
提供的事实:Kelvin's clever "quick hack" answer, which relied on
dup
, doesn't work anymore with the latest version of IRB (v1.11.0
right now).Here's an alternative that allows providing a dynamic IRB prompt, which takes advantage of the fact that
main.to_s
is provided as%m
in the IRB prompt configs: