Search code examples
bashps1

bash ctrl-A is not bringing the cursor to the beginning of line


I've recently switched my default shell from csh to bash. One thing I noticed after switching that ctrl-A is not bringing the cursor to the begining of line - it brings it to somewhere in the middle if the command being typed quite long.

Based on this question, it seems to be related to my PS1 variable. My PS1 variable is as follows:

export PS1="\e[47;30m[\s \t \u@\H \W] \$\e[0m"

If I put "\[" in the beginning and "\]" in the end (i.e. "\[\e[47;30m[\s \t \u@\H \W] \$\e[0m\]"), the ctrl-A starts working but prompt disappears when I start typing something.

I've emacs bindings enabled (set -o emacs).

Please let me know how to overcome this problem.


Solution

  • you need to properly enclose the escape sequences within the "\[" and "\]" characters

    PS1="\[\e[47;30m\][\s \t \u@\H \W] \$\[\e[0m\]"