Alternative Python bindings for GNU Readline
The GNU Readline Library is the canonical implementation of command line editing, tab completion, and history for console-based applications. It is developed as part of Bash and available on virtually any platform.
While Python ships with readline bindings in its standard library, they only implement a subset of readline’s features, just enough to perform identifier completion at the Python interpreter prompt.
The rl package aims to provide full implementations of GNU Readline’s Custom Completer and History interfaces. It also contains high-level APIs to better organize the namespace and shield applications from low-level verbosity.
rl exports these components:
For further details please refer to the API Documentation.
The code below implements system command completion similar to Bash:
import os from rl import completer from rl import generator def complete_command(text): # Return executables matching 'text' for dir in os.environ.get('PATH').split(':'): dir = os.path.expanduser(dir) if os.path.isdir(dir): for name in os.listdir(dir): if name.startswith(text): if os.access(os.path.join(dir, name), os.R_OK|os.X_OK): yield name def main(): # Set the completion function completer.completer = generator(complete_command) # Enable TAB completion completer.parse_and_bind('TAB: complete') command = raw_input('command> ') print 'You typed:', command
rl requires Python 2.5 or higher and GNU Readline 5.0 or higher.
On Linux, install libreadline6-dev before attempting to build rl. On Mac OS X, make sure you have Xcode Tools installed. Then type:
and watch the console. When it reads:
Finished processing dependencies for rl
you are done and rl is ready to use.
Normally, rl.readline will attempt to link against your system’s libreadline. If this is not possible, notably on Mac OS X which ships with libedit, a static version of GNU Readline 6.2 is built.
To force a static build, set the RL_BUILD_STATIC_READLINE environment variable.