Search code examples
pythonbackspace

^H ^? in python


Some terminals will send ^? as backspace, some other terminals will send ^H. Most of the terminals can be configured to change their behavior. I do not want to deal with all the possible combinations but I would like to accept both ^? and ^H as a backspace from python.

doing this

os.system("stty erase '^?'")

I will accept the first option and with

os.system("stty erase '^H'")

I will accept the second one but the first will be no longer available. I would like to use

raw_input("userinput>>")

to grab the input.

The only way I was able to figure out is implementing my own shell which works not on "raw based input" but on "char based input".

Any better (and quicker) idea?


Solution

  • The built-in function raw_input() (or input() in Python 3) will automatically use the readline library after importing it. This gives you a nice and full-feautured line editor, and it is probably your best bet on platforms where it is available, as long as you don't mind Readline having a contagious licence (GPL).