How Do You Clear The IRB Console?
Asked Answered
D

21

205

How do you clear the IRB console screen?

Dirndl answered 22/9, 2008 at 18:24 Comment(0)
D
287

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

Dirndl answered 22/9, 2008 at 18:24 Comment(5)
Ctrl+L also works in gnome-terminal, but something more programmatic is system 'clear'Bodyguard
With zsh ctrl + L doesn't work, ctrl + K does. (Oh My ZSH to be specific)Chainplate
@SidneyLiebrand I tested on Oh My ZSH and only Ctrl + L workedGradey
@fanaugen On MacOS, cmd+k will clear all within a terminal tap, if you use tmux to split area of the visual area, ctrl+L will do a better work.Cheddite
I can confirm that this works on Windows 10 as well, both on the Command Prompt and the WSL (running Ubuntu). In WSL, it pushes the prompt to the top (history still remains scrollable) but in Command Prompt, it wipes out all the history too.Froehlich
N
62

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

def cls
  system('cls')
end

From IRB clear screen on windows.

Nailbiting answered 22/9, 2008 at 18:25 Comment(7)
I should clarify that this applies to Windows only.Nailbiting
system('clear') will also work on a Mac. It should be noted that this will leave => true at the top of the console.Redivivus
@anthropomorphic `system('clear') will work on almost every Unix/Unix-like system.Quechua
@Quechua That's absolutely true, however many people don't know that OS X is a Unix-like system.Redivivus
@anthropomorphic because it is not. OS X is certified Unix by Open Group.Quechua
@BenHoffstein - I do not care that this comment is going to get deleted THANK YOU DUDE THANK YOU += >9,000. This took entirely too long to find, relative to my normal stackoverflow expurrrrience.Thyrse
@BenHoffstein You should put that clarification in the body of your post. And maybe note that for Mac OS its system('clear')Crompton
A
46

On *nix boxes

`clear`

on Windows

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

on OSX

system 'clear' # works
`clear` # does not work
Armillia answered 22/9, 2008 at 18:27 Comment(3)
yeah this doesn't work. Have to do system('clear') or Ctrl + LMyrticemyrtie
system 'clear' worked for me but I got : command not found => falseStanfordstang
You can add an alias such as clear you your pryrc file for this. Thanks for sharingHarshman
G
25

Command + K in macOS works great.

Gahan answered 1/11, 2012 at 22:49 Comment(0)
C
16

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 > 
Clap answered 6/12, 2011 at 15:55 Comment(0)
A
13

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

Alternative answered 17/7, 2014 at 7:9 Comment(4)
I just tried this using raw irb under Ruby 2.0.0p481 on Windows and it doesn't work.Dirndl
Yes. Seems it does not work in windows. But it surely does work in mac & Linux.Alternative
I like this answer the best. You don't have to modify anything and it's just shelling out. Simple to remember too. Btw, . cls should work on Windows.Viperish
I wonder which command would work with tmux. I think Ctrl + L is the one as thatway_3 says down below.Dardani
T
12

In order to clear the screen just do:

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

P.S. This was tested on Linux.

Th answered 14/2, 2012 at 16:11 Comment(1)
This is just the output from `clear`, and is equivalent to puts %x(/usr/bin/clear).Demy
A
11
system 'clear'

Should work for rails 4.0 as well

Arrhenius answered 20/10, 2015 at 9:54 Comment(0)
O
9

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

or

Ctrl + L to clear the IRB screen.

Osteoporosis answered 21/9, 2015 at 11:52 Comment(2)
Worked for Mac OS X.11.1 in the Terminal appQuicktempered
@Osteoporosis - nice answer: how did you print the keyboard instructions on Stackoverflow?Turnkey
V
6
puts `clear`

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

Vaccaro answered 24/4, 2012 at 3:6 Comment(0)
H
6

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')

Heptagonal answered 22/7, 2016 at 16:29 Comment(0)
S
5

In windows, using Rails 4,

system('cls')

worked for me

Stephen answered 16/1, 2014 at 15:18 Comment(0)
T
4

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.

Tryptophan answered 21/8, 2016 at 1:26 Comment(0)
A
3

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.

Aurify answered 9/7, 2014 at 20:38 Comment(0)
B
3

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
Bigener answered 29/9, 2014 at 12:11 Comment(0)
E
3

I've used this for executable files:

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

clear
Ean answered 20/11, 2014 at 17:18 Comment(0)
S
3
system 'cls' 

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

Staton answered 22/9, 2015 at 14:42 Comment(0)
R
1

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!

Ruse answered 17/10, 2012 at 21:7 Comment(4)
simplify it using back ticks: `reset`.Hendel
@the Tin Man - backticks don't always operate how you think, but reset should work fineBodyguard
Backticks always work how I expect but then, I've been using them in various languages for years and years.Hendel
Indeed, sometimes what resembles a backtick may indeed be some other UTF-8 creature. Occasionally I fix "text" docs with upper and lower quotation marks that are actually != the ANSI/ASCII " character. Like in commercial SRT files, where all hell breaks loose.Ruse
D
0

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.

Duston answered 23/8, 2012 at 21:34 Comment(0)
J
0
->(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 ;)

Jadotville answered 19/10, 2013 at 14:50 Comment(0)
C
-3

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

s = `cls`
puts s

would work better, I guess.

Conduct answered 22/9, 2008 at 19:4 Comment(2)
This fails: You get this irb(main):004:0> cls => "\f"Oxytocic
@OrionEdwards @Conduct the first line showed me "\f" which is what you got and then puts s outputs this I wonder why?Bigener

© 2022 - 2024 — McMap. All rights reserved.