I edited the .../etc/profile to export PS1="\033[1;34m$ \033[0m" and the problem happens if i type many characters. The cursor jumps to the beginning of current line before reaching the end. Please check.
The problem with your PS1 happens on my Debian as well, so it's not Termux specific. Try asking in a bash support forum.
@zhoue792a8 for hassle free BASH PS1 in Termux:
$ cat .bash_profile
PS1="[\A\[\033[0;32m\] \W \[\033[0m\]]\\$ "
Does everything it is supposed to in Android 6; wraps nicely too:
[10:54 ~ ]$ grep Refresh setupTermuxArch.sh
read -p "Refresh $HOME/TermuxArchBloom? [Y|n] " rbuanswer
# [refresh|refresh installdir] Refresh Arch Linux in Termux PRoot scripts created by TermuxArch. Useful for refreshing TermuxArch generated scripts to the newest version.
[10:55 ~ ]$
@e792a8
I realize that this is closed, but no-one has yet explained the original cause of this bug and how to fix it, so here goes:
The string \033[1;34m$ \033[0m contains 13 characters in total, 11 of which are non-printing characters. Here are the 11 non-printing characters ("n") and 2 printing characters ("p") shown "spaced-out":
\033 [ 1 ; 3 4 m $ \033 [ 0 m
n n n n n n n p p n n n n
Unfortunately, the shell is unaware that the non-printing characters don't "print", so it "thinks" that your prompt is 13 characters long (and it uses that incorrect length to determine where and when to add line feeds etc). The fix is to tell the shell where the non-printing characters sections are, by enclosing them in \[ and \] thus:
\[ \033 [ 1 ; 3 4 m \] $ \[ \033 [ 0 m \]
n n n n n n n p p n n n n
^^ ^^ ^^ ^^
As an aside, you rarely need to export PS1 - you just need to set it, as per the suggestion by @sdrausty (see the last third of https://askubuntu.com/a/984073/637246 for an excellent explanation of why). So, finally, you need something like:
PS1="\[\033[1;34m\]$ \[\033[0m\]"
or (if you prefer to use \e instead of an octal-escape):
PS1="\[\e[1;34m\]$ \[\e[0m\]"
HTH
@jaimet
That's it. I have just learned it from a book several days ago, while setting up an Arch Linux. Thank you for sharing anyway.
@jaimet
I give you a big 👍
Your excellent solution solved my problem. ✌
thanks a lot! 😘
Most helpful comment
@e792a8
I realize that this is closed, but no-one has yet explained the original cause of this bug and how to fix it, so here goes:
The string
\033[1;34m$ \033[0mcontains 13 characters in total, 11 of which are non-printing characters. Here are the 11 non-printing characters ("n") and 2 printing characters ("p") shown "spaced-out":Unfortunately, the shell is unaware that the non-printing characters don't "print", so it "thinks" that your prompt is 13 characters long (and it uses that incorrect length to determine where and when to add line feeds etc). The fix is to tell the shell where the non-printing characters sections are, by enclosing them in
\[and\]thus:As an aside, you rarely need to export PS1 - you just need to set it, as per the suggestion by @sdrausty (see the last third of https://askubuntu.com/a/984073/637246 for an excellent explanation of why). So, finally, you need something like:
or (if you prefer to use
\einstead of an octal-escape):HTH