Skip to content

Instantly share code, notes, and snippets.

@sinclairfr
Created January 18, 2018 07:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sinclairfr/96a62e28b9463dfcd8ac5a9ef54b6bef to your computer and use it in GitHub Desktop.
Save sinclairfr/96a62e28b9463dfcd8ac5a9ef54b6bef to your computer and use it in GitHub Desktop.
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples
# We use preexec and precmd hook functions for Bash
# If you have anything that's using the Debug Trap or PROMPT_COMMAND
# change it to use preexec or precmd
# See also https://github.com/rcaloras/bash-preexec
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth
# append to the history file, don't overwrite it
shopt -s histappend
# for setting unlimited history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=-1
HISTFILESIZE=-1
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
# If set, the pattern "**" used in a pathname expansion context will
# match all files and zero or more directories and subdirectories.
#shopt -s globstar
# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi
# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
xterm-color) color_prompt=yes;;
esac
# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
force_color_prompt=yes
if [ -n "$force_color_prompt" ]; then
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
# We have color support; assume it's compliant with Ecma-48
# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
# a case would tend to support setf rather than setaf.)
color_prompt=yes
else
color_prompt=
fi
fi
# up up command
upup(){ DEEP=; [ -z "${DEEP}" ] && { DEEP=1; }; for i in $(seq {DEEP}); do cd ../; done; }
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
#unset color_prompt force_color_prompt
# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
alias ls='ls --color=auto'
alias dir='dir --color=auto'
alias vdir='vdir --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
fi
# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
#user defined aliases
alias install='sudo apt-get -y install'
alias update='sudo apt-get update'
alias upgrade='sudo apt-get upgrade'
alias distupgrade='sudo apt-get dist-upgrade'
alias last='cd -'
alias rmd='rm -rf'
alias suvim='sudo vim'
alias shutdown='sudo shutdown'
alias reboot='sudo reboot'
alias setexec='sudo chmod +x'
alias search='aptitude search'
alias libsearch='pkg-config --libs'
alias forcequit='killall -9'
alias apt-get='sudo apt-get'
alias c='xclip'
alias v='xclip -o'
alias pastefile='xclip -o >'
alias up='upup'
alias encrypt='~/.encrypt'
alias decrypt='~/.decrypt'
alias ayy='echo lmao'
# Add dn "alert" alias for long running commandsthisse like so:
# sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
fi
# If this is an xterm set more declarative titles
# "dir: last_cmd" and "actual_cmd" during execution
# If you want to exclude a cmd from being printed see line 156
case "$TERM" in
xterm*|rxvt*)
PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\$(print_title)\a\]$PS1"
__el_LAST_EXECUTED_COMMAND=""
print_title ()
{
__el_FIRSTPART=""
__el_SECONDPART=""
if [ "$PWD" == "$HOME" ]; then
__el_FIRSTPART=$(gettext --domain="pantheon-files" "Home")
else
if [ "$PWD" == "/" ]; then
__el_FIRSTPART="/"
else
__el_FIRSTPART="${PWD##*/}"
fi
fi
if [[ "$__el_LAST_EXECUTED_COMMAND" == "" ]]; then
echo "$__el_FIRSTPART"
return
fi
#trim the command to the first segment and strip sudo
if [[ "$__el_LAST_EXECUTED_COMMAND" == sudo* ]]; then
__el_SECONDPART="${__el_LAST_EXECUTED_COMMAND:5}"
__el_SECONDPART="${__el_SECONDPART%% *}"
else
__el_SECONDPART="${__el_LAST_EXECUTED_COMMAND%% *}"
fi
printf "%s: %s" "$__el_FIRSTPART" "$__el_SECONDPART"
}
put_title()
{
__el_LAST_EXECUTED_COMMAND="${BASH_COMMAND}"
printf "\033]0;%s\007" ""
}
# Show the currently running command in the terminal title:
# http://www.davidpashley.com/articles/xterm-titles-with-bash.html
update_tab_command()
{
# catch blacklisted commands and nested escapes
case "$BASH_COMMAND" in
*\033]0*|update_*|echo*|printf*|clear*|cd*)
__el_LAST_EXECUTED_COMMAND=""
;;
*)
put_title "${BASH_COMMAND}"
;;
esac
}
preexec_functions+=(update_tab_command)
;;
*)
;;
esac
if [ -f ~/.suspendwarning.sh ]; then
. ~/.suspendwarning.sh
fi
DISPLAY=":0"
PATH=$PATH:/home/bhuvan/Documents/FastX/bin/:/home/bhuvan/Documents/LightTable/
# Normal Colors
Black='\e[0;30m' # Black
Red='\e[0;31m' # Red
Green='\e[0;32m' # Green
Yellow='\e[0;33m' # Yellow
Blue='\e[0;34m' # Blue
Purple='\e[0;35m' # Purple
Cyan='\e[0;36m' # Cyan
White='\e[0;37m' # White
# Bold
BBlack='\e[1;30m' # Black
BRed='\e[1;31m' # Red
BGreen='\e[1;32m' # Green
BYellow='\e[1;33m' # Yellow
BBlue='\e[1;34m' # Blue
BPurple='\e[1;35m' # Purple
BCyan='\e[1;36m' # Cyan
BWhite='\e[1;37m' # White
# Background
On_Black='\e[40m' # Black
On_Red='\e[41m' # Red
On_Green='\e[42m' # Green
On_Yellow='\e[43m' # Yellow
On_Blue='\e[44m' # Blue
On_Purple='\e[45m' # Purple
On_Cyan='\e[46m' # Cyan
On_White='\e[47m' # White
NC="\e[m" # Color Reset
ALERT=${BWhite}${On_Red} # Bold White on red background
LATENCY=2
echo -e "${BCyan}This is BASH ${BRed}${BASH_VERSION%.*}${BCyan}\
- DISPLAY on ${BRed}$DISPLAY${NC}\n"
#-------------------------------------------------------------
# Shell Prompt - for many examples, see:
# http://www.debian-administration.org/articles/205
# http://www.askapache.com/linux/bash-power-prompt.html
# http://tldp.org/HOWTO/Bash-Prompt-HOWTO
# https://github.com/nojhan/liquidprompt
#-------------------------------------------------------------
# Current Format: [TIME USER@HOST PWD] >
# TIME:
# Green == machine load is low
# Orange == machine load is medium
# Red == machine load is high
# ALERT == machine load is very high
# USER:
# Cyan == normal user
# Orange == SU to user
# Red == root
# HOST:
# Cyan == local session
# Green == secured remote connection (via ssh)
# Red == unsecured remote connection
# PWD:
# Green == more than 10% free disk space
# Orange == less than 10% free disk space
# ALERT == less than 5% free disk space
# Red == current user does not have write privileges
# Cyan == current filesystem is size zero (like /proc)
# >:
# White == no background or suspended jobs in this shell
# Cyan == at least one background job in this shell
# Orange == at least one suspended job in this shell
#
# Command is added to the history file each time you hit enter,
# so it's available to all shells (using 'history -a').
# Test connection type:
if [ -n "${SSH_CONNECTION}" ]; then
CNX=${Green} # Connected on remote machine, via ssh (good).
elif [[ "${DISPLAY%%:0*}" != "" ]]; then
CNX=${ALERT} # Connected on remote machine, not via ssh (bad).
else
CNX=${BCyan} # Connected on local machine.
fi
# Test user type:
if [[ ${USER} == "root" ]]; then
SU=${Red} # User is root.
elif [[ ${USER} != $(logname) ]]; then
SU=${BRed} # User is not login user.
else
SU=${BCyan} # User is normal (well ... most of us are).
fi
NCPU=$(grep -c 'processor' /proc/cpuinfo) # Number of CPUs
SLOAD=$(( 100*${NCPU} )) # Small load
MLOAD=$(( 200*${NCPU} )) # Medium load
XLOAD=$(( 400*${NCPU} )) # Xlarge load
# Returns system load as percentage, i.e., '40' rather than '0.40)'.
function load()
{
local SYSLOAD=$(cut -d " " -f1 /proc/loadavg | tr -d '.')
# System load of the current host.
echo $((10#$SYSLOAD)) # Convert to decimal.
}
# Returns a color indicating system load.
function load_color()
{
local SYSLOAD=$(load)
if [ ${SYSLOAD} -gt ${XLOAD} ]; then
echo -en ${ALERT}
elif [ ${SYSLOAD} -gt ${MLOAD} ]; then
echo -en ${BOrange}
elif [ ${SYSLOAD} -gt ${SLOAD} ]; then
echo -en ${BRed}
else
echo -en ${BGreen}
fi
}
# Returns a color according to free disk space in $PWD.
function disk_color()
{
if [ ! -w "${PWD}" ] ; then
echo -en ${Red}
# No 'write' privilege in the current directory.
elif [ -s "${PWD}" ] ; then
local used=$(command df -P "$PWD" |
awk 'END {print } {sub(/%/,"")}')
if [ ${used} -gt 95 ]; then
echo -en ${ALERT} # Disk almost full (>95%).
elif [ ${used} -gt 90 ]; then
echo -en ${BRed} # Free disk space almost gone.
else
echo -en ${BGreen} # Free disk space is ok.
fi
else
echo -en ${BCyan}
# Current directory is size '0' (like /proc, /sys etc).
fi
}
# Returns a color according to running/suspended jobs.
function job_color()
{
if [ $(jobs -s | wc -l) -gt "0" ]; then
echo -en ${BRed}
elif [ $(jobs -r | wc -l) -gt "0" ] ; then
echo -en ${BCyan}
fi
}
# Adds some text in the terminal frame (if applicable).
# Prompt
# Now we construct the prompt.
PROMPT_COMMAND="history -a"
case ${TERM} in
*term | rxvt | linux)
PS1="\[\$(load_color)\][\A\[${NC}\] "
# User@Host (with connection type info):
PS1=${PS1}"\[${SU}\]\u\[${NC}\]@\[${CNX}\]\h\[${NC}\] "
# PWD (with 'disk space' info):
PS1=${PS1}"\[\$(disk_color)\]\W]\[${NC}\] "
# Prompt (with 'job' info):
PS1=${PS1}"\[\$(job_color)\]$\[${NC}\] "
# Set title of current xterm:
PS1=${PS1}"\[\e]0;[\u@\h] \w\a\]"
;;
*)
PS1="(\A \u@\h \W) $ "
# --> Shows full pathname of current dir.
;;
esac
### Added by the Heroku Toolbelt
export PATH="/usr/local/heroku/bin:$PATH"
function disco
{
while true
do
printf "\e[38;5;$(($(od -d -N 2 -A n /dev/urandom)%$(tput colors)))m.\e[0m"
done;
}
? () { echo "$*" | bc -l; }
function ps?
{
ps -aux | grep "$*" | cut -f 1-3
}
rm ~/currency?from=USD >& /dev/null
rm ~/currency?from=USD.1 >& /dev/null
eval $(dircolors -b $HOME/.dircolors)
function gcbull
{
curl -s http://cbsg.sourceforge.net/cgi-bin/live | grep -Eo '^<li>.*</li>' | sed s,\</\\?li\>,,g | shuf -n 1
}
function fac
{
(echo 1; seq ) | paste -s -d\* | bc;
}
function ptemp
{
__=`sensors | grep Core` && echo \(`echo $__ | sed 's/.*+\(.*\).C\(\s\)\+(.*/\1/g' | tr "\n" "+" | head -c-1`\)\/`echo $__ | wc -l` | bc && unset __
}
function ltree
{
ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/';
}
touchegg 2&>1 > /dev/null & disown;
alias killz="killall -9 zsnes"
# to clear the port forward glitches
dropssh(){
lsof -ti:9906 | xargs kill -9
lsof -ti:9907 | xargs kill -9
lsof -ti:9908 | xargs kill -9
lsof -ti:52698 | xargs kill -9
lsof -ti:22042 | xargs kill -9
}
# SSH Login Notification on Linux
IP="$(echo $SSH_CONNECTION | cut -d " " -f 1)"
HOSTNAME=$(hostname)
NOW=$(date +"%e %b %Y, %a %r")
echo 'Someone from '$IP' logged into '$HOSTNAME' on '$NOW'.' | mail -s 'SSH Login Notification' root@localhost
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment