In order to manage the cursor properly, the shell needs to know which parts of the prompt string actually cause the cursor to advance and which will not.
For this purpose, any parts of the prompt that don't produce visible characters should be encapsulated with \\[
...\\]
.
Note that the prompt formatting ultimately happens in multiple stages: the prompt expansion codes and shell variables are handled by the shell itself, but the color & formatting codes are just passed through to the terminal emulator, which is a separate process.
Your prompt has the following:
declare -- PS1="\\[\\e]0;\\u@\\h: \\w\\a\\]\${debian_chroot:+(\$debian_chroot)}\\[\\e[1m\\e[38;5;208m\\e[48;5;24m\\u@\\h\\[\\e[0m\\]:\\[\\e[97m\\]\\w\\[\\e[0m\\]\\\$
\\[
- begin non-productive part
\\e]0;\\u@\\h: \\w\\a
- this part changes the window title in terminal emulators that support this feature, so none of this becomes part of the actual prompt.
\\]
- end non-productive part
\${debian_chroot:+(\$debian_chroot)}
- if the $debian_chroot
variable is set and non-null (i.e. the shell is inside a chrooted environment), output its value within parentheses; otherwise output a null string. These expansions are handled entirely within the shell, so the shell always knows how long this part will be.
\\[
- begin another non-productive part
\\e[1m\\e[38;5;208m\\e[48;5;24m
- set bold; set 8-bit foreground color; set 8-bit background color. These are valid non-printing format codes.
- Here should be a "end non-productive part" code, but it's missing!
\\u@\\h
- this will produce the text admin@OLAF
, i.e. username@hostname.
\\[\\e[0m\\]
- properly encapsulated "reset to default output" code
:
- just output a colon
\\[\\e[97m\\]
- properly encapsulated "set bright foreground color" code
\\w
- shell prompt expansion: output current working directory
\\[\\e[0m\\]
- properly encapsulated "reset to default output" code
\\\$
- shell prompt special expansion: output $
if a regular user, or #
if root.
So, because of the missing code, the shell assumes the string "admin@OLAF" (expanded by the shell from \\u@\\h
) becomes part of the formatting codes and will end up somewhere other than part of the actual prompt. The string is 10 characters long, and so when the shell needs to re-write the command after the prompt, it miscalculates the column to move to by exactly 10 characters.