Skip to content

Instantly share code, notes, and snippets.

@thecompyoda
Last active August 21, 2016 10:24
Show Gist options
  • Save thecompyoda/e2e5b538e5b8765266a3381e9c7a9f65 to your computer and use it in GitHub Desktop.
Save thecompyoda/e2e5b538e5b8765266a3381e9c7a9f65 to your computer and use it in GitHub Desktop.
*nix Bash Profile
# ---------------------------------------------------------------------------
#
# Description: This file holds all my BASH configurations and aliases
#
# Sections:
# 1. Environment Configuration
# 2. Make Terminal Better (remapping defaults and adding functionality)
# 3. File and Folder Management
# 4. Searching
# 5. Process Management
# 6. Networking
# 7. Git Commands
# 8. .bashrc Edit and Source
# 9. Original *nix Bash Settings (From Server Install)
# ---------------------------------------------------------------------------
# -------------------------------
# 1. ENVIRONMENT CONFIGURATION
# -------------------------------
# Change Prompt
# ------------------------------------------------------------
export PS1="################################################################################\n# \w @ \h (\u) \n# => "
export PS2="# => "
# Set Paths
# ------------------------------------------------------------
# --------------------------------------------------------------------
# Set the path using an array, and a loop that checks to see if array alements already exist in current PATH
# Added by: thecompyoda <Donnie Bartley>
# On: August 20, 2016
# --------------------------------------------------------------------
# DO NOT EDIT THIS PATH VAR!!#
export PATH=$PATH #
##############################
# ADD NEW PATH VARS TO THIS ARRAY
pathsarray=(
'/usr/local/sbin'
'/usr/local/bin'
'/usr/sbin'
'/usr/bin'
'/sbin'
'/bin'
'/usr/games'
'/usr/local/games'
'/snap/bin'
)
for i in "${pathsarray[@]}"
do
if [[ $PATH != *$i:* ]]; then
export PATH=$PATH:$i
fi
done
# Set Default Editor (change 'Nano' to the editor of your choice)
# ------------------------------------------------------------
export EDITOR=/usr/bin/vi
# Set default blocksize for ls, df, du
# from this: http://hints.macworld.com/comment.php?mode=view&cid=24491
# ------------------------------------------------------------
export BLOCKSIZE=1k
# Control History
# ----------------------------------------------
export HISTCONTROL=ignoreboth:erasedups
export HISTSIZE=5000
export HISTFILESIZE=5000
# -----------------------------
# 2. MAKE TERMINAL BETTER
# -----------------------------
alias editvim='vi ~/.vimrc'
alias cp='cp -iv' # Preferred 'cp' implementation
alias mv='mv -iv' # Preferred 'mv' implementation
alias mkdir='mkdir -pv' # Preferred 'mkdir' implementation
alias ls='ls -FGlAhp' # Preferred 'ls' implementation
alias ll='ls'
alias less='less -FSRXc' # Preferred 'less' implementation
cd() { builtin cd "$@"; ll; } # Always list directory contents upon 'cd'
alias cd..='cd ../' # Go back 1 directory level (for fast typers)
alias ..='cd ../' # Go back 1 directory level
alias .2='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
alias ~="cd ~" # ~: Go Home
alias c='clear' # c: Clear terminal display
alias which='type -all' # which: Find executables
alias path='echo -e ${PATH//:/\\n}' # path: Echo all executable Paths
alias show_options='shopt' # Show_options: display bash options settings
alias fix_stty='stty sane' # fix_stty: Restore terminal settings when screwed up
alias cic='set completion-ignore-case On' # cic: Make tab-completion case-insensitive
mcd () { mkdir -p "$1" && cd "$1"; } # mcd: Makes new Dir and jumps inside
alias DT='tee ~/terminalOut.txt' # DT: Pipe content to file on MacOS Desktop
# lr: Full Recursive Directory Listing
alias lr='ls -R | grep ":$" | sed -e '\''s/:$//'\'' -e '\''s/[^-][^\/]*\//--/g'\'' -e '\''s/^/ /'\'' -e '\''s/-/|/'\'' | less'
# mans: e.g. mans mplayer codec
# --------------------------------------------------------------------
# Note: Search manpage given in agument '1' for term given in argument '2' (case insensitive)
# displays paginated result with colored search terms and two lines surrounding each hit.
# --------------------------------------------------------------------
mans () {
man $1 | grep -iC2 --color=always $2 | less
}
# -------------------------------
# 3. FILE AND FOLDER MANAGEMENT
# -------------------------------
zipf () { zip -r "$1".zip "$1" ; } # zipf: To create a ZIP archive of a folder
alias numFiles='echo $(ls -1 | wc -l)' # numFiles: Count of non-hidden files in current dir
# 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) 7zr x $1 ;;
*) echo "'$1' cannot be extracted via extract()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}
# ---------------------------
# 4. SEARCHING
# ---------------------------
alias qfind="find . -name " # qfind: Quickly search for file
ff () { /usr/bin/find . -name "$@" ; } # ff: Find file under the current directory
ffs () { /usr/bin/find . -name "$@"'*' ; } # ffs: Find file whose name starts with a given string
ffe () { /usr/bin/find . -name '*'"$@" ; } # ffe: Find file whose name ends with a given string
# ---------------------------
# 5. PROCESS MANAGEMENT
# ---------------------------
# findPid: find out the pid of a specified process
# -----------------------------------------------------
# Note that the command name can be specified via a regex
# E.g. findPid '/d$/' finds pids of all processes with names ending in 'd'
# Without the 'sudo' it will only find processes of the current user
# -----------------------------------------------------
findPid () { lsof -t -c "$@" ; }
# my_ps: List processes owned by my user:
my_ps() { ps $@ -u $USER -o pid,%cpu,%mem,start,time,bsdtime,command ; }
# ---------------------------
# 6. NETWORKING
# ---------------------------
# myip: Public Facing IP Address
alias myip='dig + short myip.opendns.com @resolver1.opendns.com'
alias netCons='lsof -nP' # netCons: Show all open TCP/IP sockets
alias lsock='sudo /usr/bin/lsof -nP' # lsock: Display open sockets
alias lsockU='sudo /usr/bin/lsof -nP | grep UDP' # lsockU: Display only open UDP sockets
alias lsockT='sudo /usr/bin/lsof -nP | grep TCP' # lsockT: Display only open TCP sockets
alias openPorts='sudo lsof -np | grep LISTEN' # openPorts: All listening connections
# ii: display useful host related informaton
ii() {
echo -e "\nAdditionnal information:$NC " ; uname -a
echo -e "\n${RED}Current date :$NC " ; date
echo -e "\n${RED}Machine stats :$NC " ; uptime
echo -e "\n${RED}Public facing IP Address :$NC " ;myip
echo
}
# ---------------------------------------
# 7. GIT ALIASES
# ---------------------------------------
# Git branch on terminal: https://github.com/jimeh/git-aware-prompt
# Git autocomplete commands: http://code-worrier.com/blog/autocomplete-git/
alias gs='git status'
alias gc='git commit'
alias gca='git commit .'
alias gcam='git commit -am'
alias gco='git checkout'
alias gcod='git checkout origin development'
alias gpod='git pull origin development'
alias daje='gcod && gpod'
alias gp='git pull'
alias gpom="git pull origin master"
alias gp='git push'
alias gd='git diff | mate'
alias gb='git branch'
alias gba='git branch -a'
alias del='git branch -d'
# ---------------------------------------
# 8. .bashrc Edit and Source
# ---------------------------------------
# --------------------------------------------------------------------
# Bash editing, and automated resourcing
# Added by: thecompyoda <Donnie Bartley>
# On: August 20, 2016
# --------------------------------------------------------------------
# ebashrc: opens .bashrc for editing
# --------------------------------------------------------------------
# Note: This function requires the 'sourcebashwait' funtion to keep errors from displaying on
# the terminal while you are editing the file, and to make sure this file is sourced
# only AFTER the vi editor is closed. The terminal also clears after vi is closed
# --------------------------------------------------------------------
ebashrc () {
vi ~/.bashrc
sourcebashwait >/dev/null 2>&1
source ~/.bashrc
c
}
# ebashrc requires the sourcebashwait function!
sourcebashwait () {
until ! [ test -f ~/.bashrc.swp ]; do
sleep 5
done
}
# editbash: calls the ebashrc function to edit .bashrc
# --------------------------------------------------------------------
# Note: calling 'ebashrc' from command line without using this alias
# may cause 'command not found' errors to diaplay after closing the vi editor
# --------------------------------------------------------------------
alias editbash='ebashrc'
# ---------------------------------------
# 9. Original *nix Bash Settings (From Server Install)
# ---------------------------------------
# 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
# 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|*-256color) 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
#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
# If this is an xterm set the title to user@host:dir
#case "$TERM" in
#xterm*|rxvt*)
# PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
# ;;
*)
# ;;
#esac
# 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
# colored GCC warnings and errors
#export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
# Add an "alert" alias for long running commands. Use 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment