How can I clear the rails console history
Asked Answered
T

2

16

When I run rails c and press the up key when irb starts up, I can see the last commands I entered when my app dropped to irb after encountering a debugger command for the ruby-debug gem. I would not only like to clear these commands out, but I would like it if rails c would pull the last commands I issued during my last rails console session. I think it used to do this but I'm not sure what has changed. I'm on ruby 1.8.7 and rails 3.0.3 on Mac OS 10.6.5 if that helps.

Update

Ray's answer helped me out in the interim. Recently I did a bit more digging to find out more and realized that there are a number of conflicting issues.

IRB checks if you have a ~/.irbrc and if not falls back to /etc/irbrc as Ray mentioned. However, if you are using rvm there is another file to consider ~/.rvm/scripts/irbrc which just loads up ~/.rvm/scripts/irbrc.rb (note the .rb) if you have rvm_path set in your ENV (you should if using rvm).

Interestingly while ~/.rvm/scripts/irbrc.rb was based off of /etc/irbrc they are not the same and differ in a few ways. The most obvious way and easiest way to detect which one is being used on your system is their history file's name. If /etc/irbrc is being used your history file will be ~/.irb_history where as rvm's is ~/.irb-history (Note: _ vs -).

Hopefully this additional information will help you determine what you need to setup your system as you would like.

Pry Concerns

I've since stopped using debugger and have moved to pry-byebug which includes the pry gem. Pry is an alternative to IRB but can also be used along side and within it. The reason I was able to provide the above update is because I was trying to figure out how to keep their respective histories separate. For more information please see my answer to the SO question on "why does pry history keep cloberring irb history?". I've included links there to the known Github issue for Pry as well as my attempt to fix it.

Tranquilizer answered 10/12, 2010 at 19:11 Comment(2)
Were you able to find a solution for this?Reservoir
It looks like Ray might be showing me the right path but I'm still a bit in the woods.Tranquilizer
S
9

I interpret you question as asking how to turn history on in the Rails Console and off in the Ruby debugger. If this isn't true, please clarify.

IRB, and by extension, the Rails Console, read from ~/.irbrc, or if that doesn't exist, /etc/irbrc, to startup and configure irb. Your history is typically written to ~/.irb_history, but that is dictated by the contents of your irbrc file. The /etc/irbrc on my Mac OS X is set up to write the history from irb, so perhaps you've created a local .irbrc that doesn't have history, or perhaps you have a syntax error in that file.

The debugger reads a file called .rdebugrc on startup. You can turn off history in debug by adding this line to ~/.rdebugrc:

set history save off

Turn it back on with:

set history save on

You could also set your debug output to go to a different file than irb reads from with the command:

set history filename

These also work from the debug prompt, but aren't persistent.

Snorkel answered 29/6, 2011 at 7:53 Comment(10)
Thanks for the answer Ray. I'm still trying to figure out what is going on. It could also be that I've been switching between Rails 2.3 and Rails 3 projects as well. I looked in my ~/.irbrc file and it doesn't mention anything about history. However /etc/irbrc says to use ~/.irb_history. Even stranger is that when I run irb or ./script/console I get my history (minus the first entry) written to ~/.irb_history AND ~/.irb-history. I deleted both to be sure and they were both created after one irb session.Tranquilizer
I created and put "set history ~/.rdebug_history" into ~/.rdebugrc. However, after going into the irb debugger from my rails app, I still don't have a ~/.rdebug_history file written though debugger does have history and it is different from irb's and script/console's.Tranquilizer
I noticed I had require 'utility-belt' in my ~/.irbrc. Commenting out this line made it so irb and script/console only produced ~/.irb-history. Putting it back in created and populated ~/.irb_history with all that was in ~/.irb-history.Tranquilizer
Yes, the way the these histories work is that they are read in on startup up, accumulated during your session, and then maxlines number of lines are written back out. If you start up one session of irb/console, perform some actions, and then start a different session in another window, that second session will begin with the same history as the first when it started. It won't have the commands that you entered in the first session.Snorkel
Also, for debug, you can see the filename for where the debug is being stored by typing show history at the debug prompt.Snorkel
Hey Ray, I don't know why it's working all of a sudden but this morning I tried show history in a debug session and it had the file I put in ~/.rdebugrc. When I exited, that file was actually written this time. Still not entirely sure why I have both ~/.irb-history and ~/.irb_history but it looks like irb history and debugger history aren't being confused anymore.Tranquilizer
Okay, one last revelation for now. In debugger, if I type continue instead of exit (and thus don't have to restart mongrel) the history doesn't get written. That at least explains why I wasn't getting anything written to ~/.rdebug_hist last night.Tranquilizer
Aaron, That's good infomation. I've been confused by that as well. I wonder if you do, continue, then stop at another break point, then exit, if you'll preserve the history from before the continue?Snorkel
I just found out that, at leas on Linux Mint, the history file is actually called ~.irb-history (so with a hyphen instead of an underscore)Quickwitted
@Quickwitted I noticed this odd inconsistency too! I've updated my original question with my findings. tl;dr you are almost certainly using rvm which is why you have the hyphen instead of the underscore.Tranquilizer
H
0

Turns out RVM slightly changed over time. Currently my IRB history (using rvm) is stored here:

user@host:~$ ls ~/.rvm/rubies/ruby-2.4.2/.irbrc*
/home/user/.rvm/rubies/ruby-2.4.2/.irbrc
/home/user/.rvm/rubies/ruby-2.4.2/.irbrc_history
Hilaryhilbert answered 5/7, 2018 at 7:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.