The \b
control character, as I understand it, is not supposed to erase the previous character (this would be \b
+ a del character as well), so something like this works:
>>> print 'pototo\b\b\ba'
potato
Is there a character for moving forwards, like a non-overwriting space? Expected usage would be something like (I've called this character \x
):
>>> print 'pototo\r\x\x\xa'
potato
Obviously on a typewriter a normal space would do this just fine. But on a terminal a space erases the letter underneath.
My use case is a pexpect matching kind of scenario where I want to retrospectively go back and decorate certain parts of the output of a character stream with colours, and I'm wondering whether keeping a cache of the whole current line in memory will be necessary or not.
If you can rely on ANSI escape code sequences in your terminal (*), you can use the Cursor Forward (CUF) sequence "CSI n C", like this:
print "Pototo\b\b\ba\x1b[2Ces"
and get:
Potatoes
CSI is \x1b[
, and is used to start ANSI escape code sequences. 2
is the number of characters to move to the right, and C
is the command to move rightwards.
(*) A good approximation is that you can rely on ANSI codes unless you need to support Windows.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With