0

What does this command? I know that, the CSI n ; m H is for move the cursor to n row and m column, but what does command from title? ^[[H^[[2J ?

2

That's a visual representation (where ^[ represents the ESC character) of the sequence to clear the screen and bring the cursor to the top in xterm-like terminals at least:

$ TERM=xterm tput clear | cat -v
^[[H^[[2J

To find out about those escape sequences, look at the ctlseqs.txt document shipped with xterm. There, you'll find:

ESC [
Control Sequence Introducer (CSI is 0x9b).

and:

CSI Ps ; Ps H
Cursor Position [row;column] (default = [1,1]) (CUP).

and:

CSI Ps J Erase in Display (ED).
Ps = 0 -> Erase Below (default).
Ps = 1 -> Erase Above.
Ps = 2 -> Erase All.
Ps = 3 -> Erase Saved Lines (xterm).

(note that ^[[2J doesn't clear the saved lines or alternate screen).

tput clear (or clear) on xterm-like terminals does the same as printf '\e[H\e[2J'. For that it queries the terminfo database to know what the sequence of character is for the clear capability for the terminal whose name is stored in the $TERM environment variable. If you dump the entry for the xterm terminal in the terminfo database with infocmp xterm, you'll see in it:

$ infocmp -1 xterm | grep clear
        clear=\E[H\E[2J,

Which is another way to find out about a given escape sequence:

$ infocmp -L -1 | grep J
        clear_screen=\E[H\E[2J,
        clr_eos=\E[J,

(here using the Long name for the capabilities). Then, you can do man 5 terminfo for a description of those capabilities.

1

The values n and m default to 1 in CSIn;mH, thus ^[[H moves the cursor to the home position. ^[[2J clears the screen, leaving the cursor where it is.

  • Not exactly; ^[[2J clears the screen but does not move the cursor. – egmont Oct 24 '17 at 21:31
  • @egmont You are correct. I edited the answer. – Johan Myréen Oct 25 '17 at 4:38

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.