Search code examples
pythonpython-2.7libreadline

How to stop autocomplete and have normal tab with GNU readline library in Python


Right now, I'm running the following code in Python 2.7:

import readline as rl
rl.parse_and_bind('set editing-mode vi') #allow for arrow keys to be used
rl.set_completer()
raw_input()

According to this, rl.set_completer() should remove the completer function, which I assumed would make tab work normally. But, the tab key just doesn't work at all.

I've also tried writing my own function and passing it in as a completer, but that didn't work either. (If someone could find a way of doing this that would make the tab key work normally, that would also suffice.)

How do I get the ability to use arrow keys with raw_input, but also have a normal tab?


Solution

  • You can use

    #allow for arrow keys to be used for raw_input.
    readline.parse_and_bind('set editing-mode vi') 
    
    #set the tab key to make 4 spaces
    readline.parse_and_bind("TAB: '    '")
    

    For some reason, using readline.parse_and_bind("TAB: '\t'") caused Python to use way too much of the CPU and would just freeze the screen, so I had to switch it to use spaces.