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?
-
1How about `system("clear")` or similar (e.g. with `popen`)? Alternatively, simulate the pressing of Ctrl-L with AppleScript (or `osascript` through the command line) – Arc676 Oct 31 '15 at 05:23
-
1How about http://linux.die.net/man/3/curs_clear ? – jtbandes Oct 31 '15 at 05:28
-
@Arc676 Yes, this is exactly what I was looking for, thank you. Out of curiosity, where did you learn of this command? – Brandon Bradley Oct 31 '15 at 05:29
-
Which command? `osascript`? – Arc676 Oct 31 '15 at 05:31
-
Better than hard-coding - [Ncurses with Swift on Linux](http://dev.iachieved.it/iachievedit/ncurses-with-swift-on-linux/). – Thomas Dickey Apr 18 '16 at 21:35
5 Answers
You can use the following ANSI sequence:
print("\u{001B}[2J")
... where \u{001B}
is ESCAPE
and [2J
is clear screen.

- 31,030
- 13
- 103
- 118
-
Only answer that worked for me. MacBook Pro (Retina, 15-inch, Mid 2014) OS X El Capitan. – Mark Oct 01 '16 at 21:25
-
This works because `system` is unavailable in Swift 3. I'll see what I can do to update my answer to use `posix_spawn` APIs or `NSTask`. – JAL Nov 29 '16 at 16:48
-
8
-
3@BenOng I've discovered that this does work in a standard terminal window, but prints "[2J" if you are using the console built into Xcode. The Xcode terminal is also unable to print colors properly, so I'm guessing it's just a general ANSI interpretation problem when running your console app from the IDE. Try archiving your build and running it standalone. – bitwit Feb 16 '19 at 20:55
-
1
-
Any idea how to go to first line as well? this clears the screen but new prints appear in the middle of terminal while the top is empty – Swiftly Mar 31 '23 at 01:18
This answer applies to Swift 2.1 or earlier only
To elaborate on Arc676's answer:
The system
command is imported into Swift via the Darwin module on Mac platforms (with other C APIs). On Linux, Glibc replaces Darwin for bridging low-level C APIs to Swift.
import Glibc
// ...
system("clear")
Or, if the system
call is ambiguous, explicitly call Glibc's system
(or Darwin on Mac platforms):
import Glibc
// ...
Glibc.system("clear")
-
3With Swift 3 I am getting `"system" is unavailable in Swift: Use posix_spawn APIs or NSTask instead` – Chris Apr 27 '17 at 18:32
This code makes a synchronous call to the built-in clear
command. This won't cause problem with readLine()
since it prints the escape sequence returned by clear
using Swift's print()
function
var cls = Process()
var out = Pipe()
cls.launchPath = "/usr/bin/clear"
cls.standardOutput = out
cls.launch()
cls.waitUntilExit()
print (String(data: out.fileHandleForReading.readDataToEndOfFile(), encoding: String.Encoding.utf8) ?? "")

- 133
- 1
- 3
- 13
-
Just to be clear, this works in terminal. But if you try it to clear console screen inside Xcode, it gives `TERM environment variable not set.` – Anton Malmygin Nov 07 '18 at 15:54
Use the built-in clear
command either with system
system("clear")
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}'
EDIT: system
is no longer available in newer verions of Swift. See Rudolf Adamkovič's answer.

- 4,445
- 3
- 28
- 44
This works for me in Swift 3.1
var clearScreen = Process()
clearScreen.launchPath = "/usr/bin/clear"
clearScreen.arguments = []
clearScreen.launch()
clearScreen.waitUntilExit()
You can create a function with a callback like this
func clearScreen(completion:@escaping (Bool) -> () ) {
let clearScreen = Process()
clearScreen.launchPath = "/usr/bin/clear"
clearScreen.arguments = []
clearScreen.terminationHandler = { task in completion(true) }
clearScreen.launch()
clearScreen.waitUntilExit()
}

- 133
- 1
- 3
- 13

- 5,485
- 15
- 68
- 130