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

boB Stepp robertvstepp at gmail.com
Mon Apr 27 00:07:22 EDT 2020


OK, I believe that I have a solution that I can live with.

On 4/26/20 4:02 AM, Peter Otten wrote:
> boB Stepp wrote:

> 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

I used Peter's latter thought of binding four spaces to a key.  My
.pythonstartup file now looks like this:

"""Configure prompt appearance in Python REPL."""

import readline
import sys

# Use control-t to insert 4 spaces
readline.parse_and_bind("C-t:'    '")

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)


In any terminal relying on the GNU Readline Library (again see
https://tiswww.cwru.edu/php/chet/readline/rluserman.html#SEC_Top) C-t is
bound to the action to transpose two characters.  I would hardly ever
use this, so I am willing to give up that functionality in the Python
interpreter.

This now gives me what I want.  The earlier (in this thread) example now
looks like:

3.7.5:  def f(s):
...         print(s)
...
3.7.5:  f("Jeremy")
Jeremy

which is exactly how I want it.  As a side note if I do the same
copy/paste into Google Gmail the "p" in print lines up under the "d" in
"def".  This is not what is in my terminal and, for me, this is the last
straw on using Gmail's web-based email editor.  If it won't accurately
render a copy/paste operation I am not going to tolerate it any longer.
I am currently using Thunderbird to type this as it is already installed
on my PC.  When I get time I will have to get it configured like I want
it after which I will say bye to Google's Gmail editor.  And perhaps
later I will abandon Gmail altogether.  But that is a battle for later, too.

I did go ahead and create a .inputrc file in my home directory.  Right
now it is quite simple:

set editing-mode vi


As I normally use Neovim/Vim/vi (depending on what computer I am on) as
my editor, I think I will like this.  Normally in vi C-t when in insert
mode would insert a tab character.  In my GNOME Terminal C-t still
transposes two characters despite otherwise being in vi-mode.  There is
probably a way to change this, but in the link above they only have the
briefest of descriptions of vi-mode.  There is probably another docs
page somewhere that will give me the information I need to have C-t
universally insert 4 spaces, but I will save that battle for another day.

To dn:  Sorry!  No subclassing today.  ~(:>))

Cheers!
boB


-- 
boB


More information about the Tutor mailing list