Today, following my previous post, I will explain how to clear the screen using CSI Sequences. There are two sequences in the CSI Sequence for clearing.
The first one is the Erase in Line sequence, called EL. It is composed of CSI # K
; it is used to erase lines, as the name suggests. If the #
is not provided, the default value is 0, and if a value is provided, it must be one of the three: 0, 1, or 2. The terminal will ignore the sequence if any other value is provided. For example, if you print 0x311b5b334b32
(or 1^[3K2
), the terminal ignores ^[3K
, and the screen displays only 12
. The behavior of 0, 1, and 2 can be summarized as follows.
- 0
- Erases from the cursor to the end of the line.
- 1
- Erases from the beginning of the line to the cursor.
- 2
- Erases the entire line, regardless of the cursor's position.
Remember that the EL sequence does not move the cursor's position. Therefore, if you want to erase the current line and write a new line on the current line, you should use the EL sequence together with a carriage return (\r).
The second is the Erase in Display sequence, called ED. It is composed of CSI # J
and is used to erase the screen. Like EL, the area of the screen to be erased is determined by the value of #
.
- 0
- Erases from the cursor to the end of the screen.
- 1
- Erases from the beginning of the screen to the cursor.
- 2
- Erases the entire screen, regardless of the cursor's position.
- 3
- Erases the lines stored in the scroll buffer.
As you see, it is the same as EL. The default value is 0, not moving the cursor's position, and ignoring values other than the defined values. The only difference is the number 3, which is not defined in EL.
This sequence is used to erase the values stored in the scrollback buffer. It had not existed in the VT100 specification. At that time, terminals did not have the concept of scrolling. This sequence was added after the appearance of the xterm terminal emulator, which came with the X Window System. So, in the past, many terminal emulators did not support it, but nowadays most environments support CSI 3 J
without any problems.
ED and EL are rarely used directly unless you are a fan of a TUI. It is because these sequences are fundamentally for controlling the terminal, and the purpose of most programs we write is not to control the terminal. However, we use ED every day without realizing it. In the next post, we will take a closer look at this topic.
Comments
Post a Comment