Created
March 6, 2020 23:17
-
-
Save iwalton3/618af4ea3f81dd4352f934e23c7fadfe to your computer and use it in GitHub Desktop.
BASH Prompt and Aliases
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#BEGIN prompt code | |
function makePrompt { | |
local pred="\[\033[0;31m\]" | |
local pyellow="\[\033[1;33m\]" | |
bold=$'\e[1m'; underline=$'\e[4m'; dim=$'\e[2m'; strickthrough=$'\e[9m'; blink=$'\e[5m'; reverse=$'\e[7m'; hidden=$'\e[8m'; normal=$'\e[0m'; black=$'\e[30m'; red=$'\e[31m'; green=$'\e[32m'; orange=$'\e[33m'; blue=$'\e[34m'; purple=$'\e[35m'; aqua=$'\e[36m'; gray=$'\e[37m'; darkgray=$'\e[90m'; lightred=$'\e[91m'; lightgreen=$'\e[92m'; lightyellow=$'\e[93m'; lightblue=$'\e[94m'; lightpurple=$'\e[95m'; lightaqua=$'\e[96m'; white=$'\e[97m'; default=$'\e[39m'; BLACK=$'\e[40m'; RED=$'\e[41m'; GREEN=$'\e[42m'; ORANGE=$'\e[43m'; BLUE=$'\e[44m'; PURPLE=$'\e[45m'; AQUA=$'\e[46m'; GRAY=$'\e[47m'; DARKGRAY=$'\e[100m'; LIGHTRED=$'\e[101m'; LIGHTGREEN=$'\e[102m'; LIGHTYELLOW=$'\e[103m'; LIGHTBLUE=$'\e[104m'; LIGHTPURPLE=$'\e[105m'; LIGHTAQUA=$'\e[106m'; WHITE=$'\e[107m'; DEFAULT=$'\e[49m'; | |
tabChar=$'\t' | |
if [ "$UID" != "0" ]; then | |
local SYMBOL="$" | |
local UNAME_COLOR="\[\033[1;32m\]" | |
local FINAL_COLOR="\[\033[0m\]" | |
namecolor="$green" | |
else | |
local SYMBOL="#" | |
local UNAME_COLOR="\[\033[1;31m\]" | |
local FINAL_COLOR="\[\033[0;32m\]" | |
namecolor="$red" | |
fi | |
interface=$(/usr/bin/tty | /bin/sed -e 's:/dev/::') | |
PromCurTTY=$(tty | sed -e "s/.*tty\(.*\)/\1/") | |
PromBLUE_BACK="\033[44m" | |
BLUE_BACK="\[\033[44m\]" | |
promusername="$USER" | |
promhostname="$HOSTNAME" | |
sedhome=$(sed 's/[][\.*^$(){}?+|/]/\\&/g' <<< "$HOME") | |
if which acpi > /dev/null && acpi > /dev/null 2> /dev/null | |
then | |
acpiOk="1" | |
else | |
acpiOk="0" | |
fi | |
function prompt_command { | |
returnStatus="$?" | |
errortest=$(if [[ "$returnStatus" != "0" ]]; then echo "$returnStatus "; fi) | |
if [[ "$nobar" == "true" ]] | |
then | |
return | |
fi | |
currentdir=$(pwd | sed "s/${sedhome}/~/g") | |
if [[ "$acpiOk" == "1" ]] | |
then | |
battest=$(acpi | head -n 1 | tr ' ' '\n' | grep '%' | tr -d '%,') | |
if [[ "$battest" == "100" ]] | |
then | |
battery=" " | |
elif [[ "$battest" -gt "89" ]] | |
then | |
battery="$bold$green█$normal$BLACK" | |
elif [[ "$battest" -gt "79" ]] | |
then | |
battery="$bold$green▇$normal$BLACK" | |
elif [[ "$battest" -gt "69" ]] | |
then | |
battery="$bold$green▆$normal$BLACK" | |
elif [[ "$battest" -gt "59" ]] | |
then | |
battery="$bold$green▅$normal$BLACK" | |
elif [[ "$battest" -gt "49" ]] | |
then | |
battery="$bold$green▄$normal$BLACK" | |
elif [[ "$battest" -gt "39" ]] | |
then | |
battery="$orange▃$normal$BLACK" | |
elif [[ "$battest" -gt "29" ]] | |
then | |
battery="$bold$orange▂$normal$BLACK" | |
else | |
battery="$red▁$normal$BLACK" | |
fi | |
else | |
battery=" " | |
fi | |
stopped=$(jobs -s | wc -l | tr -d " ") | |
running=$(jobs -r | wc -l | tr -d " ") | |
dateget=$(date +"%a %b %d %H:%M") | |
if [[ "$(stat -c "%b" .)" -lt "100" ]] | |
then | |
filecount="$(ls -1 | wc -l | tr -d ' ') files " | |
size="$(ls -lah | grep -m 1 total | /bin/sed "s/total //") " | |
else | |
filecount="" | |
size="" | |
fi | |
length=$(echo "$promusername@$promhostname on $interface jobs:$running$stopped $filecount$size $dateget" | wc -c) | |
fulllength=$(echo "$promusername@$promhostname on $interface jobs:$running$stopped $filecount$size$currentdir $dateget" | wc -c) | |
if [[ "$fulllength" -gt "$COLUMNS" ]] | |
then | |
spaces=$(printf "%$((COLUMNS-length))s\n") | |
echo -en "\e7\e[0;$((LINES))r\e8\n\n\e7\e[0;$((LINES-2))r\e8\e[1A" | |
echo -en "\033[s\ | |
\e[$((LINES-1));0f\033[K" | |
echo -en "$BLACK$bold$namecolor" | |
echo -en "$promusername@$promhostname$normal$white$BLACK on $bold$blue$interface$red jobs:$green$running$red$stopped$aqua $filecount$orange$size$normal$BLACK$spaces$battery $purple$dateget\033[K\n$normal$BLACK$green$currentdir\ | |
\033[K\ | |
\033[u\033[1A\033[1B$default$DEFAULT" | |
lastMode="lg" | |
else | |
echo -en "\e7\e[0;$((LINES))r\e8\n" | |
if [[ "$lastMode" == "lg" ]] | |
then | |
echo -en "\e[K" | |
fi | |
spaces=$(printf "%$((COLUMNS-fulllength))s\n") | |
echo -en "\e7\e[0;$((LINES-1))r\e8\e[1A" | |
echo -en "\033[s\ | |
\e[$((LINES));0f\033[K" | |
echo -en "$BLACK$bold$namecolor" | |
echo -en "$promusername@$promhostname$normal$white$BLACK on $bold$blue$interface$red jobs:$green$running$red$stopped$aqua $filecount$orange$size$normal$BLACK$green$currentdir $spaces$battery $purple$dateget\ | |
\033[K\ | |
\033[u\033[1A\033[1B$default$DEFAULT" | |
lastMode="sm" | |
fi | |
echo "$(date +%Y-%m-%d--%H-%M-%S)$tabChar$(hostname)$tabChar$PWD$tabChar$(history 1)" >> ~/.full_history | |
} | |
export PROMPT_COMMAND=prompt_command | |
#Custom PS1 string (prompt) | |
PS1="$pred\$errortest$pyellow\! $UNAME_COLOR$SYMBOL$FINAL_COLOR " | |
export PS1; | |
} | |
makePrompt | |
function nobar { | |
echo -en "\e7\e[0;$((LINES))r\e8\n" | |
if [[ "$nobar" == "true" ]] | |
then | |
nobar= | |
else | |
nobar=true | |
fi | |
} | |
#END prompt code | |
#BEGIN env changes | |
bind '"\e[A": history-search-backward' | |
bind '"\e[B": history-search-forward' | |
HISTSIZE=300000 | |
HISTFILESIZE=300000 | |
shopt -s histappend | |
shopt -s checkwinsize | |
alias sudo="sudo " | |
if which most > /dev/null | |
then | |
export PAGER=most | |
fi | |
function cd { | |
builtin cd "${@}" | |
if [[ "$(stat -c "%b" .)" -lt "100" ]] | |
then | |
if [ "$( ls -C -w $COLUMNS | wc -l )" -gt 30 ] ; then | |
ls -C -w $COLUMNS --color=always | awk 'NR < 16 { print }; NR == 16 { print " (... snip ...)" }; { buffer[NR % 14] = $0 } END { for( i = NR + 1; i <= NR+14; i++ ) print buffer[i % 14] }' | |
else | |
ls | |
fi | |
fi | |
} | |
#END env changes | |
#BEGIN general aliases/functions | |
function nocap() { | |
rename 'y/A-Z/a-z/' * | |
} | |
function nospace() { | |
rename 's/ /_/g' * | |
} | |
function fixdir() { | |
rename 'y/A-Z/a-z/' * | |
rename 's/ /_/g' * | |
} | |
function fhist { | |
cat ~/.full_history | grep "$@" | tail | |
} | |
alias ..='cd ..' | |
alias ins='sudo apt install' | |
alias upgrade='sudo apt upgrade' | |
alias hist='history | grep -i ' | |
function mkcd() { | |
mkdir -p "$1" && cd "$1" | |
} | |
alias ll='ls -alF' #'list long' | |
alias la='ls -A' #'list all' | |
alias l='ls -CF' #'list' | |
alias lcr='ls -cr' #'list changed recently' | |
alias lc='ls -c' #'list changed' | |
# Find files of the specified types, and grep for the specified token, ignoring .svn directories. Additional grep options can be passed in | |
# in parameter $3, but multiple params must be quoted into a single string (e.g., "-i -C3"). Note that this function is not meant to be | |
# called directly, but rather to serve as a base for other filetype-specific functions/aliases, such as findInSource, etc. | |
function findInTypes { | |
types=$(echo $2 | tr "," "\n") | |
cmd="find . \\( -path '*/.svn' -prune -false \\) -o -type f \\(" | |
sep=" " | |
for t in $types; do | |
cmd="${cmd}${sep}-name '$t'" | |
sep=" -o " | |
done | |
cmd="$cmd \\) -exec grep $3 \"$1\" {} /dev/null \\;" | |
eval $cmd | |
} | |
# Grep on all "known" file types. Its up to you to define what those are. | |
function findInAll { | |
findInTypes "$1" "*.c,*.cpp,*.h,*.xml,*.dxml,*.xsl,*.java,*.js,*.log,Makefile,*.mk,*.py,*.sh,*.txt" "$2" | |
} | |
alias fa='findInAll' | |
function gcode() { grep --color=always -rnC3 -- "$@" . | less -R; } | |
function up() { pushd .. > /dev/null; } | |
function down() { popd > /dev/null; } | |
function launch { | |
( $* &> /dev/null & ) | |
} | |
alias duf='du -sk * | sort -n | while read size fname; do for unit in k M G T P E Z Y; do if [ $size -lt 1024 ]; then echo -e "${size}${unit}\t${fname}"; break; fi; size=$((size/1024)); done; done' | |
function ip-get { | |
echo -n "Wired: " | |
ip addr show eth0 | grep inet | awk '{print $2}' | tr "\n" " " | cut -f1 -d"/" | |
echo -n "Wireless: " | |
ip addr show wlan0 | grep inet | awk '{print $2}' | tr "\n" " " | cut -f1 -d"/" | |
echo -n "Public: " | |
curl http://icanhazip.com/ | |
} | |
alias passwordgen="pwgen --no-capitalize 20 1" | |
function lq { | |
nohup "$@"&exit | |
} | |
function manpdf { | |
man -t $1 | ps2pdf14 - $1.pdf | |
} | |
alias axel="axel -a " | |
function rmedir { | |
cd "$1" | |
find . -type d -empty -exec rmdir {} \; | |
} | |
alias watchfile="tail -fs 0.2 -n $LINES" | |
function pp { | |
# remember output, prevent different clipboard contents | |
# after choosing to execute | |
script=`xclip -o` | |
cat -A <<< "$script" | |
echo -ne "\nExecute? (y/n): " | |
read execute | |
# only execute when 'y' was answered | |
# all other input is ignored | |
if [[ $execute == "y" ]]; then | |
eval $script | |
fi | |
} | |
alias calc='python3 -ic "from math import *; import cmath"' | |
alias st="git status" | |
alias add="git add" | |
alias push="git push" | |
alias pull="git pull" | |
alias checkout="git checkout" | |
function commit { | |
if [[ "$1" == "all" ]] | |
then | |
git commit -am "$2" | |
elif [[ "$1" != "" ]] | |
then | |
git commit -m "$1" | |
else | |
git commit | |
fi | |
} | |
function c { | |
while read -r _ dir | |
do | |
if [[ -e "$dir" ]] | |
then | |
echo "$dir" | |
cd "$dir" | |
break | |
fi | |
done < <(cat ~/.full_history | tail -n 10000 \ | |
| cut -f 3 | sort | uniq -dc | sort -hr | grep "/$1$") | |
} | |
function _c { | |
local IFS=$'\n' | |
COMPREPLY=( $(cat ~/.full_history | tail -n 10000 \ | |
| cut -f 3 | sort | uniq -dc | sort -hr \ | |
| sed 's/.*\///g' | grep "^$2") ) | |
} | |
complete -F _c c | |
# Clear info bar before running less. | |
function less { echo -en "\e7\e[0;$((LINES))r\e8\n"; /usr/bin/less "$@"; } | |
#END general aliases/functions | |
It should update the size with each command invocation. As for why the prompt is the command number, for some reason I thought it would be useful at the time. It hasn’t been.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey, this is great! thanks for sharing.
Few questions: