home |
electronics |
toolbox |
science club |
tuxtalk |
photos |
e-cards |
online-shop
May 2026
windows terminal, bash, text wrapping incorrectly over color prompt
Are long command lines wrapping over your bash prompt? At work I have
been using windows terminal and this terminal is much less robust than
xterm. It's technically not a windows terminal fault but rather a bash problem but
xterm seems to have a better a better error handling and it does not mess up the screen
as severely as on windows terminal.
What is happening is this: you type a long command line and instead of the text wrapping to the next line you
see it overwriting your command prompt. When you recall a command from the history then things are messed up too and you see a mixture of the last command as well as the history you are stepping through.
It turns out that the PS1 bash prompt itself is causing the lines on
the terminal to wrap incorrectly. The problem does not happen with a plain
ascii prompt such as
PS1='[\h \W]\$'
The lines start to wrap incorrectly only when you use ansi color codes in
the prompt. It happens because Bash needs to know which
characters in your PS1 are non-printing. The ansi color codes are
characters in the PS1 prompt but they don't show on the screen. They
just change the color. This causes an incorrect calculation of the line
length and therefore incorrect line wrapping.
The solution is to enclose non printing characters between \[ and \].
This sequence tells bash to not count these characters when calculating
the line length. This fixes the incorrect line wrapping on the screen.
A PS1 prompt that works is:
PS1='[\[\033[01;34m\]\h\[\033[00m\] \W]\$'
The below would be visually the same prompt but it messes up the screen:
PS1='[\033[01;34m\h\033[00m \W]\$'
© 2004-2026 Guido Socher