In my case, I wrote a Perl script to dinamically change the folders in prompt to "something shorter". The script was successful, prompt get updated automatically when cd, but if I navigated history (with arrow up/down), history used to overwrite one part of the new prompt. The ugly part of this issue is that the command line doesn't get cleared by moving through history, so one "short history" could apear INSIDE a previous "printed" PS1.
The solution is in the end.
My conclusions were two:
1- Dynamic PS1 with \[`executables`\] in the PS1 body are not guaranteed to keep the size when navigating history, because it seems that history forces to 2 characters margin in the left (maybe related to an ancester "$_" prompt)
2- To have an immutable length prompt, PS1 must be a fixed size string. Documentation allows to use \[ and \] where I ran a dynamic code inside to transform \w, but it presented that weakness.
Here is a nice example of fixed size PS1:
export PS1='\[\033[01;32m\]\u\[\033[00m\]:\[\033[01;36m\]\w\[\033[00m\]\$ '
My solution:
I rewrote the cd function in a file that is executable and sourced in both .profile and .bashrc.
This function then exports the new FIXED SIZE PS1 exactly in the change of pwd. Now history is NOT messing the prompt because the PS1 body has no longer dynamic parts. I execute the script out and concatenate the result, generating a new fixed size PS1 to export.
function cd {
MYDIR="${1:-${HOME}}"
builtin cd "${MYDIR}"
# echo 'from generic cd'
export PWD=`pwd`
#Dynamic script invoked out of PS1:
folders=`getPs1`
export PS1='\[\033[01;36m\]'$folders'\[\033[00m\]\$ '
}