Brandon Bradley Brandon Bradley - 1 year ago 341
Swift Question

How to clear the Terminal screen in Swift?

I am writing a BASIC Interpreter for the Command Line in Swift 2, and I cannot find a way to implement the simple command, CLS (clear all text from the Terminal.) Should I simply print spaces in a loop, or is there a function I'm not aware of that would clear the Terminal screen?

Answer Source

Use the built-in clear command either with system


or popen (ask Google)

Alternatively, simulate the pressing of Ctrl+L using AppleScript via the command line:

osascript -e 'tell app "terminal" to tell app "system events" to keystroke "l" using {control down}'