21

In a terminal window any smaller than fullscreen, if I type in a long command it starts overwriting the current line I'm on. If I type an additional line's worth, it finally moves to the next line. Does anyone know how to fix this?

This is what happens visually:

terminal screenshot

Edit: These are my prompt settings:

PS1='\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\033[1m`__git_ps1`\033[0m\$ '
TERM=xterm

.bashrc:

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\033[1m`__git_ps1`\033[0m\$ '
fi

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac
wjandrea
  • 14,236
  • 4
  • 48
  • 98
Matt Baer
  • 410

3 Answers3

16

For some reason the terminal size is mis-reporting. It should change when you resize the terminal.

Try this in your .bashrc command file:

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
  • It looks like this was already in my .bashrc file. Running shopt shows that checkwinsize is on. – Matt Baer Aug 04 '12 at 19:48
  • Hmm, what are the values of LINES and COLUMNS when the text is messed up? – Julian Knight Aug 07 '12 at 20:56
  • 3
    LINES is 24, COLUMNS is 80. Resizing the window does change these values, but the text is still messed up. – Matt Baer Aug 08 '12 at 15:39
  • Ah. OK, I suspect the setting for the PROMPT now. Can you post the values of PS1 and TERM. In fact, post the line defining PS1 in .bashrc – Julian Knight Aug 08 '12 at 20:41
  • I updated the question with these answers. – Matt Baer Aug 10 '12 at 15:43
  • OK, so what happens if you set PS1 to something rather simpler? Say something like \u@\h: \w\a\ or just zzzzzzzzzzzzz> (just to keep it long). – Julian Knight Aug 10 '12 at 21:43
  • That does the trick! It looks like \033[1m\__git_ps1`\033[0mwas causing it. By taking out the formatting (\033[1mand\033]0m`) I can keep the git branch in there and the text will wrap fine. Thanks for the help! – Matt Baer Aug 13 '12 at 18:57
  • Ah, glad we fixed it between us. – Julian Knight Aug 18 '12 at 17:36
  • It was driving me crazy, thanks for the answer – shammelburg Nov 20 '20 at 09:04
  • I had the PS1 set to \$ via PS1='\$ ' in the ~/.bashrc. Turns out the use of single quotes confuses the backslash-escaped dollar sign in the PS1 assignment, readjusted to double-quotes PS1="\$ " - et voilà. Hope helps someone, quite a lot of frustration on my part to finally figure that one out. – muthuh Sep 25 '22 at 10:06
10

I found the answer here. The answer with the most votes solved it for me.

To sum it up I had to wrap my formats in \[ \]
export PS1='\[\033[1;32m\]$(whoami)@$(hostname): \[\033[0;37m\]$(pwd)$ \[\033[0m\]'

Jerinaw
  • 281
1

For those using PuTTY terminal (v0.67) and terminal size is not updated correctly:

  1. You can right click on the window's left hand icon, then select Change Settings....
  2. Within PuTTY Reconfiguration, from left hand sidebar, click on Window > Set the size of the window > Columns.
  3. Update the Columns value manually. I changed mine to 255 and that stopped the terminal overwriting on its own line (when I type).

menu screenshot

settings screenshot

wjandrea
  • 14,236
  • 4
  • 48
  • 98
  • Note that Windows applications are technically off-topic here, but this is fine for the sake of people SSH-ing into Ubuntu installs. – wjandrea Jan 24 '19 at 03:56