Skip to content

Instantly share code, notes, and snippets.

@rgov
Created March 30, 2011 03:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rgov/893792 to your computer and use it in GitHub Desktop.
Save rgov/893792 to your computer and use it in GitHub Desktop.
startup script for Python to enable tab completion
from sys import stdout, stderr
# Enable tab completion if readline is available. Rather than using the
# default completer, though, we allow tabs at the beginning of the line.
try:
import readline
readline.is_libedit = 'libedit' in readline.__doc__
except ImportError:
pass
else:
from rlcompleter import Completer
class RZGCompleter(Completer):
def complete(self, text, state):
if readline.get_line_buffer().lstrip() == '':
readline.insert_text('\t')
if readline.is_libedit: stdout.write('\t')
readline.redisplay()
return None
else:
return Completer.complete(self, text, state)
readline.set_completer(RZGCompleter().complete)
if readline.is_libedit:
# The OS X readline module wraps libedit. Unfortunately, it does so
# poorly: tab completion only works at the beginning of the line. You
# can install a working readline module with `easy_install readline`.
print >> stderr, 'Note: Tab completion is borked with libedit.'
readline.parse_and_bind('bind ^I rl_complete')
else:
readline.parse_and_bind('tab: complete')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment