How to Clear the Terminal in Ruby

How Do You Clear The IRB Console?

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

Clear the terminal in rails server

In gnome terminal: Terminal -> Reset and Clear.

Sample Image

I mapped it to CTRL+k.

+K for OSX

Completely clear the console in RubyMine (or WebStorm or PhpStorm)

Right click and select Clear buffer or just press Ctrl + K when the console is focused.

Deleting multiple lines of terminal output using ruby

Well, for simple things, if you can assume an ANSI-compatible terminal (usually a good bet), you can just directly output ANSI codes. For instance,

 5.times do |item|
print "\r" + ("\e[A\e[K"*3) if item > 0
puts "#{item+1}\nHello!"
end

Where \r moves the cursor to the start of the line, \e[A moves the cursor up one line, and \e[K clears from the cursor position to the end of the line. If you don't need anything further down the screen, you can also just send \e[J once you have the cursor where you want; that clears all the way to the end of the screen.

For more sophisticated stuff, you could start by taking a look at the Highline gem.

Clear screen before any output to console

Well, the requirements are not clear and I won’t recommend this way, but the stated problem might be solved with:

$stdout.singleton_class.prepend(Module.new do
def write(string)
super("\e[2J" << string)
end
end)

I think, this answer is not suitable for the OP, but it perfectly answers the exact question asked.



Related Topics



Leave a reply



Submit