Terminal Prompt Issues – Fixing Prompt Not Wrapping Correctly

bashpromptterminal

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:

username@someserver ~/somepath $ ssh -i /path/to/private/key
myusername@something.someserver.com

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:

myreallylongusername@something.somelongserver.comh -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 80×24 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.

Best Answer

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.

Related Question