问题
After typing "vi [FILENAME]" in the bash I get the following result:
....\r\"gu1.txt\" 16L, 87C
ESC[>cESC[1;1H
111\r\n1112
.....
The first text marked in the this row is the file name following the number of lines and columns of the vi result.
The second text includes two escape sequences which I don't understand their meaning : ESC[>c
and ESC[1;1H
.
The last text includes the text shown as a result of the vi, i.e. the content of the file.
Can you please explain what is the ANSI escape code sequence ESC[>c
and how it is related to the escape code sequence which comes right after it ESC[1;1H
?
Thanks in advance
回答1:
I believe the ANSI standard isn't available publicly, but a quick Google turned up a third-party summary based on the DEC PDP-10 docs.
For the first one: The 'c'
command means "DA" for "Device Attributes". You send a CSI 'c'
with no arguments to the terminal, and it sends back a CSI 'c'
with private mode characters that mean something terminal-specific. Private mode characters must start with one of "<=>?"
, and run up to a digit, trailing character, or letter. So, ESC[>c
is a minimal response that somehow encodes 2 bits of terminal-specific information (by choosing >
over one of the other 3 choices). Note that ESC[>c
is equivalent to ESC[>0c
, which is how DEC VT240 terminals distinguished themselves from VT220, so this may mean that your text file was captured from output intended for a VT240 emulator. But it could mean all kinds of other things. At any rate, sending that back to the terminal should have no effect.
The second one is a lot easier. The 'H'
command means "CUP", for "CUrsor Position": given arguments n
and m
, it moves the cursor to 1-based row n
and column m
from the top left. So, ESC[1;1H
moves the cursor to the top-left corner of the screen.
回答2:
It is "Send Device Attributes (Secondary DA)", according to XTerm Control Sequences:
CSI > Ps c
Send Device Attributes (Secondary DA). Ps = 0 or omitted -> request the terminal's identification code. The response depends on the decTerminalID resource setting. It should apply only to VT220 and up, but xterm extends this to VT100.
The control (applied to VT220, etc.) is documented in DEC manuals on vt100.net
The point of Send Device Attributes is that a program is asking the terminal something. The terminal sends back information, the response. This happens to be a string that looks something like the request (which is helpful if the terminal is not really connected to a host). The program which sent the request has to read the response, otherwise you will see odd characters printed on the terminal—the unread parts of the response.
In the context of your question about vi, vim uses this to determine the version of xterm which it may be using. The answer is provided in the response from the terminal:
and Pv is the firmware version (for xterm, this was originally the XFree86 patch number, starting with 95). In a DEC terminal, Pc indicates the ROM cartridge registration number and is always zero.
The particular feature in vim which is using this is called "termresponse". If you use it on a terminal which pretends to be xterm, but is not really, then you will see stray characters on your terminal, as described in an answer for Why is vim starting in delete mode?. Any terminal emulator which is "VT100 compatible" must handle this, however.
The following ESC[1;1H
is not related to the Secondary DA, but rather is standard (ISO-6429 / ECMA-48) cursor-positioning done for initialization (and you would see something like this in any text editor running in a terminal). Usually it is sent in a shorter form, ESC[H
, since the numbers default to 1
.
The ESC[>c
is a private mode sequence, as indicated by the >
character. That (and many other details) are described in ECMA-48. DEC's terminals followed the standard, along with extensions. The "ANSI" standard for terminal controls was long ago subsumed into the ISO standard, and (still) long ago the corresponding ANSI standard was withdrawn.
You would be unlikely to encounter this sequence used in communicating with a hardware terminal, since it was (before xterm began using it in the 1990s) predominantly used by DEC's (Open)VMS systems to determine what types of terminals were connected to it.
来源:https://stackoverflow.com/questions/29939026/what-is-the-ansi-escape-code-sequence-escc