Reputation: 21
I'm using pyserial to create a serial application. I am looking for the ASCII or ANSI escape sequence for CTRL+A and CTRL+E (go to beginning and end of the line, respectively). I cannot seem to find the escape codes anywhere. Does anyone know of a resource that lists the codes? CTRL+A and CTRL+E seem to be pretty universal cursor to beginning of line and cursor to end of line control keys. I have to believe that an escape sequence exists.
Any suggestions would be greatly appreciated!
Upvotes: 2
Views: 3515
Reputation: 365925
Neither ASCII characters nor ANSI control codes really quite define a thing called CTRL+A.
However, the ASCII control characters 0-31 (which officially have names from NUL through US) are traditionally mapped as the "control version" of the printable characters 64-95. So, 0 is ^@, 1 is ^A, 2 is ^B, etc.
The Wikipedia pages on ASCII and control character give further information.
So, ^A is 1, and ^E is 5.
Meanwhile:
CTRL+A and CTRL+E seem to be pretty universal cursor to beginning of line and cursor to end of line control keys.
They're not that universal. These are part of the "basic emacs control keys", which a number of Unix terminal emulators and some Unix-based GUI apps (and things like the libreadline
CLI editing library and the Qt
text widgets) support by convention. However, try hitting ^A in, say, a DOS prompt or Microsoft Word and it won't go to the start of the line.
And, just sending ^A or ^E to an ANSI-compatible terminal won't necessarily move the cursor to the start or end of the line. To do that properly, you want to actually send the corresponding ANSI control sequences. Any list of ANSI escape codes, like Wikipedia's, will show you what's available.
Unfortunately, there is no right answer to what you're trying to do. ANSI treats the entire 80x25 (or whatever) screen as accessible, and doesn't distinguish between character positions that have something in them and those that are blank. So, you can't move to the "end of the line", unless by that you mean "column 79".
And, if what you're looking for is moving to columns 0 and 79, that's easy with the CHA
command ('\x1b[0G'
and '\x1b[79G'
)—but not all ANSI terminals support that; in particular, DOS ANSI.SYS and anything built to be compatible with it will ignore it.
Upvotes: 3