[Tutor] OT (probably): How to change default tab key value to 4 spaces in GNOME Terminal?

Peter Otten __peter__ at web.de
Sun Apr 26 05:02:04 EDT 2020


boB Stepp wrote:

> Linux Mint, Python 3.7.5, GNOME Terminal 3.28.1
> 
> A while back I became tired of the default Python interpreter prompt
> ">>>" and replaced it with the Python version number, e.g., "3.7.5:
> ".  But my original version that I have used for years did not take
> into account the secondary default prompt "..." and where it put the
> cursor.  For whatever reason my annoyance level with this flaw rose
> high enough today that I fixed it as follows:
> 
> """Configure prompt appearance in Python REPL."""
> 
> import sys
> 
> py_version = sys.version.split()[0]
> sys.ps1 = py_version + ":  "
> 
> # Ensure cursor lines up with previous line of code:
> sys.ps2 = "..." + " " * (len(sys.ps1) - 3)
> 
> This does what I want, but now there is a new annoyance:
> 
> 3.7.5:  def f(s):
> ...                   print(s)
> ...
> 3.7.5:  f('Jeremy')
> Jeremy
> 
> In the second line as I was typing it I pressed <tab> when the cursor
> was lined up with the "d" in "def".  What I see in the terminal is the
> "p" in "print" lining up under the colon as I have shown above.  I
> understand that the default terminal behavior is that <tab> shifts in
> 8 columns wide tab stops.  I would like to change this behavior for
> when I am in the Python interpreter to have <tab> always shift 4
> columns from the starting cursor position.  Does anyone know how to
> accomplish this?
> 
> After some searching online the best advice I could find was to enter
> at the command prompt "tabs -4".  I tried this and as far as I can
> tell it did not work for me while in the Python interpreter.  However,
> in drafting this email to Tutor when I actually copy and paste what
> looks like the above code in my terminal window actually pastes into
> this Gmail message as:
> 
> 3.7.5:  def f(s):
> ...             print(s)
> ...
> 3.7.5:  f('Jeremy')
> Jeremy
> 
> Hmm.  That is strange!  Pasting what appears different in my terminal
> shows up in Gmail like I want it to appear in the terminal.  That is
> suggestive of the tab character in the terminal has the value I want,
> but if this is so, where are the additional 4 spaces/columns coming
> from?

With readline you can do

import readline
readline.parse_and_bind("TAB: '    '")

but then you lose tab-completion (which you don't seem to use anyway).
An alternative might be to keep autocompletion and bind 4-space indent to 
another key:

readline.parse_and_bind("TAB: complete")
readline.parse_and_bind("C-h: '    '")  # Control-h




More information about the Tutor mailing list