colorize hostname in command line prompt [duplicate]

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











up vote
2
down vote

favorite













This question already has an answer here:



  • Terminal prompt not wrapping correctly

    6 answers



I wish to set a custom color for the hostname portion of my command line prompt (in bash).



In my .bashrc file, the default command line prompt is:



PS1='[u@h W]$ '


To colorize the hostname only, I came up with this prompt:



PS1='[u@e[40;36mhe[0m W]$ '


I used different colors for each host I work on. The colorizing part seems to be working as expected. However, with the prompt above, navigation on the command line becomes dysfunctional. For example, at times I cannot move the cursor to the beginning or end of the line. If I switch back to the default prompt, everything works correctly again.



What's wrong with my colorized prompt? How can I properly specify a color for the hostname only?



It should not matter for this question, but I'm running Arch Linux KDE and I work in Konsole.







share|improve this question













marked as duplicate by Mikel, Community♦ May 8 at 1:38


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.


















    up vote
    2
    down vote

    favorite













    This question already has an answer here:



    • Terminal prompt not wrapping correctly

      6 answers



    I wish to set a custom color for the hostname portion of my command line prompt (in bash).



    In my .bashrc file, the default command line prompt is:



    PS1='[u@h W]$ '


    To colorize the hostname only, I came up with this prompt:



    PS1='[u@e[40;36mhe[0m W]$ '


    I used different colors for each host I work on. The colorizing part seems to be working as expected. However, with the prompt above, navigation on the command line becomes dysfunctional. For example, at times I cannot move the cursor to the beginning or end of the line. If I switch back to the default prompt, everything works correctly again.



    What's wrong with my colorized prompt? How can I properly specify a color for the hostname only?



    It should not matter for this question, but I'm running Arch Linux KDE and I work in Konsole.







    share|improve this question













    marked as duplicate by Mikel, Community♦ May 8 at 1:38


    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.
















      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite












      This question already has an answer here:



      • Terminal prompt not wrapping correctly

        6 answers



      I wish to set a custom color for the hostname portion of my command line prompt (in bash).



      In my .bashrc file, the default command line prompt is:



      PS1='[u@h W]$ '


      To colorize the hostname only, I came up with this prompt:



      PS1='[u@e[40;36mhe[0m W]$ '


      I used different colors for each host I work on. The colorizing part seems to be working as expected. However, with the prompt above, navigation on the command line becomes dysfunctional. For example, at times I cannot move the cursor to the beginning or end of the line. If I switch back to the default prompt, everything works correctly again.



      What's wrong with my colorized prompt? How can I properly specify a color for the hostname only?



      It should not matter for this question, but I'm running Arch Linux KDE and I work in Konsole.







      share|improve this question














      This question already has an answer here:



      • Terminal prompt not wrapping correctly

        6 answers



      I wish to set a custom color for the hostname portion of my command line prompt (in bash).



      In my .bashrc file, the default command line prompt is:



      PS1='[u@h W]$ '


      To colorize the hostname only, I came up with this prompt:



      PS1='[u@e[40;36mhe[0m W]$ '


      I used different colors for each host I work on. The colorizing part seems to be working as expected. However, with the prompt above, navigation on the command line becomes dysfunctional. For example, at times I cannot move the cursor to the beginning or end of the line. If I switch back to the default prompt, everything works correctly again.



      What's wrong with my colorized prompt? How can I properly specify a color for the hostname only?



      It should not matter for this question, but I'm running Arch Linux KDE and I work in Konsole.





      This question already has an answer here:



      • Terminal prompt not wrapping correctly

        6 answers









      share|improve this question












      share|improve this question




      share|improve this question








      edited May 8 at 3:42
























      asked May 8 at 0:28









      MountainX

      4,4092367115




      4,4092367115




      marked as duplicate by Mikel, Community♦ May 8 at 1:38


      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 Mikel, Community♦ May 8 at 1:38


      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 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          Background



          After the expansion is done for special prompt sequences, every character remaining in the prompt is counted in order to calculate the length of the prompt.



          Problem



          Since you've added the color sequences, which in fact shouldn't be counted for the length of a prompt, bash now actually thinks that your prompt is longer than it really is.

          Because of that, bash can't know where is the beginning or the end of the line and that creates mentioned dysfunctionalities.



          Solution



          From bash manual:




          [ Begin a sequence of non-printing characters, which could be used to 
          embed a terminal control sequence into the prompt

          ] End a sequence of non-printing characters



          Just add these around your color escape sequences so that they aren't counted for the prompt length.






          share|improve this answer






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote



            accepted










            Background



            After the expansion is done for special prompt sequences, every character remaining in the prompt is counted in order to calculate the length of the prompt.



            Problem



            Since you've added the color sequences, which in fact shouldn't be counted for the length of a prompt, bash now actually thinks that your prompt is longer than it really is.

            Because of that, bash can't know where is the beginning or the end of the line and that creates mentioned dysfunctionalities.



            Solution



            From bash manual:




            [ Begin a sequence of non-printing characters, which could be used to 
            embed a terminal control sequence into the prompt

            ] End a sequence of non-printing characters



            Just add these around your color escape sequences so that they aren't counted for the prompt length.






            share|improve this answer



























              up vote
              2
              down vote



              accepted










              Background



              After the expansion is done for special prompt sequences, every character remaining in the prompt is counted in order to calculate the length of the prompt.



              Problem



              Since you've added the color sequences, which in fact shouldn't be counted for the length of a prompt, bash now actually thinks that your prompt is longer than it really is.

              Because of that, bash can't know where is the beginning or the end of the line and that creates mentioned dysfunctionalities.



              Solution



              From bash manual:




              [ Begin a sequence of non-printing characters, which could be used to 
              embed a terminal control sequence into the prompt

              ] End a sequence of non-printing characters



              Just add these around your color escape sequences so that they aren't counted for the prompt length.






              share|improve this answer

























                up vote
                2
                down vote



                accepted







                up vote
                2
                down vote



                accepted






                Background



                After the expansion is done for special prompt sequences, every character remaining in the prompt is counted in order to calculate the length of the prompt.



                Problem



                Since you've added the color sequences, which in fact shouldn't be counted for the length of a prompt, bash now actually thinks that your prompt is longer than it really is.

                Because of that, bash can't know where is the beginning or the end of the line and that creates mentioned dysfunctionalities.



                Solution



                From bash manual:




                [ Begin a sequence of non-printing characters, which could be used to 
                embed a terminal control sequence into the prompt

                ] End a sequence of non-printing characters



                Just add these around your color escape sequences so that they aren't counted for the prompt length.






                share|improve this answer















                Background



                After the expansion is done for special prompt sequences, every character remaining in the prompt is counted in order to calculate the length of the prompt.



                Problem



                Since you've added the color sequences, which in fact shouldn't be counted for the length of a prompt, bash now actually thinks that your prompt is longer than it really is.

                Because of that, bash can't know where is the beginning or the end of the line and that creates mentioned dysfunctionalities.



                Solution



                From bash manual:




                [ Begin a sequence of non-printing characters, which could be used to 
                embed a terminal control sequence into the prompt

                ] End a sequence of non-printing characters



                Just add these around your color escape sequences so that they aren't counted for the prompt length.







                share|improve this answer















                share|improve this answer



                share|improve this answer








                edited May 8 at 1:31


























                answered May 8 at 0:55









                Iskustvo

                667118




                667118












                    Popular posts from this blog

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

                    Bahrain

                    Postfix configuration issue with fips on centos 7; mailgun relay