I developped a python application that uses the cmd module to make an interactive command-line interface. This cmd module uses readline for command-line management: completion, history...

My issue is: on the computer I'm trying to use that app, when I type too long lines, it goes back to the beginning of the line and doesn't put a line feed. So I'm writing over the line itself. This makes browsing and editing commands in history a real pain. Although it works well on my development box.

I tried changing the shell from which I started python (from bash to sh), no difference, I tried to change the terminal I used (from Terminator to XTerm or UXTerm), no difference.

What would be your guess about where it comes from and how to solve this issue?

Edit: Here is a screenshot. I have found the cause of the bug, it comes from color characters that are not well treated when comparing to terminal line length, I guess.


In the first part of the screenshot (above the green line), I'm using a colored prompt (you can see the pink /), and when reaching the end of the line, I start writing over the prompt. If I keep writing, I can write one full line, overwriting the original first line completely and then it starts feeding new lines.

In the second part (below the green line), I'm writing until the end of line and the line feed appears at the right moment.

What could be the explanation behind this behaviour ? (It doesn't happen on every configuration, as I said.) How could I fix it ?

  • can you post the code and a screenshot? Nov 28 '14 at 11:04
  • Please consider editing your question title - it will likely be confusing to non-idiomatic English speakers Nov 28 '14 at 11:23
  • @steeldriver: done ;) It doesn't have to do with a particular part of code so I will try to come up with a small PoC where I can reproduce the bug.
    – DaLynX
    Nov 28 '14 at 11:29
  • Do you have the same problem with other readline applications? Do you have the same problem if you rename your ~/.inputrc out of the way? Nov 29 '14 at 20:58
  • it might be worth considering libedit as an alternative. it is the (heavily extended) library responsible for much of zsh's cli magic (and also a build-time option for dash). in almost every case i find implementations of it superior to readline - particularly in their handling of multi-line commands. where readline concats them all (especially annoying where HOME/END are concerned) libedit respects newline boundaries. i often write whole scripts at a prompt w/ ctrl+v ctrl+j to queue up the next line.
    – mikeserv
    Nov 30 '14 at 9:21

It was coming from my use of colors. I was using the following strings to colorise my text:

class colors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'

I added \001 and \002 characters, and now, it works!

class colors:
    HEADER = '\001\033[95m\002'
    OKBLUE = '\001\033[94m\002'
    OKGREEN = '\001\033[92m\002'
    WARNING = '\001\033[93m\002'
    FAIL = '\001\033[91m\002'
    ENDC = '\001\033[0m\002'

Related solution post: https://stackoverflow.com/questions/8806643/colorized-output-breaks-linewrapping-with-readline/8916332#8916332

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.