Why does my custom bash prompt glitch when browsing the history [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite













This question already has an answer here:



  • Why is my bash prompt getting bugged when I browse the history? [duplicate]

    2 answers



I have set up a custom bash prompt so I can see stuff like the current git branch, npm package version info, the virtual env I'm in, etc. However, when i press the up arrow key to browse the history, the prompt glitches:



Here's what it is normally:



enter image description here



However, when I browse the history, the space between the lambda sign and the command disappears:



enter image description here



It only happens sometimes, and I don't know why. I guess I'm just being picky about a small space, but after spending a lot of time configuring my bash prompt I want it to look perfect.



This is what my .bashrc looks like. I'm using Git Bash for Windows, btw, if that makes a difference:



bashPrompt() 
SYMBOL="λ"

COUNT=(`find ./ -maxdepth 1 -name "package.json"`)

if [ $#COUNT[@] -gt 0 ]; then
NPM_PACKAGE_INFO="($(node -p -e "require('./package.json').version"))"
else
NPM_PACKAGE_INFO=""
fi

if [ ! -z "$CONDA_DEFAULT_ENV" ]; then
ENV=" ($CONDA_DEFAULT_ENV)"
else
ENV=""
fi

if [ -d .git ]; then
if [ -z "$(git status --porcelain)" ]; then
echo "[[e[0;36;40mu@H][e[0;37;40m]][e[1;34;40m$ENV] [e[1;31;40mw][e[1;32;40m$(__git_ps1)] [e[0;37;40m$NPM_PACKAGE_INFO]n[e[0;37;40m$SYMBOL] "
else
echo "[[e[0;36;40mu@H][e[0;37;40m]][e[1;34;40m$ENV] [e[1;31;40mw][e[1;33;40m$(__git_ps1)] [e[0;37;40m$NPM_PACKAGE_INFO]n[e[0;37;40m$SYMBOL] "
fi
else
echo "[[e[0;36;40mu@H][e[0;37;40m]][e[1;34;40m$ENV] [e[1;31;40mw] [e[0;37;40m$NPM_PACKAGE_INFO]n[e[0;37;40m$SYMBOL] "
fi;




PROMPT_COMMAND='PS1="$(bashPrompt)"'



Thanks in advance.



EDIT: btw, sorry if this is a duplicate, a lot of people said to add the [ and ] but it still doesn't work.










share|improve this question













marked as duplicate by Jeff Schaller, RalfFriedl, G-Man, Filipe Brandenburger, JigglyNaga Nov 25 at 8:30


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1




    [ and ] should only enclose things that occupy zero space on the screen, such as escape sequences that change the text color. Please try moving everything that takes up space, such as u@H and $SYMBOL, out of those braces.
    – Mark Plotnick
    Nov 23 at 21:06











  • What if it varies in length. It could be zero length or it could be more? What then? Do I keep the [ and ]
    – Yash Patel
    Nov 23 at 22:29







  • 1




    Zero length as in "the string may have zero characters in it" is fine and should be outside of [ and ]. I should have said "[ and ] should be used to enclose a string of n (n > 0) characters that will take up zero space on the screen. If you do not use [ and ], bash will assume they take up n character positions on the screen, and redisplay may be messed up."
    – Mark Plotnick
    Nov 23 at 23:09










  • @Mark that is an answer. You could write it up better though. If it was an answer, then I would have done a few edits, for you.
    – ctrl-alt-delor
    Nov 24 at 0:11







  • 1




    This is unix.stackexchange.com/questions/317734 again.
    – JdeBP
    Nov 24 at 0:59














up vote
0
down vote

favorite













This question already has an answer here:



  • Why is my bash prompt getting bugged when I browse the history? [duplicate]

    2 answers



I have set up a custom bash prompt so I can see stuff like the current git branch, npm package version info, the virtual env I'm in, etc. However, when i press the up arrow key to browse the history, the prompt glitches:



Here's what it is normally:



enter image description here



However, when I browse the history, the space between the lambda sign and the command disappears:



enter image description here



It only happens sometimes, and I don't know why. I guess I'm just being picky about a small space, but after spending a lot of time configuring my bash prompt I want it to look perfect.



This is what my .bashrc looks like. I'm using Git Bash for Windows, btw, if that makes a difference:



bashPrompt() 
SYMBOL="λ"

COUNT=(`find ./ -maxdepth 1 -name "package.json"`)

if [ $#COUNT[@] -gt 0 ]; then
NPM_PACKAGE_INFO="($(node -p -e "require('./package.json').version"))"
else
NPM_PACKAGE_INFO=""
fi

if [ ! -z "$CONDA_DEFAULT_ENV" ]; then
ENV=" ($CONDA_DEFAULT_ENV)"
else
ENV=""
fi

if [ -d .git ]; then
if [ -z "$(git status --porcelain)" ]; then
echo "[[e[0;36;40mu@H][e[0;37;40m]][e[1;34;40m$ENV] [e[1;31;40mw][e[1;32;40m$(__git_ps1)] [e[0;37;40m$NPM_PACKAGE_INFO]n[e[0;37;40m$SYMBOL] "
else
echo "[[e[0;36;40mu@H][e[0;37;40m]][e[1;34;40m$ENV] [e[1;31;40mw][e[1;33;40m$(__git_ps1)] [e[0;37;40m$NPM_PACKAGE_INFO]n[e[0;37;40m$SYMBOL] "
fi
else
echo "[[e[0;36;40mu@H][e[0;37;40m]][e[1;34;40m$ENV] [e[1;31;40mw] [e[0;37;40m$NPM_PACKAGE_INFO]n[e[0;37;40m$SYMBOL] "
fi;




PROMPT_COMMAND='PS1="$(bashPrompt)"'



Thanks in advance.



EDIT: btw, sorry if this is a duplicate, a lot of people said to add the [ and ] but it still doesn't work.










share|improve this question













marked as duplicate by Jeff Schaller, RalfFriedl, G-Man, Filipe Brandenburger, JigglyNaga Nov 25 at 8:30


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1




    [ and ] should only enclose things that occupy zero space on the screen, such as escape sequences that change the text color. Please try moving everything that takes up space, such as u@H and $SYMBOL, out of those braces.
    – Mark Plotnick
    Nov 23 at 21:06











  • What if it varies in length. It could be zero length or it could be more? What then? Do I keep the [ and ]
    – Yash Patel
    Nov 23 at 22:29







  • 1




    Zero length as in "the string may have zero characters in it" is fine and should be outside of [ and ]. I should have said "[ and ] should be used to enclose a string of n (n > 0) characters that will take up zero space on the screen. If you do not use [ and ], bash will assume they take up n character positions on the screen, and redisplay may be messed up."
    – Mark Plotnick
    Nov 23 at 23:09










  • @Mark that is an answer. You could write it up better though. If it was an answer, then I would have done a few edits, for you.
    – ctrl-alt-delor
    Nov 24 at 0:11







  • 1




    This is unix.stackexchange.com/questions/317734 again.
    – JdeBP
    Nov 24 at 0:59












up vote
0
down vote

favorite









up vote
0
down vote

favorite












This question already has an answer here:



  • Why is my bash prompt getting bugged when I browse the history? [duplicate]

    2 answers



I have set up a custom bash prompt so I can see stuff like the current git branch, npm package version info, the virtual env I'm in, etc. However, when i press the up arrow key to browse the history, the prompt glitches:



Here's what it is normally:



enter image description here



However, when I browse the history, the space between the lambda sign and the command disappears:



enter image description here



It only happens sometimes, and I don't know why. I guess I'm just being picky about a small space, but after spending a lot of time configuring my bash prompt I want it to look perfect.



This is what my .bashrc looks like. I'm using Git Bash for Windows, btw, if that makes a difference:



bashPrompt() 
SYMBOL="λ"

COUNT=(`find ./ -maxdepth 1 -name "package.json"`)

if [ $#COUNT[@] -gt 0 ]; then
NPM_PACKAGE_INFO="($(node -p -e "require('./package.json').version"))"
else
NPM_PACKAGE_INFO=""
fi

if [ ! -z "$CONDA_DEFAULT_ENV" ]; then
ENV=" ($CONDA_DEFAULT_ENV)"
else
ENV=""
fi

if [ -d .git ]; then
if [ -z "$(git status --porcelain)" ]; then
echo "[[e[0;36;40mu@H][e[0;37;40m]][e[1;34;40m$ENV] [e[1;31;40mw][e[1;32;40m$(__git_ps1)] [e[0;37;40m$NPM_PACKAGE_INFO]n[e[0;37;40m$SYMBOL] "
else
echo "[[e[0;36;40mu@H][e[0;37;40m]][e[1;34;40m$ENV] [e[1;31;40mw][e[1;33;40m$(__git_ps1)] [e[0;37;40m$NPM_PACKAGE_INFO]n[e[0;37;40m$SYMBOL] "
fi
else
echo "[[e[0;36;40mu@H][e[0;37;40m]][e[1;34;40m$ENV] [e[1;31;40mw] [e[0;37;40m$NPM_PACKAGE_INFO]n[e[0;37;40m$SYMBOL] "
fi;




PROMPT_COMMAND='PS1="$(bashPrompt)"'



Thanks in advance.



EDIT: btw, sorry if this is a duplicate, a lot of people said to add the [ and ] but it still doesn't work.










share|improve this question














This question already has an answer here:



  • Why is my bash prompt getting bugged when I browse the history? [duplicate]

    2 answers



I have set up a custom bash prompt so I can see stuff like the current git branch, npm package version info, the virtual env I'm in, etc. However, when i press the up arrow key to browse the history, the prompt glitches:



Here's what it is normally:



enter image description here



However, when I browse the history, the space between the lambda sign and the command disappears:



enter image description here



It only happens sometimes, and I don't know why. I guess I'm just being picky about a small space, but after spending a lot of time configuring my bash prompt I want it to look perfect.



This is what my .bashrc looks like. I'm using Git Bash for Windows, btw, if that makes a difference:



bashPrompt() 
SYMBOL="λ"

COUNT=(`find ./ -maxdepth 1 -name "package.json"`)

if [ $#COUNT[@] -gt 0 ]; then
NPM_PACKAGE_INFO="($(node -p -e "require('./package.json').version"))"
else
NPM_PACKAGE_INFO=""
fi

if [ ! -z "$CONDA_DEFAULT_ENV" ]; then
ENV=" ($CONDA_DEFAULT_ENV)"
else
ENV=""
fi

if [ -d .git ]; then
if [ -z "$(git status --porcelain)" ]; then
echo "[[e[0;36;40mu@H][e[0;37;40m]][e[1;34;40m$ENV] [e[1;31;40mw][e[1;32;40m$(__git_ps1)] [e[0;37;40m$NPM_PACKAGE_INFO]n[e[0;37;40m$SYMBOL] "
else
echo "[[e[0;36;40mu@H][e[0;37;40m]][e[1;34;40m$ENV] [e[1;31;40mw][e[1;33;40m$(__git_ps1)] [e[0;37;40m$NPM_PACKAGE_INFO]n[e[0;37;40m$SYMBOL] "
fi
else
echo "[[e[0;36;40mu@H][e[0;37;40m]][e[1;34;40m$ENV] [e[1;31;40mw] [e[0;37;40m$NPM_PACKAGE_INFO]n[e[0;37;40m$SYMBOL] "
fi;




PROMPT_COMMAND='PS1="$(bashPrompt)"'



Thanks in advance.



EDIT: btw, sorry if this is a duplicate, a lot of people said to add the [ and ] but it still doesn't work.





This question already has an answer here:



  • Why is my bash prompt getting bugged when I browse the history? [duplicate]

    2 answers







bash prompt






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 at 19:00









Yash Patel

33




33




marked as duplicate by Jeff Schaller, RalfFriedl, G-Man, Filipe Brandenburger, JigglyNaga Nov 25 at 8:30


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Jeff Schaller, RalfFriedl, G-Man, Filipe Brandenburger, JigglyNaga Nov 25 at 8:30


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









  • 1




    [ and ] should only enclose things that occupy zero space on the screen, such as escape sequences that change the text color. Please try moving everything that takes up space, such as u@H and $SYMBOL, out of those braces.
    – Mark Plotnick
    Nov 23 at 21:06











  • What if it varies in length. It could be zero length or it could be more? What then? Do I keep the [ and ]
    – Yash Patel
    Nov 23 at 22:29







  • 1




    Zero length as in "the string may have zero characters in it" is fine and should be outside of [ and ]. I should have said "[ and ] should be used to enclose a string of n (n > 0) characters that will take up zero space on the screen. If you do not use [ and ], bash will assume they take up n character positions on the screen, and redisplay may be messed up."
    – Mark Plotnick
    Nov 23 at 23:09










  • @Mark that is an answer. You could write it up better though. If it was an answer, then I would have done a few edits, for you.
    – ctrl-alt-delor
    Nov 24 at 0:11







  • 1




    This is unix.stackexchange.com/questions/317734 again.
    – JdeBP
    Nov 24 at 0:59












  • 1




    [ and ] should only enclose things that occupy zero space on the screen, such as escape sequences that change the text color. Please try moving everything that takes up space, such as u@H and $SYMBOL, out of those braces.
    – Mark Plotnick
    Nov 23 at 21:06











  • What if it varies in length. It could be zero length or it could be more? What then? Do I keep the [ and ]
    – Yash Patel
    Nov 23 at 22:29







  • 1




    Zero length as in "the string may have zero characters in it" is fine and should be outside of [ and ]. I should have said "[ and ] should be used to enclose a string of n (n > 0) characters that will take up zero space on the screen. If you do not use [ and ], bash will assume they take up n character positions on the screen, and redisplay may be messed up."
    – Mark Plotnick
    Nov 23 at 23:09










  • @Mark that is an answer. You could write it up better though. If it was an answer, then I would have done a few edits, for you.
    – ctrl-alt-delor
    Nov 24 at 0:11







  • 1




    This is unix.stackexchange.com/questions/317734 again.
    – JdeBP
    Nov 24 at 0:59







1




1




[ and ] should only enclose things that occupy zero space on the screen, such as escape sequences that change the text color. Please try moving everything that takes up space, such as u@H and $SYMBOL, out of those braces.
– Mark Plotnick
Nov 23 at 21:06





[ and ] should only enclose things that occupy zero space on the screen, such as escape sequences that change the text color. Please try moving everything that takes up space, such as u@H and $SYMBOL, out of those braces.
– Mark Plotnick
Nov 23 at 21:06













What if it varies in length. It could be zero length or it could be more? What then? Do I keep the [ and ]
– Yash Patel
Nov 23 at 22:29





What if it varies in length. It could be zero length or it could be more? What then? Do I keep the [ and ]
– Yash Patel
Nov 23 at 22:29





1




1




Zero length as in "the string may have zero characters in it" is fine and should be outside of [ and ]. I should have said "[ and ] should be used to enclose a string of n (n > 0) characters that will take up zero space on the screen. If you do not use [ and ], bash will assume they take up n character positions on the screen, and redisplay may be messed up."
– Mark Plotnick
Nov 23 at 23:09




Zero length as in "the string may have zero characters in it" is fine and should be outside of [ and ]. I should have said "[ and ] should be used to enclose a string of n (n > 0) characters that will take up zero space on the screen. If you do not use [ and ], bash will assume they take up n character positions on the screen, and redisplay may be messed up."
– Mark Plotnick
Nov 23 at 23:09












@Mark that is an answer. You could write it up better though. If it was an answer, then I would have done a few edits, for you.
– ctrl-alt-delor
Nov 24 at 0:11





@Mark that is an answer. You could write it up better though. If it was an answer, then I would have done a few edits, for you.
– ctrl-alt-delor
Nov 24 at 0:11





1




1




This is unix.stackexchange.com/questions/317734 again.
– JdeBP
Nov 24 at 0:59




This is unix.stackexchange.com/questions/317734 again.
– JdeBP
Nov 24 at 0:59










1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










One of your string reads:



"[[e[0;36;40mu@H][e[0;37;40m]][e[1;34;40m$ENV] [e[1;31;40mw][e[1;32;40m$(__git_ps1)] [e[0;37;40m$NPM_PACKAGE_INFO]n[e[0;37;40m$SYMBOL] "


In which you are enclosing every text inside [] pairs.
If you remove the newline n and try that PS1:



PS1="[[e[0;36;40mu@H][e[0;37;40m]][e[1;34;40m$ENV] [e[1;31;40mw][e[1;32;40m$(__git_ps1)] [e[0;37;40m$NPM_PACKAGE_INFO] n[e[0;37;40m$SYMBOL] "


You will find that most of the prompt will be replaced by the history commands if you press up (up arrow).



Just place text that is meant to use line space out of those brackets:



PS1="[[e[0;36;40m]u@H [e[0;37;40m]][e[1;34;40m]$ENV [e[1;31;40m]w[e[1;32;40m]$(__git_ps1) [e[0;37;40m]$NPM_PACKAGE_INFO n[e[0;37;40m]$SYMBOL "


And try again.



After that works correctly, add the newline n back and try again.



If still in trouble, update bash to 4.4.18 (present built) or similar.






share|improve this answer



























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    One of your string reads:



    "[[e[0;36;40mu@H][e[0;37;40m]][e[1;34;40m$ENV] [e[1;31;40mw][e[1;32;40m$(__git_ps1)] [e[0;37;40m$NPM_PACKAGE_INFO]n[e[0;37;40m$SYMBOL] "


    In which you are enclosing every text inside [] pairs.
    If you remove the newline n and try that PS1:



    PS1="[[e[0;36;40mu@H][e[0;37;40m]][e[1;34;40m$ENV] [e[1;31;40mw][e[1;32;40m$(__git_ps1)] [e[0;37;40m$NPM_PACKAGE_INFO] n[e[0;37;40m$SYMBOL] "


    You will find that most of the prompt will be replaced by the history commands if you press up (up arrow).



    Just place text that is meant to use line space out of those brackets:



    PS1="[[e[0;36;40m]u@H [e[0;37;40m]][e[1;34;40m]$ENV [e[1;31;40m]w[e[1;32;40m]$(__git_ps1) [e[0;37;40m]$NPM_PACKAGE_INFO n[e[0;37;40m]$SYMBOL "


    And try again.



    After that works correctly, add the newline n back and try again.



    If still in trouble, update bash to 4.4.18 (present built) or similar.






    share|improve this answer
























      up vote
      0
      down vote



      accepted










      One of your string reads:



      "[[e[0;36;40mu@H][e[0;37;40m]][e[1;34;40m$ENV] [e[1;31;40mw][e[1;32;40m$(__git_ps1)] [e[0;37;40m$NPM_PACKAGE_INFO]n[e[0;37;40m$SYMBOL] "


      In which you are enclosing every text inside [] pairs.
      If you remove the newline n and try that PS1:



      PS1="[[e[0;36;40mu@H][e[0;37;40m]][e[1;34;40m$ENV] [e[1;31;40mw][e[1;32;40m$(__git_ps1)] [e[0;37;40m$NPM_PACKAGE_INFO] n[e[0;37;40m$SYMBOL] "


      You will find that most of the prompt will be replaced by the history commands if you press up (up arrow).



      Just place text that is meant to use line space out of those brackets:



      PS1="[[e[0;36;40m]u@H [e[0;37;40m]][e[1;34;40m]$ENV [e[1;31;40m]w[e[1;32;40m]$(__git_ps1) [e[0;37;40m]$NPM_PACKAGE_INFO n[e[0;37;40m]$SYMBOL "


      And try again.



      After that works correctly, add the newline n back and try again.



      If still in trouble, update bash to 4.4.18 (present built) or similar.






      share|improve this answer






















        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        One of your string reads:



        "[[e[0;36;40mu@H][e[0;37;40m]][e[1;34;40m$ENV] [e[1;31;40mw][e[1;32;40m$(__git_ps1)] [e[0;37;40m$NPM_PACKAGE_INFO]n[e[0;37;40m$SYMBOL] "


        In which you are enclosing every text inside [] pairs.
        If you remove the newline n and try that PS1:



        PS1="[[e[0;36;40mu@H][e[0;37;40m]][e[1;34;40m$ENV] [e[1;31;40mw][e[1;32;40m$(__git_ps1)] [e[0;37;40m$NPM_PACKAGE_INFO] n[e[0;37;40m$SYMBOL] "


        You will find that most of the prompt will be replaced by the history commands if you press up (up arrow).



        Just place text that is meant to use line space out of those brackets:



        PS1="[[e[0;36;40m]u@H [e[0;37;40m]][e[1;34;40m]$ENV [e[1;31;40m]w[e[1;32;40m]$(__git_ps1) [e[0;37;40m]$NPM_PACKAGE_INFO n[e[0;37;40m]$SYMBOL "


        And try again.



        After that works correctly, add the newline n back and try again.



        If still in trouble, update bash to 4.4.18 (present built) or similar.






        share|improve this answer












        One of your string reads:



        "[[e[0;36;40mu@H][e[0;37;40m]][e[1;34;40m$ENV] [e[1;31;40mw][e[1;32;40m$(__git_ps1)] [e[0;37;40m$NPM_PACKAGE_INFO]n[e[0;37;40m$SYMBOL] "


        In which you are enclosing every text inside [] pairs.
        If you remove the newline n and try that PS1:



        PS1="[[e[0;36;40mu@H][e[0;37;40m]][e[1;34;40m$ENV] [e[1;31;40mw][e[1;32;40m$(__git_ps1)] [e[0;37;40m$NPM_PACKAGE_INFO] n[e[0;37;40m$SYMBOL] "


        You will find that most of the prompt will be replaced by the history commands if you press up (up arrow).



        Just place text that is meant to use line space out of those brackets:



        PS1="[[e[0;36;40m]u@H [e[0;37;40m]][e[1;34;40m]$ENV [e[1;31;40m]w[e[1;32;40m]$(__git_ps1) [e[0;37;40m]$NPM_PACKAGE_INFO n[e[0;37;40m]$SYMBOL "


        And try again.



        After that works correctly, add the newline n back and try again.



        If still in trouble, update bash to 4.4.18 (present built) or similar.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 24 at 23:31









        Isaac

        9,91111445




        9,91111445












            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?