Skip to main content

Difference Between the clear Command in Linux and Mac

I've been writing a series of posts about CSI Sequences, but we rarely use CSI Sequences directly. However, there is a CSI Sequence that we use unknowingly. It's the clear command that clears the screen.

The clear command basically uses two types of CSI sequences. One is CSI H(Cursor Position, a.k.a CUP); it moves the cursor to the beginning of the screen. The cursor is at the top-left corner after the command ends, thanks to CUP. The second CSI Sequence is CSI 2 J(Erase in Display, a.k.a. ED), which is used to clear the entire screen. Linux and Mac use these two sequences; they behave the same way up to this point. However, Linux's clear and Mac's differ in their subsequent actions.

In a nutshell, Linux's clear clears the scrollback buffer, while Mac's does not. Linux's one prints CSI 3 J after the two sequences. CSI 3 J is an extension of the Escape Sequence introduced by xterm that removes lines stored in the scrollback buffer. Since being named E3 by the Red Hat community in 2011, terminal emulators usually call it the E3 extension. Mac's clear does not print this sequence, leaving the scrollback buffer untouched.

This difference is not as old as one might think. Originally, most terminal emulators did not understand xterm extensions, including the E3 extension. Naturally, the clear command used in Linux also did not use E3, so the scrollback buffer was not cleared. However, as time passed, xterm extensions gradually spread to other terminal emulators, and the E3 extension began to be implemented. From 2007's PuTTY, 2011's Red Hat, 2014's Gnome Terminal, and iTerm2, various terminal emulators started to support CSI 3 J, and today, most terminals support the E3 extension.

Another issue arose as most terminal emulators began to support the E3 extension. The question was how to define the behavior of the clear command. Opinions were divided between two groups of people. One group wanted the clear command to only clear the visible display, insisting that deleting the scrollback buffer is the task of the reset command, which literally resets the terminal. The other group believed that the scrolling area is also part of the screen, clear should delete the scrollback buffer as well. The latter won in the Linux community. Eventually, E3 was added to the clear command, and those who disliked this decision had to use the clear command with a -x option like clear -x.

However, this change has not yet been propagated to the Mac community. Therefore, Mac uses the program implemented before E3 was added. There isn't even an option to choose. It seems that there is no discussion related to this issue. So, if you want Linux-like clear behavior on Mac, you need to create an alias like alias clear='printf "\033[H\033[2J\033[3J"'. Conversely, if you want the original behavior on Linux, you must set an alias like alias clear=clear -x or alias clear='printf "\033[H\033[2J"'.


Popular posts from this blog

Type Conversion in Rust

Type conversion is not special in Rust. It's just a function that takes ownership of the value and returns the other type. So you can name convert functions anything. However, it's a convention to use as_ , to_ , and into_ prefixed name or to use from_ prefixed constructor. From You can create any function for type conversion. However, if you want to provide generic interfaces, you'd better implement the From trait. For instance, you should implement From<X> for Y when you want the interface that converts the X type value to the Y type value. The From trait have an associated function named from . You can call this function like From::from(x) . You also can call it like Y::from(x) if the compiler cannot infer the type of the destination type. Into From have an associated function, it makes you be able to specify the destination type. It's why From has an associated function instead of a method, but on the other hands, you cannot use it as a me

Do not use garbage collection to catch memory leak

Garbage collection is a technique that automatically releases unnecessary memory. It's very famous because many programming languages adopted garbage collection after John McCarthy implemented it in Lisp. However, there are a few people who misunderstand what garbage collection does. If you think garbage collection prevents a memory leak, unfortunately, you are one of them. Garbage collection cannot prevent a memory leak. There is no way to avoid all memory leaks if you are using Turing-complete language. To understand it you should know what a memory leak is. Wikipedia describes a memory leak as the following: a type of resource leak that occurs when a computer program incorrectly manages memory allocations in such a way that memory which is no longer needed is not released. Briefly, a memory leak is a bug that doesn't release a memory that you don't use. So it is first to find the memory which will not be used in order to detect memory leaks. Unfortunately, it i

[C++] Handling Exceptions in Constructors

When you use RAII idiom, there are often situations where constructors have to do complex tasks. These complex tasks can sometimes fail, resulting in throwing exceptions. This raises a concern: Is it okay to throw exceptions in constructors? The first concern is memory leaks. Fortunately, memory leaks do not occur. Variables created on the stack are released through stack unwinding, and if an exception occurs during heap allocation with the new operator, the new operator automatically deallocates the memory and returns nullptr . The next concern is whether the destructor of the member variables will be called correctly. However, this is also not a problem. When an exception occurs, member variables can be divided into three categories: fully initialized member variables, member variables being initialized, and uninitialized member variables. Fully initialized member variables have had their constructors called and memory allocations completed successfully. In the example code, t