Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7565391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:08:34+00:00 2026-05-30T14:08:34+00:00

I use standard tips for customizing interactive Python session: $ cat ~/.bashrc export PYTHONSTARTUP=~/.pystartup

  • 0

I use standard tips for customizing interactive Python session:

$ cat ~/.bashrc
export PYTHONSTARTUP=~/.pystartup

$ cat ~/.pystartup
import os
import sys
import atexit
import readline
import rlcompleter

historyPath = os.path.expanduser("~/.pyhistory")

def save_history(historyPath=historyPath):
    import readline
    readline.write_history_file(historyPath)

if os.path.exists(historyPath):
    readline.read_history_file(historyPath)

term_with_colors = ['xterm', 'xterm-color', 'xterm-256color', 'linux', 'screen', 'screen-256color', 'screen-bce']
if os.environ.get('TERM') in term_with_colors:
    green='\033[32m'
    red='\033[31m'
    reset='\033[0m'
    sys.ps1 = red + '>>> ' + reset
    sys.ps2 = green + '... ' + reset
del term_with_colors

atexit.register(save_history)
del os, sys, atexit, readline, rlcompleter, save_history, historyPath

Now I get context sensitive completion and color prompt.

The problem comes from color prompt – when I invoke history-search-backward (by pressing UP) in an interactive Python session, Readline takes in account terminal escape sequences, so the cursor position is wrongly calculated and the text is wrongly displayed.

In Bash man page this problem is mentioned and fixed by special markers:

    \[     begin a sequence of non-printing characters,
           which could be used to embed a
           terminal control sequence into the prompt
    \]     end a sequence of non-printing characters

How to fix this issue for Python prompt?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T14:08:36+00:00Added an answer on May 30, 2026 at 2:08 pm

    I open info readline and found:

     -- Function: int rl_expand_prompt (char *prompt)
         Expand any special character sequences in PROMPT and set up the
         local Readline prompt redisplay variables.  This function is
         called by `readline()'.  It may also be called to expand the
         primary prompt if the `rl_on_new_line_with_prompt()' function or
         `rl_already_prompted' variable is used.  It returns the number of
         visible characters on the last line of the (possibly multi-line)
         prompt.  Applications may indicate that the prompt contains
         characters that take up no physical screen space when displayed by
         bracketing a sequence of such characters with the special markers
         `RL_PROMPT_START_IGNORE' and `RL_PROMPT_END_IGNORE' (declared in
         `readline.h'.  This may be used to embed terminal-specific escape
         sequences in prompts.
    

    As text suggested I searched for RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE definition in readline.h and found next:

    /* Definitions available for use by readline clients. */
    #define RL_PROMPT_START_IGNORE  '\001'
    #define RL_PROMPT_END_IGNORE    '\002'
    

    So I put appropriate changes to my ~/.pystartup:

        green='\001\033[32m\002'
        red='\001\033[31m\002'
        reset='\001\033[0m\002'
    

    and now all work fine!!!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're planning to use standard ASP.NET user authentication for our application. However, by default
I would like to use standard ASP.NET file download response, like in other Stack
I would like to use standard icons in treeview but I am not sure
For those of us who use standard shared hosting packages, such as GoDaddy or
I just noticed that you can not use standard math operators on an enum
If for example I should not use standard library functions like printf() , putchar()
In my asp mvc application I use standard client side validation (DataAnnotations + MicrosoftAjax.js
Our company builds custom Java EE web solutions. At the moment, we use standard
You can use a standard dot notation or a method call in Objective-C to
How can I use the standard Edit menu in my Palm OS application, instead

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.