Terminal prompt not wrapping correctly

  • I have an issue where if I type in very long commands in bash the terminal will not render what I'm typing correctly. I'd expect that if I had a command like the following:

    [email protected] ~/somepath $ ssh -i /path/to/private/key
    [email protected]

    The command should render on two lines. Instead it will often wrap around and start writing over the top of my prompt, somewhat like this:

    [email protected] -i /path/to/private/key

    If I decide to go back and change some argument there's no telling where the cursor will show up, sometimes in the middle of the prompt, but usually on the line above where I'm typing.

    Additional fun happens when when I Up to a previous command. I've tried this in both gnome-terminal and terminator and on i3 and Cinnamon. Someone suggested it was my prompt, so here that is:

    \[\033[01;32m\]\u:\[\033[01;34m\] \W\033[01;34m \$\[\033[00m\]

    Ctrll, reset, and clear all do what they say, but when I type the command back in or Up the same things happens.

    I checked and checkwinsize is enabled in bash. This happens on 80x24 and other window sizes.

    Is this just something I learn to live with? Is there some piece of magic which I should know? I've settled for just using a really short prompt, but that doesn't fix the issue.

    So using the command `env -i bash --norc` fixes it. The $COLUMNS and $LINES match. Does that mean that there's something funny with my .bashrc?

    So I commented out my .bashrc and wound up isolating my prompt as the problematic part, specifically the coloration syntax involved. What's wrong with the PS1 above?

    `\[\033[01;32m\]\u: \[\033[01;34m\]\W \[\033[01;34m\] \$ \[\033[0m\]` seems to avoid the weirdness in the behavior - but don't know if it respects your original prompt completely...

  • Runium

    Runium Correct answer

    7 years ago

    Non-printable sequences should be enclosed in \[ and \]. Looking at your PS1 it has a unenclosed sequence after \W. But, the second entry is redundant as well as it repeats the previous statement "1;34".

    \[\033[01;32m\]\u:\[\033[01;34m\] \W\033[01;34m \$\[\033[00m\]
                      |_____________|               |_|
                             |                       |
                             +--- Let this apply to this as well.
    

    As such this should have intended coloring:

    \[\033[1;32m\]\u:\[\033[1;34m\] \W \$\[\033[0m\]
                                   |_____|
                                      |
                                      +---- Bold blue.
    

    Keeping the "original" this should also work:

    \[\033[1;32m\]\u:\[\033[1;34m\] \W\[\033[1;34m\] \$\[\033[0m\]
                                      |_|         |_|
                                       |           |
                                       +-----------+-- Enclose in \[ \]
    

    Edit:

    The reason for the behavior is because bash believes the prompt is longer then it actually is. As a simple example, if one use:

    PS1="\033[0;34m$"
           1 2345678
    

    The prompt is believed to be 8 characters and not 1. As such if terminal window is 20 columns, after typing 12 characters, it is believed to be 20 and wraps around. This is also evident if one then try to do backspace or Ctrl+u. It stops at column 9.

    However it also does not start new line unless one are on last column, as a result the first line is overwritten.

    If one keep typing the line should wrap to next line after 32 characters.

    If you have - or anyone - has an explanation as to what exactly in the original sequence caused the line to repeat over itself, I'd be interested in knowing that. Also +1 for how you showed this visually.

    @illuminÉ: Have not looked at the source, but added an update with a note on behavior from observation.

    Just in case you are facing any problems, you can use this website to create a new one - http://bashrcgenerator.com/

    This is amazing, thank you @Runium - would you mind sharing how you knew this? I would love to find some documentation on this.

    @nycynik: Observation. I guess the closest to documentation on this is the source code ...

    Not all heroes wear capes.

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM