0

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?

0

2 Answers 2

0

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.

0
rl.set_completer()

Removes completer function, but not the TAB binding. So tab is eaten, but nothing is done as the tab is passed to a "None function".

readline.parse_and_bind("TAB: '\t'")

results in infinite calls to the underlying completer function.

Read TAB -> Return TAB -> Read TAB …

Simplest method, at least when testing here, is to use the default emacs mode. If one only import readline without setting anything, read_raw works for arrow keys, and TAB result in TAB.

If that is not an option set a custom completer function. (Works the same for Python 3). This is also very likely the safer option.

If one press TAB the completer function is called. For one TAB key press the state argument starts at 0 and increment for each time function is called. As we return TAB it is important that we check that state is 0. Else it will be called again and again.

We also have to return the text, if any, else it is eaten by readline.

Simple example:

#!/usr/bin/env python2.7

import readline

def rl_tab_expander():
    def completer(txt, state):
        if state == 0:
            return txt + '\t'

    return completer

readline.set_completer(rl_tab_expander())
readline.parse_and_bind('set editing-mode vi')

while 1:
    inp = raw_input("$ ")
    if inp.strip() in ('exit', 'q', 'quit'):
        break

Check on original test

You could use a log file. Here we look at what happens in the completer function if we return TAB or if we do not check state as per example above. We set a limit for state at 25, else it would continue ad infinity.

Run script in one console window and do a tail -f /tmp/test-readline.log in another.

#!/usr/bin/env python2.7

import readline
import logging

logging.basicConfig(filename = '/tmp/test-readline.log', level = logging.DEBUG)

def rl_tab_expander():
    def completer(txt, state):
        logging.debug("STATE: %2d TXT: %s", state, repr(txt))
        if state > 24:
            return None
        return txt + '\t'

    return completer

readline.set_completer(rl_tab_expander())
readline.parse_and_bind('set editing-mode vi')

while 1:
    inp = raw_input("$ ")
    if inp.strip() in ('exit', 'q', 'quit'):
        break

Result

DEBUG:root:STATE:         0 TXT: ''
DEBUG:root:STATE:         1 TXT: ''
DEBUG:root:STATE:         2 TXT: ''
...
DEBUG:root:STATE:        24 TXT: ''
DEBUG:root:STATE:        25 TXT: ''

Without limiting state level this would have continued until program was killed or if we press Ctrl+C and get lucky.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.