m00saca
m00saca

Reputation: 363

Command Line - PS1 Settings causing unexplainable behavior

Good morning,

Something strange is going on with my command line and I need a little assistance figuring out what the problem is.

First I have been using my Mac Book to write code for over a year and when I started I was using tools provided by The Flatiron School. I mention this because I don't know how this .bash_profile file was created in my home directory and I suspect it was created when I set up my machine with their "assistance". I think I am dealing with some version of Git Bash but I'm not sure- I was blindly following this bootcamp's instructions.

Recently I changed a local variable CHAR in my .bash_profile that is a part of my PS1 settings. Changing my prompt from //❤️ (default) to ⚡️ 👻 ⚡️ (much cooler).

I read up on the PS1 setting in order to change my command line prompt and believe to have done everything right but two really annoying behaviors have arisen:

1) When I delete all my terminal input my prompt is deleted as well but returns after hitting enter like this: (going to have to take my word for it) Issue #1 deleting prompt

2) When my terminal input reaches the edge of the window it forms a new line of 5 characters then forms a new line again like so:

Issue #2 skipping a line, 5 characters, and a new line

Finally here is what I believe to be the relevant code in my .bash_profile file, my hope is that someone with more experience in the command line and PS1 will be able to see what I'm doing incorrectly, thanks for reading.

   # This function builds your prompt. It is called below
  function prompt {
    # Define some local colors
    local         RED="\[\033[0;31m\]" # This syntax is some weird bash color thing I never
    local   LIGHT_RED="\[\033[1;31m\]" # really understood
    local        CHAR="\[⚡️ 👻 ⚡️\]"
    local   BLUE="\[\e[0;49;34m\]"

    # ♥ ☆ - Keeping some cool ASCII Characters for reference

    # Here is where we actually export the PS1 Variable which stores the text for your prompt

    export PS1="\[\e]2;\u@\h\a[\[\e[37;44;1m\]\t\[\e[0m\]]$RED\$(parse_git_branch) \[\e[32m\]\W\[\e[0m\]\n\[\e[0;31m\]$CHAR \[\e[0m\]"
      PS2='> '
      PS4='+ '
    }

  # Finally call the function and our prompt is all pretty
  prompt

Upvotes: 0

Views: 271

Answers (1)

petrpulc
petrpulc

Reputation: 970

Use hard quotation marks (') if possible

... or double escape.

The real problem here is, that you need to state correctly which parts are not really printed and thus have zero character count. If you incorrectly state that the terminal control for colour change (e.g. \033[0;31m for red) has some width, lines will overflow and prompt (or part of it) can be erased by ⌫ Backspace.

You can state that the terminal codes have a zero real width by square brackets. But in reality they need to be escaped to '\[' and '\]'.

Double quotes can be used, but an extra layer of escaping needs to be added to result in "\\[" and "\\]" respectively.

Enclosing your PS1 in single quotation marks (') made pretty much all the difference.

For further reading please refer to: https://unix.stackexchange.com/questions/150492/backspace-deletes-bash-prompt

Upvotes: 1

Related Questions