pyCmdlineHistory

Shield: CC BY-SA 4.0

This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

image1

Save command line history and provide a command line completer for python.

Original code is from ActiveState Code » Recipes 2006.06.29 by Sunjoong LEE. ActiveState content is published under CC BY-SA 3.0.

Usage

Insert the lines

import sys
import subprocess

try:
    import berhoel.cmd_line_history
except ImportError:
    # checking if python is running under virtualenv of venv
    is_venv = (
        # This handles PEP 405 compliant virtual environments.
        (sys.prefix != getattr(sys, "base_prefix", sys.prefix))
        or
        # This handles virtual environments created with pypa's virtualenv.
        hasattr(sys, "real_prefix")
    )

    subprocess.check_call(
        [sys.executable, "-m", "pip", "install"]
        + ([] if is_venv else ["--user"])
        + ["pyCmdlineHistory",]
    )

to “~/.pystartup” file, and set an environment variable to point to it:

export PYTHONSTARTUP=${HOME}/.pystartup

in bash.

This will locally install the module for each python you are calling.

Documentation

Documentation can be found here

References

  • Guido van Rossum. Python Tutorial. Python Software Foundation, 2005. 86

  • Jian Ding Chen. Indentable rlcompleter. Python Cookbook Recipe 496812

  • Guido van Rossum. rlcompleter.py. Python Software Foundation, 2005

2006.06.29 Sunjoong LEE sunjoong@gmail.com 2020 - 2023 Berthold Höllmann berhoel@gmail.com

Example

To show an example, I had input as below:

>>> i⭾
id(          import       input(       is           issubclass(
if           in           int(         isinstance(  iter(
>>> import pwd⮐
>>> print(f"{pwd.⭾
pwd.getpwall()      pwd.getpwuid(
pwd.getpwnam(       pwd.struct_passwd(
>>> print(f"{pwd.__⭾
pwd.__annotations__     pwd.__gt__(             pwd.__package__
pwd.__class__(          pwd.__hash__()          pwd.__reduce__()
pwd.__delattr__(        pwd.__init__(           pwd.__reduce_ex__(
pwd.__dict__            pwd.__init_subclass__(  pwd.__repr__()
pwd.__dir__(            pwd.__le__(             pwd.__setattr__(
pwd.__doc__             pwd.__loader__()        pwd.__sizeof__()
pwd.__eq__(             pwd.__lt__(             pwd.__spec__
pwd.__format__(         pwd.__name__            pwd.__str__()
pwd.__ge__(             pwd.__ne__(             pwd.__subclasshook__(
pwd.__getattribute__(   pwd.__new__(
>>> print(f"{pwd.__do⭾c__}")⮐
...
>>> history⮐
import pwd
print(f"{pwd.__doc__}")

API documentation

cmd_line_history

Save command line history and provide a command line completer for python.

Contents:

Indices and tables