How do you clear the IRB console screen?
21 Answers
On Mac OS X or Linux you can use Ctrl + L to clear the IRB screen.

- 113,588
- 46
- 195
- 237
-
11`Ctrl+L` also works in gnome-terminal, but something more programmatic is `system 'clear'` – vol7ron Jun 06 '13 at 15:54
-
With zsh ctrl + L doesn't work, ctrl + K does. (Oh My ZSH to be specific) – SidOfc Jul 20 '15 at 14:33
-
@SidneyLiebrand I tested on Oh My ZSH and only Ctrl + L worked – pluralism Mar 18 '16 at 00:50
-
2@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. – Fan Yer May 19 '17 at 09:06
-
1I can confirm that this works on Windows 10 as well, both on the Command Prompt and the [WSL](https://learn.microsoft.com/en-us/windows/wsl/about) (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. – ADTC Jun 08 '21 at 02:08
Throw this inside %userprofile%\.irbrc
and you're good
def cls
system('cls')
end

- 102,129
- 8
- 104
- 120
-
14
-
9`system('clear')` will also work on a Mac. It should be noted that this will leave `=> true` at the top of the console. – Michael Dorst Mar 26 '13 at 05:49
-
1@anthropomorphic `system('clear') will work on almost every Unix/Unix-like system. – enedil Jul 29 '14 at 13:47
-
@enedil That's absolutely true, however many people don't know that OS X is a Unix-like system. – Michael Dorst Jul 30 '14 at 00:54
-
@anthropomorphic because it is **not**. OS X is certified Unix by Open Group. – enedil Jul 30 '14 at 10:02
-
@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. – Rob Truxal Dec 10 '17 at 01:55
-
@BenHoffstein You should put that clarification in the body of your post. And maybe note that for Mac OS its `system('clear')` – Jemar Jones Dec 07 '18 at 20:08
On *nix boxes
`clear`
on Windows
system 'cls' # works
`cls` # does not work
on OSX
system 'clear' # works
`clear` # does not work

- 34,686
- 15
- 91
- 152
-
3yeah this doesn't work. Have to do `system('clear')` or `Ctrl` + `L` – Connor Leech Feb 25 '14 at 11:01
-
-
1You can add an alias such as `clear` you your pryrc file for this. Thanks for sharing – James Klein Dec 18 '17 at 15:29
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 >

- 217,595
- 99
- 455
- 496

- 189
- 1
- 4
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

- 3
- 3

- 1,173
- 15
- 19
-
I just tried this using raw irb under Ruby 2.0.0p481 on Windows and it doesn't work. – John Topley Jul 17 '14 at 13:00
-
Yes. Seems it does not work in windows. But it surely does work in mac & Linux. – Chandresh Pant Jul 18 '14 at 08:06
-
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. – Eric Boehs Jul 22 '14 at 19:58
-
I wonder which command would work with tmux. I think Ctrl + L is the one as thatway_3 says down below. – Pablo Feb 16 '20 at 18:35
In order to clear the screen just do:
puts "\e[H\e[2J"
P.S. This was tested on Linux.

- 158,662
- 42
- 215
- 303

- 137
- 1
- 2
-
1This is just the output from `\`clear\``, and is equivalent to `puts %x(/usr/bin/clear)`. – Todd A. Jacobs Oct 19 '13 at 14:56
On Linux Mint 17 also you can use Ctrl + Shift + L
or
Ctrl + L to clear the IRB screen.

- 404
- 1
- 10
- 22
-
-
@thatway_3 - nice answer: how did you print the keyboard instructions on Stackoverflow? – BenKoshy Mar 09 '16 at 12:51
puts `clear`
Clears the screen and then returns => nil
Tested on Mac OSX 10.6 Terminal and iTerm2.

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

- 376
- 4
- 17
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.

- 5,126
- 2
- 29
- 36
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.

- 31
- 2
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

- 16,787
- 19
- 117
- 151
I've used this for executable files:
def clear
system("cls") || system("clear") || puts("\e[H\e[2J")
end
clear

- 1,177
- 1
- 11
- 25
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!

- 4,796
- 5
- 40
- 64
-
-
@the Tin Man - backticks don't always operate how you think, but ``reset`` should work fine – vol7ron Jun 06 '13 at 15:57
-
Backticks always work how I expect but then, I've been using them in various languages for years and years. – the Tin Man Jun 06 '13 at 16:17
-
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. – Marcos Jul 24 '14 at 08:30
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.

- 158,662
- 42
- 215
- 303
->(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 ;)

- 166
- 1
- 7
The backtick operator captures the output of the command and returns it
s = `cls`
puts s
would work better, I guess.

- 63,317
- 21
- 138
- 197
-
1
-
@OrionEdwards @JesperE the first line showed me `"\f"` which is what you got and then `puts s` outputs this `♀` I wonder why? – Lucky Sep 29 '14 at 11:59