Tab completion

  • Thread starter Steven D'Aprano
  • Start date
S

Steven D'Aprano

Does anyone use the tab-completion recipe in the docs?

http://docs.python.org/library/rlcompleter.html#module-rlcompleter

suggests using this to enable tab-completion:

try:
import readline
except ImportError:
print "Module readline not available."
else:
import rlcompleter
readline.parse_and_bind("tab: complete")

which is all very nice, but it makes it rather difficult to indent code
blocks:
....
Display all 174 possibilities? (y or n)


I like tab-completion, but I'd rather not be reduced to typing spaces for
indents in the interpreter. What do other people do?

Can I bind Ctrl-tab to the completion instead of regular tab? I tried
readline.parse_and_bind("C-tab: complete") but it still completes on
regular tab.

The GNU readline library claims that M-tab (which I guess is Alt-tab)
will enter a tab character. Problem is that Alt-tab is intercepted by my
window manager for something else.

Any other suggestions or hints?
 
C

Chris Rebert

Does anyone use the tab-completion recipe in the docs?

http://docs.python.org/library/rlcompleter.html#module-rlcompleter

suggests using this to enable tab-completion:

try:
   import readline
except ImportError:
   print "Module readline not available."
else:
   import rlcompleter
   readline.parse_and_bind("tab: complete")

which is all very nice, but it makes it rather difficult to indent code
blocks:

...
Display all 174 possibilities? (y or n)


I like tab-completion, but I'd rather not be reduced to typing spaces for
indents in the interpreter. What do other people do?

Can I bind Ctrl-tab to the completion instead of regular tab? I tried
readline.parse_and_bind("C-tab: complete") but it still completes on
regular tab.

The GNU readline library claims that M-tab (which I guess is Alt-tab)
will enter a tab character. Problem is that Alt-tab is intercepted by my
window manager for something else.

Any other suggestions or hints?

You could write a ~/.inputrc to change the bindings.

Cheers,
Chris
 
S

sjbrown

readline.parse_and_bind('\C-n: complete')

Makes it Ctrl-n do the completion, like vim.
 
C

Chris Jones

Does anyone use the tab-completion recipe in the docs?

http://docs.python.org/library/rlcompleter.html#module-rlcompleter

suggests using this to enable tab-completion:

try:
import readline
except ImportError:
print "Module readline not available."
else:
import rlcompleter
readline.parse_and_bind("tab: complete")

which is all very nice, but it makes it rather difficult to indent code
blocks:

...
Display all 174 possibilities? (y or n)


I like tab-completion, but I'd rather not be reduced to typing spaces for
indents in the interpreter. What do other people do?

Can I bind Ctrl-tab to the completion instead of regular tab? I tried
readline.parse_and_bind("C-tab: complete") but it still completes on
regular tab.

The GNU readline library claims that M-tab (which I guess is Alt-tab)
will enter a tab character. Problem is that Alt-tab is intercepted by my
window manager for something else.

Any other suggestions or hints?

CTRL+V <TAB>
 
R

Robert Kern

I like tab-completion, but I'd rather not be reduced to typing spaces for
indents in the interpreter. What do other people do?
Any other suggestions or hints?

I use IPython. The completion is set up (I don't know how) such that tabbing for
indentation does not trigger the completer. Additionally, you get reasonable
auto-indents inside functions and loops so I rarely have to tab in the first place.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
S

Steven D'Aprano

Acknowledge that using U+0009 for indentation is wrong, and use
beautiful U+0020 always.

Perhaps you missed the "in the interpreter". I'm *not* talking about what
happens when I hit TAB in my editor, I'm talking about hitting TAB in an
interactive Python session in a console window. I expect TAB to result in
an indent level, and backspace to remove that indent level. Tab-
completion clashes with that.

When defining a function in the interactive interpreter:
.... return "spam"
....
I do not wish to type four literal spaces to indent the 'return' line, or
backspace four times to remove it, but with tab-completion I am forced
to. I'm used to pressing the TAB key once to get an indent. Within the
interpreter, I do not care whether it inserts a tab character or four
spaces or seven formfeed characters, so long as the Python parser
recognises it as a single indent level and typing backspace once removes
that indent level.

I'm sorry if this was unclear from my post.

“M-tab†is Emacs-speak for “<Meta> plus <Tab>â€. Not all keyboards have
“Altâ€, you see (maybe none, when Emacs first started referring to the
Meta modifier?).

But on my keyboard, I have no Meta key, I have an Alt key.

In most terminal programs, you can send “<Alt>-<foo>†also by pressing


Suck it up and accept the truth! U+0020 is the unambiguous indentation
character that always does what it should, and U+0009 is a horrible
mistake which must be suppressed with extreme prejudice. No true
Pythonista would disagree.

The "No true Scotsman" fallacy.

But regardless of what I use in my source code, I'm purely talking about
interactive sessions in a console, not an editor.
 
S

Steven D'Aprano

It sounds like, instead of pressing <Tab> once, you can press <Space>
once and meet your requirements.

Yuck!

Well, okay, technically that would work, but it's seriously disturbing to
set your indent level to one space. If there's a more elegant solution
that gives four-space indents I'd prefer that.

Thanks to all for your suggestions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top