10

I'm making my first Lua program with the console, and I need to find out how to remove all the text in the console without just appending a ton of \n's.

0

4 Answers 4

11

You can use os.execute().

On Windows:

os.execute("cls")

On Unix:

os.execute("clear")
6

If your console understands ANSI terminal escape sequences, try io.write("\027[H\027[2J").

3

As mentioned in the other reply, you can use os.execute() to clear the console. However, if you do not have access to this function, then you might be forced to spam the console with new lines, so it seems "empty" to the user.

However, if you are able to use os.execute, then you should definitely use it.

for i = 1, 255 do
    print()
end
2
  • Clever, very clever.
    – theFish
    Dec 29, 2016 at 21:46
  • it spam \n's when OP didn't ask something that spam \n's
    – Fox
    Nov 27, 2020 at 2:14
2

Sorry for the late answer, but if anyone check this post here's a way to do it :

if not os.execute("clear") then
    os.execute("cls")
elseif not os.execute("cls") then
    for i = 1,25 do
        print("\n\n")
    end
end

This will work on any os.

2
  • I think it will never run elseif branch. Aug 28, 2019 at 13:48
  • also the code work incorrectly it spam new lines when i have 'clear' command you havent test it right on multiple OSes?
    – Fox
    Nov 27, 2020 at 2:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.