0

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.

dwanderson
  • 2,775
  • 2
  • 25
  • 40

1 Answers1

3

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')
mata
  • 67,110
  • 10
  • 163
  • 162
  • Hmm, I'm having some trouble getting that to work - now, it just provides me an empty screen (that I need to 'q' to get out of, so I think the 'less' part is working?)... – dwanderson Aug 22 '13 at 01:05
  • Try without the `column`. which python version are you using? – mata Aug 22 '13 at 01:13
  • it was the python3 issue; it works now even with column. Thanks very much, this was one of those little things that was driving me crazy. – dwanderson Aug 23 '13 at 00:40