Almost all Linux users have used bash at some point, but how many of them know how to customize it to their liking? Here is a screenshot of one custom bash prompt I've created, which I find much better than the default (it took me a few hours to reach this point, owing mainly to my poor skills in bash scripting):
So, other than showing useful stuff related to git repositories, it gives me a full line to type my command, and separates each command with a hyphenated separator and a newline, so I can see it clearly as I scroll upwards. The corresponding code snippet is (put it at the end of your
Rather than explaining the code (which I'm not very clear about myself), I'll point you to some links which helped me along the way:
1. https://wiki.archlinux.org/index.php/Color_Bash_Prompt
2. http://superuser.com/questions/187455/right-align-part-of-prompt
3. https://gist.github.com/306785/4da3e39fc012475140fdf33ef0f6bc0a6e7c04a5
-----
Liked this article? How about following me or Rajat on Twitter?
Click the image to view it properly. |
~/.bashrc
if you wish to use it):PS1='\[\033[01;34m\]\$\[\033[00m\] ' parse_git_dirty () { [[ $(__git_ps1) != "" ]] && [[ $(git status 2> /dev/null | tail -n1) != "nothing to commit (working directory clean)" ]] && echo "*" } print_pre_prompt () { # the first part of the prompt, i.e., the user's name local PS1U=$USER # the second part of the prompt, i.e., the present working directory local PS1WD="${PWD}" if [[ $PS1WD = "$HOME" ]]; then PS1WD=\~; fi if [[ $PS1WD = *"$HOME"/* ]]; then PS1WD=\~${PS1WD#$HOME}; fi # the fourth and last part of the prompt, i.e., the git stuff local git_dirty=$(parse_git_dirty) local git_dirty_color="\033[1;31m" local git_clean_color="\033[1;30m" if [[ $git_dirty = "*" ]]; then local git_color=$git_dirty_color; else local git_color=$git_clean_color; fi local PS1R=`__git_ps1 "%s"`$git_dirty # the third part of the prompt, i.e., the separator consisting of hyphens local PS1SEP="" while [ $(echo ${#PS1U}+${#PS1SEP}+${#PS1WD}+${#PS1R}+3 | bc) -lt $COLUMNS ]; do PS1SEP=$PS1SEP- done # putting it all together printf "\n\033[1;32m%s \033[1;34m%s \033[0;36m%s $git_color%s" "$PS1U" "$PS1WD" "$PS1SEP" "$PS1R" } PROMPT_COMMAND=print_pre_prompt
Rather than explaining the code (which I'm not very clear about myself), I'll point you to some links which helped me along the way:
1. https://wiki.archlinux.org/index.php/Color_Bash_Prompt
2. http://superuser.com/questions/187455/right-align-part-of-prompt
3. https://gist.github.com/306785/4da3e39fc012475140fdf33ef0f6bc0a6e7c04a5
-----
Liked this article? How about following me or Rajat on Twitter?
This one was a great example. I have known that it is possible to customize bash outputs to some extent like the color, but never thought it to be useful enough to explore. This example shows how it can be pretty useful. Showing certain states, through the colors can be pretty handy :)
ReplyDelete