Rodrigo Lopez
Rodrigo Lopez

Reputation: 901

Autocomplete and tab key in PDB

I have been trying to get TAB to do something else than inserting a tab while at the (pdb) prompt.

What I have in mind is triggering autocomplete such as in here or here, but the tab key doesn't do anything else than adding tabs to pdb.

So with:

(pdb)var + tabKeyPressed

I'd want to get:

(pdb)variable

instead of:

(pdb)var[          ]

Upvotes: 29

Views: 11282

Answers (4)

Constantin Hong
Constantin Hong

Reputation: 867

If you use macOS with NetBSD libedit(default) or your Python is not compiled with GNU readline lib but Net BSD libedit, insert python:bind ^I rl_complete in ~/.editrc. In this case, ^I has two characters.(^ and I)

Also, I found the following strings are possible.

Add one of the strings below into ~/.editrc

# https://github.com/python/cpython/pull/107748#issue-1840209719
# The PR is merged. The tab completion for
# editline(libedit) will be enabled Python 3.13

python:bind "\t" rl_complete
python:bind "\011" rl_complete
python:bind \\t rl_complete
python:bind "   " rl_complete
# " " is a tab.

Also, you have to remove the GNU readline solutions you tried. e.g. some part in ~/.pdbrc(or ./.pdbrc) or rlcompleter of the answer.

Upvotes: 2

eshizhan
eshizhan

Reputation: 4645

Official documents said:

Changed in version 3.3: Tab-completion via the readline module is available for commands and command arguments, e.g. the current global and local names are offered as arguments of the p command.

https://docs.python.org/3/library/pdb.html

So you just using p command:

(Pdb) p var[TAB] # complete global and local names
var1 var2
(Pdb) [TAB] # complete commands
EOF        b          cl         cont       disable    exit       interact   list       next       quit       retval     source     unalias    up         
a          break      clear      continue   display    h          j          ll         p          r          run        step       undisplay  w          
alias      bt         commands   d          down       help       jump       longlist   pp         restart    rv         tbreak     unt        whatis     
args       c          condition  debug      enable     ignore     l          n          q          return     s          u          until      where 

Upvotes: 12

ВелоКастръ
ВелоКастръ

Reputation: 4483

The iPython is third-party solution for this problem. Sometimes you can only rely on vanilla Python. I found 2 solutions for it.

Per-shell solution - usage module 'rlcompleter':

$ python3

Python 3.4.3 (default, Sep 14 2016, 12:36:27) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> import pdb
>>> pdb.set_trace()
--Return--
> <stdin>(1)<module>()->None

# press tab - but nothing
(Pdb) str.
*** SyntaxError: invalid syntax
(Pdb) --KeyboardInterrupt--
(Pdb) c
>>> import rlcompleter
>>> pdb.Pdb.complete=rlcompleter.Completer(locals()).complete
>>> pdb.set_trace()
--Return--
> <stdin>(1)<module>()->None
(Pdb) str.
str.__add__(            str.__getattribute__(   str.__name__            str.__text_signature__  str.isdigit(            str.rfind(
str.__base__(           str.__getitem__(        str.__ne__(             str.__weakrefoffset__   str.isidentifier(       str.rindex(
str.__bases__           str.__getnewargs__(     str.__new__(            str.capitalize(         str.islower(            str.rjust(
str.__basicsize__       str.__gt__(             str.__prepare__(        str.casefold(           str.isnumeric(          str.rpartition(
str.__call__(           str.__hash__(           str.__qualname__        str.center(             str.isprintable(        str.rsplit(
str.__class__(          str.__init__(           str.__reduce__(         str.count(              str.isspace(            str.rstrip(
str.__contains__(       str.__instancecheck__(  str.__reduce_ex__(      str.encode(             str.istitle(            str.split(
str.__delattr__(        str.__itemsize__        str.__repr__(           str.endswith(           str.isupper(            str.splitlines(
str.__dict__            str.__iter__(           str.__rmod__(           str.expandtabs(         str.join(               str.startswith(
str.__dictoffset__      str.__le__(             str.__rmul__(           str.find(               str.ljust(              str.strip(
str.__dir__(            str.__len__(            str.__setattr__(        str.format(             str.lower(              str.swapcase(
str.__doc__             str.__lt__(             str.__sizeof__(         str.format_map(         str.lstrip(             str.title(
str.__eq__(             str.__mod__(            str.__str__(            str.index(              str.maketrans(          str.translate(
str.__flags__           str.__module__          str.__subclasscheck__(  str.isalnum(            str.mro(                str.upper(
str.__format__(         str.__mro__             str.__subclasses__(     str.isalpha(            str.partition(          str.zfill(
str.__ge__(             str.__mul__(            str.__subclasshook__(   str.isdecimal(          str.replace(            
(Pdb) c
>>>

System-wide solution - usage file ~/.pdbrc

$ cat ~/.pdbrc
import rlcompleter
pdb.Pdb.complete=rlcompleter.Completer(locals()).complete
$ python3
Python 3.4.3 (default, Sep 14 2016, 12:36:27) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pdb
>>> pdb.set_trace()
--Return--
> <stdin>(1)<module>()->None
(Pdb) str.
str.__add__(            str.__getattribute__(   str.__name__            str.__text_signature__  str.isdigit(            str.rfind(
str.__base__(           str.__getitem__(        str.__ne__(             str.__weakrefoffset__   str.isidentifier(       str.rindex(
str.__bases__           str.__getnewargs__(     str.__new__(            str.capitalize(         str.islower(            str.rjust(
str.__basicsize__       str.__gt__(             str.__prepare__(        str.casefold(           str.isnumeric(          str.rpartition(
str.__call__(           str.__hash__(           str.__qualname__        str.center(             str.isprintable(        str.rsplit(
str.__class__(          str.__init__(           str.__reduce__(         str.count(              str.isspace(            str.rstrip(
str.__contains__(       str.__instancecheck__(  str.__reduce_ex__(      str.encode(             str.istitle(            str.split(
str.__delattr__(        str.__itemsize__        str.__repr__(           str.endswith(           str.isupper(            str.splitlines(
str.__dict__            str.__iter__(           str.__rmod__(           str.expandtabs(         str.join(               str.startswith(
str.__dictoffset__      str.__le__(             str.__rmul__(           str.find(               str.ljust(              str.strip(
str.__dir__(            str.__len__(            str.__setattr__(        str.format(             str.lower(              str.swapcase(
str.__doc__             str.__lt__(             str.__sizeof__(         str.format_map(         str.lstrip(             str.title(
str.__eq__(             str.__mod__(            str.__str__(            str.index(              str.maketrans(          str.translate(
str.__flags__           str.__module__          str.__subclasscheck__(  str.isalnum(            str.mro(                str.upper(
str.__format__(         str.__mro__             str.__subclasses__(     str.isalpha(            str.partition(          str.zfill(
str.__ge__(             str.__mul__(            str.__subclasshook__(   str.isdecimal(          str.replace(            
(Pdb) c
>>> 

Notes:

  1. Tested only on the Python 3.4

  2. OS - Linux Mint

  3. Based on https://reminiscential.wordpress.com/2009/06/12/python-enable-auto-complete-in-a-pdb-session/

Upvotes: 34

shx2
shx2

Reputation: 64338

ipdb to the rescue.

ipdb exports functions to access the IPython debugger, which features tab completion, syntax highlighting, better tracebacks, better introspection with the same interface as the pdb module.

Upvotes: 15

Related Questions