Skip to content

Commit

Permalink
Merge pull request HarshCasper#706 from avinal/add-custom-prompt
Browse files Browse the repository at this point in the history
Add custom prompt script for Bash
  • Loading branch information
HarshCasper authored Jan 23, 2021
2 parents 0d188d5 + cea3186 commit 34eb768
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 0 deletions.
46 changes: 46 additions & 0 deletions Bash/Custom_Prompt/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Custom Command Prompt on Bash

[![Bash Shell](https://img.shields.io/static/v1?label=MADE%20WITH&message=BASH&color=red&style=for-the-badge&logo=gnu-bash)](https://shields.io/)
[![Linux](https://img.shields.io/static/v1?label=MADE%20FOR&message=LINUX&color=red&style=for-the-badge&logo=linux)](https://shields.io/)

By default Bash command prompts are boring and show no extra information. You can tweak your command prompt to show a variety of information such as `git` and virtual environment, command execution time, previous command status, current directory, SSH session and many more can be added.

## Prerequisite

You must have `git` installed in order to use this script. If you haven't already follow the step below

```bash
sudo apt-get update && sudo apt-get install -y git
```

## Setup Instructions

- Open your `.bashrc` in `nano` or `vim` or any editor of your choice.

```bash
sudo nano ~/.bashrc
```

- Goto the last of your `.bashrc` and paste everything from the file [custom_prompt.sh](custom_prompt.sh). *Do not change anything else until you know. You can however customise the colors of your choice*

- Save your `.bashrc` and exit

```bash
# for nano
CTRL + O
CTRL +X

# for vim
:w
:q
```

- Run the following command to load new configuration.

```bash
source ~/.bashrc
```

## Author(s)

Made by [Avinal Kumar](https://github.com/avinal)
102 changes: 102 additions & 0 deletions Bash/Custom_Prompt/custom_prompt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@

# colors - you may add more colors
WHITE='\[\033[1;37m\]'
LIGHTGRAY='\[\033[0;37m\]'
GRAY='\[\033[1;30m\]'
BLACK='\[\033[0;30m\]'
RED='\[\033[0;31m\]'
LIGHTRED='\[\033[1;31m\]'
GREEN='\[\033[0;32m\]'
LIGHTGREEN='\[\033[1;32m\]'
BROWN='\[\033[0;33m\]'
YELLOW='\[\033[1;33m\]'
BLUE='\[\033[0;34m\]'
LIGHTBLUE='\[\033[1;34m\]'
PURPLE='\[\033[0;35m\]'
PINK='\[\033[1;35m\]'
CYAN='\[\033[0;36m\]'
LIGHTCYAN='\[\033[1;36m\]'
DEFAULT='\[\033[0m\]'
VIOLET='\[\033[01;35m\]'

# change colors for different components here
cARROW=$VIOLET
cLINES=$GRAY #Lines and Arrow
cBRACKETS=$GRAY # Brackets around each data item
cERROR=$LIGHTRED # Error block when previous command did not return 0
cSUCCESS=$GREEN # When last command ran successfully and return 0
cTIME=$LIGHTGRAY # The current time
cUSR=$LIGHTBLUE # Color of user
cUHS=$CYAN # Color of the user and hostname separator, probably '@'
cSSH=$PINK # Color for brackets if session is an SSH session
cHST=$LIGHTGREEN # Color of hostname
cRWN=$RED # Color of root warning
cPWD=$BLUE # Color of current directory
cCMD=$DEFAULT # Color of the command you type

# seperator, default '@' you may use any ASCII character
UHS="@"

# parse git branch - install git beforehand
function parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}

function command_prompt() {
# get error code of previous command
PREVIOUS=$?

# insert a new line to clear space from previous command
PS1="\n"

# First line - arrow wrap around and color
PS1="${PS1}${cARROW}\342\224\214\342\224\200"

# First line - show if previous command was successful
if [ $PREVIOUS -ne 0 ] ; then
PS1="${PS1}${cBRACKETS}[${cERROR}:(${cBRACKETS}]${cLINES}\342\224\200"
else
PS1="${PS1}${cBRACKETS}[${cSUCCESS}:)${cBRACKETS}]${cLINES}\342\224\200"
fi

# First line - current time
PS1="${PS1}${cBRACKETS}[${cTIME}\D{%H:%M}${cBRACKETS}]${cLINES}\342\224\200"

# First line - set color of brackets if its an SSH session
if [[ $SSH_CLIENT ]] || [[ $SSH2_CLIENT ]]; then
sesClr="$cSSH"
else
sesClr="$cBRACKETS"
fi

# First line - Dont display user if its root
if [ $EUID -eq 0 ] ; then
PS1="${PS1}${sesClr}[${cRWN}!"
else
PS1="${PS1}${sesClr}[${cUHS}\u"
fi

# First line - Host session
PS1="${PS1}${cUHS}${UHS}\h${sesClr}]${cLINES}\342\224\200"

# First line - current Directory
PS1="${PS1}[${cPWD}\w${cBRACKETS}]"

#First line - Conda and Git information
if [[ ! -z "$CONDA_DEFAULT_ENV" ]]; then
PS1="${PS1} ${cSUCCESS}$(parse_git_branch) ${cBRACKETS}venv:${VIOLET}${CONDA_DEFAULT_ENV}"
else
PS1="${PS1} ${cSUCCESS}$(parse_git_branch)"
fi

# Second line - arrow ending
PS1="${PS1}\n${cARROW}\342\224\224\342\224\200\342\224\200> ${cCMD}"
}

# load your custom prompt
function load_prompt () {
PROMPT_COMMAND=command_prompt
export PS1 PROMPT_COMMAND
}

load_prompt

0 comments on commit 34eb768

Please sign in to comment.