How Do You Clear The IRB Console?

RubyIrb

Ruby Problem Overview


How do you clear the IRB console screen?

Ruby Solutions


Solution 1 - Ruby

On Mac OS X or Linux you can use Ctrl + L to clear the IRB screen.

Solution 2 - Ruby

Throw this inside %userprofile%\.irbrc and you're good

def cls
  system('cls')
end

From IRB clear screen on windows.

Solution 3 - Ruby

On *nix boxes

`clear`

on Windows

system 'cls' # works
`cls` # does not work

on OSX

system 'clear' # works
`clear` # does not work

Solution 4 - Ruby

Command + K in macOS works great.

Solution 5 - Ruby

On Ubuntu 11.10 system clear will mostly clear the irb window. You get a return => True value printed.

A big mess of ugly text

ruby-1.9.2-p290 :007 > system 'clear'

what ya get:

 => true 
ruby-1.9.2-p290 :007 > 

Solution 6 - Ruby

Just discovered this today: in Pry (an IRB alternative), a line of input that begins with a . will be forwarded to the command shell. Which means on Mac & Linux, we can use:

. clear

And, on Windows (Command Prompt and Windows Terminal), we can use:

. cls

Source: pry.github.io

Solution 7 - Ruby

system 'clear'

Should work for rails 4.0 as well

Solution 8 - Ruby

In order to clear the screen just do:

puts "\e[H\e[2J"

P.S. This was tested on Linux.

Solution 9 - Ruby

On Linux Mint 17 also you can use Ctrl + Shift + L

or

Ctrl + L to clear the IRB screen.

Solution 10 - Ruby

puts `clear`

Clears the screen and then returns => nil Tested on Mac OSX 10.6 Terminal and iTerm2.

Solution 11 - Ruby

Method: def clear_screen if RUBY_PLATFORM =~ /win32|win64|\.NET|windows|cygwin|mingw32/i system('cls') else system('clear') end end

Or in IRB you can use system('clear')

Solution 12 - Ruby

In windows, using Rails 4,

system('cls')

worked for me

Solution 13 - Ruby

Tons of good answers here, but I often remote into a linux box with Mintty from windows. Kudos to the above about using .irbrc, but came up with this:

def cls
  puts "\ec\e[3J"
end

def clear
  puts "\e[H\e[2Js"
end

This gives you the options for both the *nix 'clear' behavior and the Windows 'cls' behavior, which I often find more useful if I really want to nuke the buffer rather than just scrolling it out of view.

P.S. a similar variant also works in .bashrc:

alias cls='echo -e "\ec\e[3J"'

If anyone could find a way to actually map that to a keystroke, I'd love to hear it. I would really like to have something akin to cmd-k on osx that would work in Mintty.

Solution 14 - Ruby

Add the following method to ~/.irbrc:

def clear
  conf.return_format = ""
  system('clear')
end

Cntrl-L or Cntrl-K work in regular console but I'm using tmux and those mess the screen up inside the tmux window.

The conf.return_format = "" takes the nil off the return value.

Solution 15 - Ruby

Windows users simply try,

system 'cls'

OR

system('cls')

Looks like this in the IRB window,

irb(main):333:0> system 'cls'
irb(main):007:0> system('cls')

Did the trick for me in ruby 1.9.3. However the following commands did not work and returned => nil,

system('clear')
system 'clear'
system `cls`       #using the backquotes below ESC Key in windows

Solution 16 - Ruby

I've used this for executable files:

def clear
    system("cls") || system("clear") || puts("\e[H\e[2J")
end

clear

Solution 17 - Ruby

system 'cls' 

Works for me in Windows, with Ruby 2.2.0 and rails 4.0

Solution 18 - Ruby

I came here looking for a way to reset the tty with irb, since it wasn't printing newlines or showing what I typed somehow, only some output.

1.9.3-p125 :151 >   system 'reset'

finally did the trick for me!

Solution 19 - Ruby

For windows users:

If you create a bat file name c.bat whose contents are:

@echo off
cls

Then, in IRB, you can say:

system('c')

to clear the console. I just thought I would share because I thought that was pretty cool. Essentially anything in the path is accessible.

Solution 20 - Ruby

->(a,b,c){x=a.method(b);a.send(c,b){send c,b,&x;false};print"\e[2J\e[H \e[D"}[irb_context,:echo?,:define_singleton_method]

This will fully clear your IRB screen, with no extra empty lines and “=> nil” stuff. Tested on Linux/Windows.

This one-liner could be expanded as:

lambda {
  original_echo = irb_context.method(:echo?)
  irb_context.send(:define_singleton_method, :echo?) {
    send :define_singleton_method, :echo?, &original_echo
    false
  }
  print "\e[2J\e[H \e[D"
}.call

This uses lots of tricks.

Firstly, irb will call echo? to check if the result should be printed. I saved the method, then redefined with a method which restores the defination but returns false so irb will not echo the result.

Secondly, I printed some ANSI control chars. \e[2J will clean the screen and \e[H will move the cursor to the upper left position of the screen. \e[D will print a space and then move back the cursor while this is a workaround for something strange on Windows.

Finally this is kind of not practical at all. Just smile ;)

Solution 21 - Ruby

The backtick operator captures the output of the command and returns it

s = `cls`
puts s

would work better, I guess.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJohn TopleyView Question on Stackoverflow
Solution 1 - RubyJohn TopleyView Answer on Stackoverflow
Solution 2 - RubyBen HoffsteinView Answer on Stackoverflow
Solution 3 - RubyAShellyView Answer on Stackoverflow
Solution 4 - Rubyuser1323136View Answer on Stackoverflow
Solution 5 - RubyTW ScannellView Answer on Stackoverflow
Solution 6 - RubyChandresh PantView Answer on Stackoverflow
Solution 7 - RubyArvind singhView Answer on Stackoverflow
Solution 8 - RubySujit KumarView Answer on Stackoverflow
Solution 9 - Rubythatway_3View Answer on Stackoverflow
Solution 10 - RubyLucas Rockett GuttermanView Answer on Stackoverflow
Solution 11 - RubyStephen RossView Answer on Stackoverflow
Solution 12 - RubyClay HView Answer on Stackoverflow
Solution 13 - RubyEric HaynesView Answer on Stackoverflow
Solution 14 - RubyTheFedView Answer on Stackoverflow
Solution 15 - RubyLuckyView Answer on Stackoverflow
Solution 16 - RubyavinashbotView Answer on Stackoverflow
Solution 17 - RubysaadibabarView Answer on Stackoverflow
Solution 18 - RubyMarcosView Answer on Stackoverflow
Solution 19 - Rubyaccident-proneView Answer on Stackoverflow
Solution 20 - RubyorzFlyView Answer on Stackoverflow
Solution 21 - RubyJesperEView Answer on Stackoverflow