Skip to content

Instantly share code, notes, and snippets.

@RobbiNespu
Last active May 18, 2018 10:33
Show Gist options
  • Save RobbiNespu/27ceea8b9edf4b74a6f914770d6b558f to your computer and use it in GitHub Desktop.
Save RobbiNespu/27ceea8b9edf4b74a6f914770d6b558f to your computer and use it in GitHub Desktop.
My personal bashrc
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=
# User specific aliases and functions
# ═══════════════════════════════════════
# Powerline
# ═══════════════════════════════════════
# Look geek but not well responsive
#if [ -f `which powerline-daemon` ]; then
# powerline-daemon -q
# POWERLINE_BASH_CONTINUATION=1
# POWERLINE_BASH_SELECT=1
# . /usr/share/powerline/bash/powerline.sh
#fi
# ═══════════════════════════════════════
# Color Prompt
# ═══════════════════════════════════════
# bash prompt
FGBLK=$( tput setaf 0 ) # 000000
FGRED=$( tput setaf 1 ) # ff0000
FGGRN=$( tput setaf 2 ) # 00ff00
FGYLO=$( tput setaf 3 ) # ffff00
FGBLU=$( tput setaf 4 ) # 0000ff
FGMAG=$( tput setaf 5 ) # ff00ff
FGCYN=$( tput setaf 6 ) # 00ffff
FGWHT=$( tput setaf 7 ) # ffffff
BGBLK=$( tput setab 0 ) # 000000
BGRED=$( tput setab 1 ) # ff0000
BGGRN=$( tput setab 2 ) # 00ff00
BGYLO=$( tput setab 3 ) # ffff00
BGBLU=$( tput setab 4 ) # 0000ff
BGMAG=$( tput setab 5 ) # ff00ff
BGCYN=$( tput setab 6 ) # 00ffff
BGWHT=$( tput setab 7 ) # ffffff
RESET=$( tput sgr0 )
BOLDM=$( tput bold )
UNDER=$( tput smul )
REVRS=$( tput rev )
# get current branch in git repo
function parse_git_branch() {
BRANCH=`git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`
if [ ! "${BRANCH}" == "" ]
then
STAT=`parse_git_dirty`
echo "(Branch : ${BRANCH}${STAT}) "
else
echo ""
fi
}
# get current status of git repo
function parse_git_dirty {
status=`git status 2>&1 | tee`
dirty=`echo -n "${status}" 2> /dev/null | grep "modified:" &> /dev/null; echo "$?"`
untracked=`echo -n "${status}" 2> /dev/null | grep "Untracked files" &> /dev/null; echo "$?"`
ahead=`echo -n "${status}" 2> /dev/null | grep "Your branch is ahead of" &> /dev/null; echo "$?"`
newfile=`echo -n "${status}" 2> /dev/null | grep "new file:" &> /dev/null; echo "$?"`
renamed=`echo -n "${status}" 2> /dev/null | grep "renamed:" &> /dev/null; echo "$?"`
deleted=`echo -n "${status}" 2> /dev/null | grep "deleted:" &> /dev/null; echo "$?"`
bits=''
if [ "${renamed}" == "0" ]; then
bits=">${bits}"
fi
if [ "${ahead}" == "0" ]; then
bits="*${bits}"
fi
if [ "${newfile}" == "0" ]; then
bits="+${bits}"
fi
if [ "${untracked}" == "0" ]; then
bits="?${bits}"
fi
if [ "${deleted}" == "0" ]; then
bits="x${bits}"
fi
if [ "${dirty}" == "0" ]; then
bits="!${bits}"
fi
if [ ! "${bits}" == "" ]; then
echo " | Status: ${bits}"
else
echo ""
fi
}
if [ $EUID == 0 ]; then
export PS1="[\[$FGRED\]\u\[$FGMAG\]@\[$FGCYN\]\h \[$FGBLU\]\W] $ \[$RESET\]"
else
export PS1="[\[$FGGRN\]\u\[$FGMAG\]@\[$FGCYN\]\h \[$FGBLU\]\W] \[$FGYLO\]\[$(tput bold)\]\`parse_git_branch\`\[$(tput sgr0)\]\[$FGBLU\]$ \[$RESET\]"
fi
# ═══════════════════════════════════════
# Check file size
# ═══════════════════════════════════════
function checkfilesize() {
du -h $1 | tail -n 1
}
alias sizeof=checkfilesize
# Sometime I run CMD clear command
alias cls=clear
# ═══════════════════════════════════════
# Check file details
# ═══════════════════════════════════════
alias detail=stat
# ═══════════════════════════════════════
# Git Graph
# ═══════════════════════════════════════
alias gitgraph='git log --graph --oneline --all --decorate --topo-order'
# ═══════════════════════════════════════
# SSH Logging
# ═══════════════════════════════════════
# ssh to WMS project and log STDOUT output
function sshwms(){
echo "Connect to $1 via VPN..."
script -a -f -q -c "ssh $1" /tmp/ssh-$1-$(date +%Y-%m-%d_%H:%M:%S).log
}
# ═══════════════════════════════════════
# Let be careful, ok?
# ═══════════════════════════════════════
# interactive
# -i prompt before overwrite
# -v verbose
alias rm='rm -i -v'
alias mv='mv -i -v'
alias cp='cp -i -v'
alias ln='ln -i'
# ═══════════════════════════════════════
# Code highlighter
# ═══════════════════════════════════════
# View coding with syntax highlighter on terminal
# kucing and tikus are much cooler than cat!
alias kucing='pygmentize -g -O style=colorful,linenos=1' # using python package
alias tikus='highlight -l -O ansi --force' # using highlighter package
# ═══════════════════════════════════════
# Python stuff
# ═══════════════════════════════════════
# Updat PIP
function update_pip(){
pip freeze --local | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 pip install -U --user
}
# ═══════════════════════════════════════
# Weather
# ═══════════════════════════════════════
# usage :
# weather simpang+renggam
# weather ~wongnoi
function weather {
curl -s wttr.in/$1
}
# ═══════════════════════════════════════
# Bash history
# ═══════════════════════════════════════
export HISTSIZE=1000000 # remember the last 100000 commands
export HISTFILESIZE=100000 # start truncating commands after 100000 lines
export HISTCONTROL='ignoreboth' # ignoreboth is shorthand for ignorespace and ignoredups
export HISTIGNORE='&:ls:[bf]g:exit'
export HISTTIMEFORMAT="%Y/%m/%d %T "
# The following line ensures that history logs screen commands as well
shopt -s histappend
# This line makes the history file to be rewritten and reread at each bash prompt
PROMPT_COMMAND="$PROMPT_COMMAND;history -a; history -n"
set cmdhist
# ═══════════════════════════════════════
# Copy from terminal to HOST clipboard
# ═══════════════════════════════════════
alias copyclipboard="xclip -selection clipboard"
# ═══════════════════════════════════════
# OS POWER MANAGEMENT
# ═══════════════════════════════════════
# easy shutdown/reboot
alias reboot="sudo /sbin/reboot"
alias shutdown="sudo /sbin/shutdown"
# ═══════════════════════════════════════
# Checking Memory
# ═══════════════════════════════════════
alias free="free -mth"
# ═══════════════════════════════════════
# BATTERY DIAGNOSTICS FOR PS1
# ═══════════════════════════════════════
alias bat='upower -i /org/freedesktop/UPower/devices/battery_BAT0| grep --color=NEVER -E "present|state|to\ full|percentage|capacity|time\ to empty|time\ to full|technology"'
# ═══════════════════════════════════════
# Proxy connection
# ═══════════════════════════════════════
# set proxy (BASH only not systemwide)
function setproxy() {
export NO_PROXY='localhost,127.0.0.0/8,::1,*.uthm.edu.my'
export no_proxy='localhost,127.0.0.0/8,::1,*.uthm.edu.my'
export {HTTP,HTTPS,FTP}_PROXY='http://proxy.uthm.edu.my:8080/'
export {http,https,ftp}_proxy='http://proxy.uthm.edu.my:8080/'
gsettings set org.gnome.system.proxy mode 'manual'
gsettings set org.gnome.system.proxy.ftp host 'proxy.uthm.edu.my'
gsettings set org.gnome.system.proxy.ftp port 8080
gsettings set org.gnome.system.proxy.https host 'proxy.uthm.edu.my'
gsettings set org.gnome.system.proxy.https port 8080
gsettings set org.gnome.system.proxy.http host 'proxy.uthm.edu.my'
gsettings set org.gnome.system.proxy.http port 8080
gnome-terminal -x bash -c "git config --global http.proxy \"proxy.uthm.edu.my:8080\""
echo -e "Proxy environment variable has been setup!"
}
function unsetproxy() {
unset {HTTP,HTTPS,FTP}_PROXY
unset {http,https,ftp}_proxy
unset no_proxy
unset NO_PROXY
gsettings set org.gnome.system.proxy mode 'none'
gnome-terminal -x bash -c "git config --global http.proxy \"\""
echo -e "Proxy environment variable removed."
}
# ═══════════════════════════════════════
# Archive
# ═══════════════════════════════════════
# extract: Extract most know archives with one command
#---------------------------------------------------------
extract () {
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xjf $1 ;;
*.tar.gz) tar xzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar e $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "'$1' cannot be extracted via extract()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}
# ═══════════════════════════════════════
# Android development stuff
# ═══════════════════════════════════════
# list of Android Virtual Device
function avd(){
# Gather emulators that exist on this computer
#DEVICES=( $(optirun /mnt/01D19F269DAADFD0/Android/Sdk-linux/tools/emulator -list-avds 2>&1 ) )
DEVICES=( $(vblank_mode=0 primusrun /mnt/01D19F269DAADFD0/Android/Sdk-linux/tools/emulator -list-avds 2>&1 ) )
# Display list of emulators
echo " Available Emulators"
echo " ----------------------------------------"
N=1
for DEVICE in ${DEVICES[@]}
do
echo " $N) $DEVICE"
let N=$N+1
done
# Request an emulator to start
read -p "
Choose an emulator: " num
# If the input is valid, launch our emulator on a separate PID and exit
if [ $num -lt $N ] && [ $num -gt 0 ];
then
DEVICE=${DEVICES[$num-1]}
#optirun /mnt/01D19F269DAADFD0/Android/Sdk-linux/tools/emulator -netdelay none -netspeed full -accel auto -engine auto -no-boot-anim -avd "$DEVICE" -verbose > /dev/null 2>&1 &
vblank_mode=0 primusrun /mnt/01D19F269DAADFD0/Android/Sdk-linux/tools/emulator -netdelay none -netspeed full -accel auto -engine auto -no-boot-anim -avd "$DEVICE" -verbose > /dev/null 2>&1 &
exit 0
else
echo "Invalid Entry : $num"
exit 1
fi
}
export ANDROID_SDK_ROOT=/mnt/01D19F269DAADFD0/Android/Sdk-linux
export PATH=$PATH:$ANDROID_SDK_ROOT/platform-tools
export PATH=$PATH:$ANDROID_SDK_ROOT/tools
# ═══════════════════════════════════════
# Others
# ═══════════════════════════════════════
# Go to Data partition (already automounted)
alias cddata='cd /mnt/01D19F269DAADFD0'
# When compiling or patching, we need huge /tmp size
alias tmp10g='sudo mount -o remount,size=10G,noatime /tmp'
# Run jeklly locally
alias jekylllocal='bundle exec jekyll clean && bundle exec jekyll serve --watch --verbose' #jekyll clean && jekyll serve --verbose --watch
# VS code from flatpak
alias vscode='/usr/bin/flatpak run --branch=stable --arch=x86_64 --command=code --file-forwarding com.visualstudio.code --reuse-window'
# List out file size/dir in location
alias kecik2takNakMampos='du -sch .[!.]* * |sort -h'
# View CSV file from terminal
csview(){
local file="$1"
cat "$file" | sed -e 's/,,/, ,/g' | column -s, -t | less -#5 -N -S
}
## Resume wget by default - this one saved by butt so many times
alias wget='wget -c'
# backup with date "file.txt.backup-2018_11_12_2307AM" / Do we need seconds also?
bu() { cp "$1" "$1".backup-`date +%Y_%m_%d__%H%M%p`; }
# find text inside java
alias whichjava='find . -name "*.java" | xargs grep'
# Find string inside files
carikan(){
#find . -type f -exec grep -l "$1" {} \;
fd . --type f -exec grep -l "$1" {} \;
}
carifile(){
# exclude class
#find . -type f ! -name '*.class' -exec grep -l "$1" {} \; 2> /dev/null
find . -type f \( -iname "*.*" ! -iname "*.class" ! -iname "*.jpg" ! -iname "*.png" ! -iname "*.exe" ! -iname "*.jar" \) -exec grep -l "$1" {} \; 2> /dev/null
}
# Download video from youtube and convert to mp3
youtubemp3(){
youtube-dl --verbose --extract-audio --audio-format mp3 --audio-quality 0 "$1"
}
# Remove all bash history!
alias clearBashHistory='cat /dev/null > ~/.bash_history && history -c && exit'
# Suspend
alias tidor='systemctl suspend'
# Golang stuff
export PATH=$PATH:/usr/local/go/bin
export GOPATH="/home/rnm/Workspaces/Golang"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment