Created
March 30, 2011 03:11
-
-
Save rgov/893792 to your computer and use it in GitHub Desktop.
startup script for Python to enable tab completion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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