Search code examples
bashterminalcommand-prompt

How to make the changed console prompt text and its layout in Linux Mint GNOME Terminal stable?


In order to change the default Terminal prompt text and its layout I am using following command:

export PS1="\e[1;37;40m\t \e[7;37m\w\e[0m $ "

This changes the prompt like illustrated by images in following screenshots demonstrating that scrolling back in the command history after the prompt text is changed does not work as it should:

enter image description here ^--- executing the command got by scrolling through the command history. Scrolling back and forth from this point does not change anything in case of the standard prompt text, but ...

enter image description here ... with the changed prompt scrolling back in the command history to reach another previously entered commands goes first through the last command entered ...

enter image description here ... what changes how the next commands in the history list are displayed:

enter image description here ^--- Executing the 'weird' displayed 'command' results in usual execution of the command neglecting the text erroneously displayed as its prefix. Using Backspace does not have any effect on the prefix which stays there unreachable if once there (like it would belong to the prompt).

My question in this context is: How can I change the prompt to the desired one without the weird side-effect? Is it a bug in the Terminal software or do I miss something important to know to make it work the right way?


Solution

  • While there's likely some documentation somewhere pointing this out ... eons ago I figured out that I need to wrap the non-printing codes in a pair of \[ and \].

    Applying to OP's PS1 definition:

    export PS1="\[\e[1;37;40m\]\t \[\e[7;37m\]\w\[\e[0m\] $ "
                ^^           ^^   ^^        ^^  ^^     ^^
    

    Using OP's original PS1 definition I was able to generate some wacky behavior when scrolling back in my history.

    With the suggested change I can now scroll back in the history as would (normally) be expected.

    NOTE: above was tested in:

    • cygwin / mintty(xterm)
    • ubuntu (20.04;GNOME) / Terminal(xterm) and Terminator(xterm)