0

I have been trying to find a documentation explaining the use of escape sequences but have not been successful. For instance, I know that I can use

printf("%c[%d;%df",0x1B, y, x);

for placing the cursor to a certain position in the console. But where I would find an explanation for this and other escape sequences. As said, I've been looking through the internet, there are a lot of articles explaining that you can escape sequences for different things but not found one with a list of available functions.

It would be great if anyonw knew where I can find this. Thanks for all answers!

Update after some answer:

  • I am aware of the wikipedia page. It e.g. mentions the above possibility but not really explained in the table of CSIs. What I am looking for is something like

ESC[<l>;<c>f => move cursor to line "l" and column "c"
ESC[<l>;<c>H => move cursor to line "l" and column "c"

and explanation of other ESC...

  • I am not looking for formatting possibilities of printf (but thanks anyway for all answers)
Bert
  • 57
  • 6
  • This is what I get when I google `printf "%c"`: https://www.cplusplus.com/reference/cstdio/printf/ Is it helpful? – Thomas Aug 01 '21 at 09:47
  • wiki is quite good https://en.wikipedia.org/wiki/ANSI_escape_code – KamilCuk Aug 01 '21 at 09:48
  • 1
    Dont forget to flush! (stdout is line-buffered) – wildplasser Aug 01 '21 at 09:51
  • I don't understand your update. You seems to be saying that the Wikipedia page does not provide that information. It does; perhaps the issue is rather one of understanding the documentation and how to apply it? [This](https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797) presents more-or less the same information in a different presentation that you may find easier to follow. But no doubt you will have come across that already by Googling "ANSI Escape Sequences" – Clifford Aug 01 '21 at 10:37
  • In any case the question is off-topic for SO; asking for off-site resources and documentation rather then answers to specific questions is off-topic. It encourages link-only answers. – Clifford Aug 01 '21 at 10:37
  • Clifford: Thank you very much for this information. There is some very useful information in this post! – Bert Aug 01 '21 at 10:53
  • @Clifford: A request for an explanation of positioning codes and other ANSI escape sequences is not a duplicate of a request for color codes. – Eric Postpischil Aug 01 '21 at 12:02
  • @EricPostpischil On reflection that is a fair point - that question is specific to colour control. – Clifford Aug 01 '21 at 12:15
  • @EricPostpischil: Correct, but the answer to the "duplicate" also includes the information about positioning (at the bottom of the long answer by Richard). So it really helped. – Bert Aug 01 '21 at 12:26
  • @stark: Yes, see comments above – Bert Aug 01 '21 at 12:44
  • @Bert: Nonetheless, it is not a duplicate and should not be marked as a duplicate. People asking about question X in the future ought to be able to find questions about X and read their answers. They should not have to guess that some question about Y may happen to contain an answer for X in one of its lower-ranked answers. – Eric Postpischil Aug 01 '21 at 13:50
  • @EricPostpischil: Yes, agree! – Bert Aug 01 '21 at 14:13
  • @EricPostpischil : Me too! ;-) I already conceded that point. I found the _answer_ form a Google search and did not consider the context. It was that easy I am unclear why Bert had so much trouble finding suitable resource material. – Clifford Aug 01 '21 at 16:39

2 Answers2

4

where I would find an explanation for this and other escape sequences

Wikipedia has a quite extensive list https://en.wikipedia.org/wiki/ANSI_escape_code . The standard is ECMA-48 (and it's horrible to read), but it's old, and I think there are some new escape sequences "in the wild".

but not found one with a list of available functions.

There is no list, or the closest you can get is ECMA-48. Each and every terminal (well, nowadays, terminal emulators) has different support for ANSI escape sequences, and this list is not strict, developers add support for new escape sequences, and terminals sometimes support their own escape sequences. There are endless terminals and emulators and versions of them. The terminfo database was created to deal with compatibility issues between ANSI escape codes between terminals.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • Note: a source of possible confusion is that `ansi` is also a terminal type, more or less the common denomitor/ smallest usable subset. `ansi` (and: `xterm`, `linux`) are the longest surviving terminal types. In termcap/terminfo, `ansi` also serves as a base-type for other *derived* types. – wildplasser Aug 01 '21 at 10:35
  • Thanks for this. The ECMA-48 is what I would be looking for. But, as you say, it's really very difficult to read ... – Bert Aug 01 '21 at 10:35
0

The escape sequences are different for each terminal type as a general rule. In the past, each terminal brand used (and published) their own set of escape sequences and they were in generla incompatible.

With time, DEC (Digital Equipment Corporation) imposed their set for several reasons:

  • Their terminals where the most extended and popular ones (vt100, vt200, vt220, vt420, etc.)
  • All their models shared the same specification.
  • PDP-11 and later the VAX mainly where sold with these terminals.

For these reasons, the escape sequences of DEC terminals became an standard and slowly all the software adapted to them.

At the same time, some software tools started to use full screen applications, and addressed the problem of using different terminals. This resulted in the unix environments in a library (curses) that allowed the user to have almost any terminal type with addressable cursor and display features to be possible to use with almost any application. Curses was written to support vi(1) but later, it has been successfuly used in many other programs.

Escape sequences became standarized, and the standard (ANSI X3.64 (ISO 6429)) became a de-facto standard in almost any application that was not designed using the curses library. This standard covers just a subset of the full set of escapes that DEC terminals implement (mainly because the sequences to multiplex several sessions in the same terminal is a patented ---and not published--- set of commands, protected by copyright rules).

ECMA has also standarized escape sequences, as answered in another answer to this question.

But, if you actually want to be completely terminal agnostic, you had better to use some curses-like (e.g. ncurses, which is also opensource) library in order to cope with the large database of terminals that have different and incompatible escape sequences. For example, Hewlett Packard terminals have a completely different language for expressing escape codes, and so, escape sequences for HP terminals are completely different than the ones from DEC. Look at ANSI wikipedia page for a medium to full list of these escapes, and for other links related to documentation of these escapes.

Luis Colorado
  • 10,974
  • 1
  • 16
  • 31