Skip to content

Instantly share code, notes, and snippets.

@Bellov
Created May 13, 2019 10:46
Show Gist options
  • Save Bellov/477df24d7823bec9e63755e27162ad50 to your computer and use it in GitHub Desktop.
Save Bellov/477df24d7823bec9e63755e27162ad50 to your computer and use it in GitHub Desktop.
Bash Profile
#!/bin/bash
# ~/.bashrc - bash interactive session config
#
# is this an interactive session?
#
[[ -z "${PS1}" ]] && return
#
# prevent bashrc from loading twice
#
[[ -n "${BASHRC}" ]] && return
export BASHRC="${BASH_SOURCE[0]}"
#
# bashrc.prepend
#
[[ -r "${HOME}/.bashrc.prepend" ]] && source "${HOME}/.bashrc.prepend"
#
# the prompt string
#
#
# the prompt string
#
prompt_command() {
# last command exit status
[[ ${?} -eq 0 ]] && local exc='\$' || local exc='\[\033[01;31m\]\$\[\033[0m\]'
# colors
local reset='\[\033[0m\]'
local grey='\[\033[1;30m\]'
local red='\[\033[1;31m\]'
local green='\[\033[1;32m\]'
local yellow='\[\033[1;33m\]'
local blue='\[\033[1;34m\]'
local cyan='\[\033[0;36m\]'
# hostname & user
local host="${blue}${HOSTNAME%%.*}${reset}"
[[ ${UID} -eq 0 ]] && local user="${red}${USER}${reset}" || local user="${green}${USER}${reset}"
# current directory
local pwd="${PWD}"
[[ "${pwd}" = ${HOME} || "${pwd}" = ${HOME}/* ]] && pwd='~'"${PWD#${HOME}}"
[[ "${pwd}" = /home/* ]] && pwd='~'"${pwd#/home/}"
[[ "${pwd}" = /Users/* ]] && pwd='~'"${pwd#/Users/}"
pwd="${yellow}${pwd}${reset}"
# scm statuses, programming language environments
local scm= env=
# avoid tree scans on home directory & add an option to disable them (mainly for slow disks &| large repos)
if [[ -z ${BASHRC_DISABLE_SCM} && "${dir}" != "${HOME}" ]]; then
# search for first .git/.svn/.hg in the tree
local dir="${PWD}" git_dir= svn_dir= hg_dir=
while [[ "${dir}" != '/' && -n "${dir}" ]]; do
[[ -z ${BASHRC_DISABLE_SCM_GIT} && -z ${git_dir} && -e "${dir}/.git" ]] && git_dir="${dir}/.git" && break
[[ -z ${BASHRC_DISABLE_SCM_SVN} && -z ${svn_dir} && -e "${dir}/.svn" ]] && svn_dir="${dir}/.svn" && break
[[ -z ${BASHRC_DISABLE_SCM_HG} && -z ${hg_dir} && -e "${dir}/.hg" ]] && hg_dir="${dir}/.hg" && break
dir="${dir%/*}"
done
# git
if [[ -n ${git_dir} ]]; then
local branch= extra=
if [[ -d "${git_dir}/rebase-apply" ]]; then
if [[ -f "${git_dir}/rebase-apply/rebasing" ]]; then
extra="|${yellow}rebase${reset}"
elif [[ -f "${git_dir}/rebase-apply/applying" ]]; then
extra="|${yellow}am${reset}"
else
extra="|${yellow}am/rebase${reset}"
fi
branch="$(< "${git_dir}/rebase-apply/head-name")"
elif [[ -f "${git_dir}/rebase-merge/interactive" ]]; then
extra="|${yellow}rebase-i${reset}"
branch="$(< "${git_dir}/rebase-merge/head-name")"
elif [[ -d "${git_dir}/rebase-merge" ]]; then
extra="|${yellow}rebase-m${reset}"
branch="$(< "${git_dir}/rebase-merge/head-name")"
elif [[ -f "${git_dir}/MERGE_HEAD" ]]; then
extra="|${yellow}merge${reset}"
branch=`git --git-dir="${git_dir}" symbolic-ref HEAD 2>/dev/null`
else
if ! branch=`git --git-dir="${git_dir}" symbolic-ref HEAD 2>/dev/null`; then
if branch=`git --git-dir="${git_dir}" describe --exact-match HEAD 2>/dev/null`; then
branch="${blue}${branch}"
elif branch=`git --git-dir="${git_dir}" describe --tags HEAD 2>/dev/null`; then
branch="${blue}${branch}"
else
branch="${blue}`cut -c1-8 "${git_dir}/HEAD"`"
fi
fi
fi
branch="${branch#refs/heads/}"
if [[ -n ${branch} ]]; then
local status=`git status --porcelain 2>/dev/null | head -1`
if [[ -n ${status} ]]; then
scm="${scm}${reset}(${grey}git:${red}${branch}${reset}${extra})"
else
scm="${scm}${reset}(${grey}git:${green}${branch}${reset}${extra})"
fi
fi
fi
# svn
if [[ -n ${svn_dir} ]]; then
local revision=`svn info 2>/dev/null | grep Revision: | cut -d' ' -f2`
if [[ -n ${revision} ]]; then
local status=`svn status 2>/dev/null | head -1`
if [[ -n ${status} ]]; then
scm="${scm}${reset}(${grey}svn:${red}r${revision}${reset})"
else
scm="${scm}${reset}(${grey}svn:${green}r${revision}${reset})"
fi
fi
fi
# mercurial
if [[ -n ${hg_dir} ]]; then
local branch=`hg branch 2>/dev/null`
if [[ -n ${branch} ]]; then
local status=`hg status 2>/dev/null | head -1`
if [[ -n ${status} ]]; then
scm="${scm}${reset}(${grey}hg:${red}${branch}${reset})"
else
scm="${scm}${reset}(${grey}hg:${green}${branch}${reset})"
fi
fi
fi
fi
# environments
if [[ -z ${BASHRC_DISABLE_ENVMGR} ]]; then
# ruby
if [[ -z ${BASHRC_DISABLE_ENVMGR_RUBY} && -n ${GEM_HOME} && ${GEM_HOME} != *@global ]]; then
local rb="${GEM_HOME##*/}"
env="${env}{${grey}rb:${cyan}${rb#ruby-}${reset}}"
fi
# erlang
if [[ -z ${BASHRC_DISABLE_ENVMGR_ERLANG} && -n ${ERLANG_PREFIX} ]]; then
env="${env}{${grey}erl:${cyan}${ERLANG_PREFIX##*/}${reset}}"
fi
# elixir
if [[ -z ${BASHRC_DISABLE_ENVMGR_ELIXIR} && -n ${MIX_HOME} ]]; then
env="${env}{${grey}ex:${cyan}${MIX_HOME##*/}${reset}}"
fi
fi
# finally, set the variable
PS1="${reset}[${user}@${host}(${pwd}${scm}${env})]${exc} "
}
PS1='\u@\h:\w\$ '
PROMPT_COMMAND=prompt_command
#
# icon name & window title
#
icon_name_and_window_title() {
# hostname
${BASHRC_SSH} && local host_icon_name="${HOSTNAME%%.*}:" || local host_icon_name=
local host_window_title="${HOSTNAME%%.*}"
# current directory
local pwd="${PWD}"
[[ "${pwd}" = ${HOME} || "${pwd}" = ${HOME}/* ]] && pwd='~'"${PWD#${HOME}}"
[[ "${pwd}" = /home/* ]] && pwd='~'"${pwd#/home/}"
[[ "${pwd}" = /Users/* ]] && pwd='~'"${pwd#/Users/}"
# set the icon name & window title
if [[ ${BASH_COMMAND} = prompt_command ]]; then
echo -ne "\033]1;${host_icon_name}bash\007"
echo -ne "\033]2;${USER}@${host_window_title}:${pwd}\007"
else
echo -ne "\033]1;${host_icon_name}${BASH_COMMAND%% *}\007"
echo -ne "\033]2;${USER}@${host_window_title}:${pwd} > $BASH_COMMAND\007"
fi
}
trap icon_name_and_window_title DEBUG
#
# umask !@#$
#
umask 0022
#
# detect ssh session
#
BASHRC_SSH='false'
if [[ -n ${SSH_CLIENT} || -n ${SSH_TTY} ]]; then
BASHRC_SSH='true'
elif [[ -z ${BASHRC_DISABLE_SSH} ]]; then
pid=$$
while [[ -n ${pid} && ${pid} -ne 1 ]]; do
pid_cmd="`ps -oppid= -ocomm= -p${pid}`"
pid="${pid_cmd%% *}"
cmd="${pid_cmd#* }"
if [[ ${cmd} = *sshd ]]; then
BASHRC_SSH='true'
break
fi
done
fi
unset pid_cmd pid cmd
export BASHRC_SSH
#
# aliases (and some function overrides)
#
# ls
if [[ ${OSTYPE} = darwin* || ${OSTYPE} = freebsd* ]]; then
# freebsd & osx both have color support in `ls'
export LSCOLORS='gxBxhxDxfxhxhxhxhxcxcx'
alias ls='ls -AFG'
elif [[ ${OSTYPE} = openbsd* ]]; then
# on openbsd `colorls' is a different tool
if type -p colorls >/dev/null; then
export LSCOLORS='gxBxhxDxfxhxhxhxhxcxcx'
alias ls='colorls -AFG'
else
alias ls='ls -AF'
fi
elif [[ ${OSTYPE} = netbsd* ]]; then
# on netbsd `colorls' is generally crippled, but still better than nothing
if type -p colorls >/dev/null; then
export LSCOLORS='6x5x2x3x1x464301060203'
alias ls='colorls -AFG'
else
alias ls='ls -AF'
fi
else
# assume we have gnu coreutils
alias ls='ls -AF --color=auto'
fi
alias ll='ls -hAlF'
alias li='ls -hAlFi'
#valet
export PATH=$PATH:~/.composer/vendor/bin
#mongo
export MONGO_PATH=/usr/local/mongodb
export PATH=$PATH:$MONGO_PATH/bin
# dooh!
alias sl='ls'
alias ks='ls'
alias LS='ls'
# other useful/useless aliases
alias rm='rm -rf'
alias grep='grep --color=auto'
alias df='df -h'
alias du='du -h'
alias dirsize='du -csh'
# rename is lame, ~/.bin/prename is better & more dangerous
alias rename='prename'
# i hate the .viminfo
alias vim='vim -i NONE'
# fix some other typos i make intensively
alias CD='cd'
alias cD='cd'
alias Cd='cd'
alias cd..='cd ..'
cd.(){ cd ."${@}"; }
# rails
alias c='r console'
alias s='r server --binding=0.0.0.0'
alias g='r generate'
alias bundel='bundle'
alias bruby='bundle exec ruby'
alias brake='bundle exec rake'
alias brails='bundle exec r'
alias ls='ls -AFG'
alias ll='ls -hAlF'
alias l='ls -AlF'
alias atom='open -a Atom'
alias subl='open -a "Sublime Text"'
alias rbm='open -a "Ruby Mine"'
alias vsc='open -a "Visual Studio Code"'
alias javaide='open -a "IntelliJ IDEA CE"'
#laravel
alias comp='~/compile.sh'
#git
alias gs='git status'
alias gp='git pull'
alias gc='git checkout'
alias gs='git status'
alias gi='git init'
alias gd='git diff'
alias gco='git checkout'
alias master='git checkout master'
alias develop='git checkout develop'
alias gss='git status --short'
alias today='grep -h -d skip `date +%m/%d` /usr/share/calendar/*'
alias glg='git log --date-order --all --graph --format="%C(green)%h%Creset %C(yellow)%an%Creset %C(blue bold)%ar%Creset %C(red bold)%d%Creset%s"'
alias sleep='sudo pmset -a hibernatemode 0'
alias safesleep='sudo pmset -a hibernatemode 3'
alias lock='/System/Library/CoreServices/"Menu Extras"/User.menu/Contents/Resources/CGSession -suspend'
alias Cd..='cd'
alias cd..='cd ..'
alias cle='clear'
alias cpu_hogs='ps wwaxr -o pid,stat,%cpu,time,command | head -10'
my_ps() { ps $@ -u $USER -o pid,%cpu,%mem,start,time,bsdtime,command ; }
alias ps='ps -ax'
alias grep='grep --color=auto'
alias totalcode='git ls-files | xargs wc -l'
#some administrative ones
alias su='sudo su'
alias chown='sudo chown'
alias path='echo -e ${PATH//:/\\n}'
trash () { command mv "$@" ~/.Trash ; }
alias edit='subl'
alias code='cd ~/Projects/'
alias ll='ls -FGlAhp'
alias sl='ls'
alias ks='ls'
alias LS='ls'
alias ..='cd ../' # Go back 1 directory level
alias ...='cd ../../' # Go back 2 directory levels
alias .3='cd ../../../' # Go back 3 directory levels
alias .4='cd ../../../../' # Go back 4 directory levels
alias .5='cd ../../../../../' # Go back 5 directory levels
alias .6='cd ../../../../../../' # Go back 6 directory levels
# some administrative ones
alias su='sudo su'
alias chown='sudo chown'
# ssh & scp
alias ssh-guest='ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
alias sshrc-guest='sshrc -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
alias scp-guest='scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
# macosx
if [[ ${OSTYPE} = darwin* ]]; then
alias htop='sudo htop'
alias mtr='sudo mtr'
fi
#
# remove /usr/local/sbin & /usr/local/bin from path
#
PATH="${PATH/\/usr\/local\/sbin:}"
PATH="${PATH/:\/usr\/local\/sbin}"
PATH="${PATH/\/usr\/local\/bin:}"
PATH="${PATH/:\/usr\/local\/bin}"
#
# prepend /usr/local/sbin, /usr/local/bin, ~/bin, ~/.bin, ~/local/bin, ~/.local/bin to path
#
[[ ! "${PATH}" = */usr/local/sbin* ]] && PATH="/usr/local/sbin:${PATH}"
[[ ! "${PATH}" = */usr/local/bin* ]] && PATH="/usr/local/bin:${PATH}"
[[ -d "${HOME}/bin" ]] && PATH="${HOME}/bin:${PATH}"
[[ -d "${HOME}/.bin" ]] && PATH="${HOME}/.bin:${PATH}"
[[ -d "${HOME}/local/bin" ]] && PATH="${HOME}/local/bin:${PATH}"
[[ -d "${HOME}/.local/bin" ]] && PATH="${HOME}/.local/bin:${PATH}"
export PATH
#
# disable some common history files (i hate'em)
#
export HISTFILE=/dev/null
export LESSHISTFILE=/dev/null
#
# disable history expansion
#
set +H
#
# set some other handy stuff
#
export SVN_EDITOR=vim
export EDITOR=vim
#
# nicer python
#
export PYTHONSTARTUP="${HOME}/.pythonrc"
#
# bash completion
#
if [[ -f /usr/local/etc/bash_completion ]]; then
source /usr/local/etc/bash_completion
elif [[ -f /usr/local/share/bash-completion/bash_completion ]]; then
source /usr/local/share/bash-completion/bash_completion
elif [[ -f /etc/bash_completion ]]; then
source /etc/bash_completion
fi
#
# envmgr
#
[[ -r "${HOME}/.envmgr/init" ]] && source "${HOME}/.envmgr/init"
[[ -r "${HOME}/.envmgr/completion" ]] && source "${HOME}/.envmgr/completion"
#
# bashrc.d
#
if [[ -d "${HOME}/.bashrc.d" ]]; then
shopt -s nullglob
for file in "${HOME}/.bashrc.d"/*; do
source "${file}"
done
shopt -u nullglob
fi
#
# bashrc.append
#
[[ -r "${HOME}/.bashrc.append" ]] && source "${HOME}/.bashrc.append"
#
# show message-of-the-day (not really)
#
[[ -z ${BASHRC_DISABLE_MOTD} ]] && type -p motd >/dev/null && motd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment