Question

I've tried searching for this answer, and either don't have the right word combination, or simply can't find, but I apologize if it's a repeat:

When in a python interpreter (running python[3] from the command line, i.e. not IPython or anything), how do I get the interpreter to "page" my tab completions when there are too many to fit on one screen?

For example, if I

import os
os.<tab>

on some computers, it will fill the screen with columnar output of all os.* options, and the bottom line is "More" (as if I've run 'more' or 'less' on the output, so to speak), and I page through with Enter or the space bar. On my current OS, though, it just spits out all the possibilities, which requires me to scroll up to see everything.

Is there a simple function that I should have included in, say, my .pythonstartup that would alleviate this? All I have in there now is:

import readline
readline.parse_and_bind("tab: complete")

which obviously isn't enough to get what I want; I get tab completion, but not paged output.

Was it helpful?

Solution

use readline.set_completion_display_matches_hook to set the display function.

Here's a quick-and-dirty example that just pipes all matches through column to format them in columns and uses less to display.

import readline
import subprocess
import rlcompleter

def display_matches(substitutions, matches, longest_match_length):
    m = '\n'.join(matches) + '\n'
    proc = subprocess.Popen('column | less', shell=True, stdin=subprocess.PIPE)
    # python2:
    proc.communicate(m)
    # python3:
    # proc.communicate(m.encode('utf-8'))


readline.set_completion_display_matches_hook(display_matches)
readline.parse_and_bind('tab: complete')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top