Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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 would 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 arrow 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\]

Ctrl^l, reset, and clear all do what they say, but when I type the command back in or up arrow 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.

share|improve this question
    
In my experience slm's suggestion is what messes this up in most cases. Does your $COLUMNS and $LINES match up with actual size of window? Does it represent it self with echo -en \\033c (Reset of terminal)? Is the issue present if you execute something like env -i bash --norc? –  Sukminder Dec 20 '13 at 0:13
    
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? –  Muricula Dec 20 '13 at 0:44
    
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? –  Muricula Dec 20 '13 at 0:53
    
\[\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... –  Amphiteót Dec 20 '13 at 1:19

2 Answers 2

up vote 8 down vote accepted

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:

Reason for the behavior is because it believe the prompt is longer then it 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.

share|improve this answer
    
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. –  Amphiteót Dec 20 '13 at 2:42
1  
@illuminÉ: Have not looked at the source, but added an update with a note on behavior from observation. –  Sukminder Dec 20 '13 at 3:26

This sounds like an issue with your COLUMNS & LINES environment variable settings. When you resize the window they're typically set automatically by gnome-terminal (I believe) you can force them to be manually set by issuing the command resize.

Example

If I resize my gnome-terminal to 79x17 my variables show up like so:

$ echo $COLUMNS; echo $LINES
79
17

I can force it like so:

$ resize
COLUMNS=79;
LINES=17;
export COLUMNS LINES;
share|improve this answer
    
Interesting, but doesn't help. –  Muricula Dec 19 '13 at 22:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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