Skip to content

Instantly share code, notes, and snippets.

@iamamused
Last active December 11, 2015 07:48
Show Gist options
  • Select an option

  • Save iamamused/4568613 to your computer and use it in GitHub Desktop.

Select an option

Save iamamused/4568613 to your computer and use it in GitHub Desktop.
Home Files
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
#; This will check the Operating System type so we can case on it later
UNAME="`uname -s`"
export UNAME
# don't put duplicate lines in the history. See bash(1) for more options
export HISTCONTROL=ignoredups
# Initialize HISTIGNORE with commands we don't want going into the bash history. Only exact matches are filtered.
HISTIGNORE="l:ll:llt:cdb:b:r:exit:env:date:.:..:...:....:.....:pwd:cfg:rb:eb:!!:ls:fg:bg:cd ..:h:mc"
# Declare that vim is the default editor
export EDITOR=vim
#---------------
# settings
#---------------
set -o notify
set -o noclobber
set -o ignoreeof
#set -o nounset
#http://fvue.nl/wiki/Bash:_Error_%60Unbound_variable'_when_testing_unset_variable
#set -o xtrace # useful for debuging
shopt -s cdspell
shopt -s cdable_vars
shopt -s checkhash
shopt -s checkwinsize
shopt -s mailwarn
shopt -s sourcepath
shopt -s no_empty_cmd_completion
shopt -s histappend histreedit
shopt -s extglob # useful for programmable completion
#-----------------------
# Greeting, motd etc...
#-----------------------
# Define some colors first:
red='\e[0;31m'
RED='\e[1;31m'
blue='\e[0;34m'
BLUE='\e[1;34m'
cyan='\e[0;36m'
CYAN='\e[1;36m'
NC='\e[0m' # No Color
# --> Nice. Has the same effect as using "ansi.sys" in DOS.
# Looks best on a black background.....
echo -e "${CYAN}This is BASH ${RED}${BASH_VERSION%.*}${CYAN} - DISPLAY on ${RED}$DISPLAY${NC}\n"
date
if [ -x /usr/games/fortune ]; then
/usr/games/fortune -s # makes our day a bit more fun.... :-)
fi
function _exit() # function to run upon exit of shell
{
echo -e "${RED}Later...${NC}"
}
trap _exit 0
#---------------
# Mods
#---------------
# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(lesspipe)"
#---------------
# Shell prompt
#---------------
function darwinprompt()
{
unset PROMPT_COMMAND
case "$TERM" in
xterm-color)
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
;;
*)
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
;;
esac
}
function linuxprompt()
{
unset PROMPT_COMMAND
# 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)
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
;;
*)
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
;;
esac
# Comment in the above and uncomment this below for a color prompt
#PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD/$HOME/~}\007"'
;;
*)
;;
esac
}
case ${UNAME} in
Linux )
linuxprompt
# password management
SSH_ENV="$HOME/.ssh/environment"
function start_agent {
echo "Initializing new SSH agent..."
/usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
echo succeeded
chmod 600 "${SSH_ENV}"
. "${SSH_ENV}" > /dev/null
/usr/bin/ssh-add;
}
# Source SSH settings, if applicable
if [ -f "${SSH_ENV}" ]; then
. "${SSH_ENV}" > /dev/null
#ps ${SSH_AGENT_PID} doesn't work under cywgin
ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
start_agent;
}
else
start_agent;
fi
;;
Darwin )
darwinprompt
# set PATH so it includes user's private bin if it exists
PATH=/opt/local/bin:/opt/local/sbin:/opt/subversion/bin:/usr/local/bin:/usr/local/php5/bin:/usr/local/pgsql/bin/:"${PATH}"
;;
esac
#---------------
# Alias definitions.
#---------------
#unalias -a #Remove all aliases
# 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
case ${UNAME} in
Linux )
eval "`dircolors -b`"
alias ls='ls --color=auto'
#alias dir='ls --color=auto --format=vertical'
#alias vdir='ls --color=auto --format=long'
alias lx='ls -lXB' # sort by extension
;;
Darwin )
alias ls='ls -laG'
;;
esac
alias lk='ls -lSr' # sort by size
alias la='ls -Al' # show hidden files
alias lr='ls -lR' # recursice ls
alias lt='ls -ltr' # sort by date
alias lm='ls -al |more' # pipe through 'more'
# grep
alias grepr='grep -Ir --exclude="*\.svn*" --exclude="*\.tmp"'
# SVN
alias sup='svn update'
alias schanged='svn merge --dry-run -r BASE:HEAD .'
alias removesvn='find ./ -name ".svn" -exec rm -rf {} \;'
#---------------
# Bash Completion
#---------------
# 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 [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
#---------------
# Useful functions
#---------------
#-------------------------------------------------------------
# short list function
function l () {
# Check for the *.* case and handle it separately
case $1 in
*.*)
ls -adF $*
return
;;
esac
# Check if $1 is non null
pattern=${1:-""}
if [ "$pattern" != "" ]; then
# If $1 is nonzero length then add an asterisk to the end
# so that all files matching $** will be listed
local pattern=$*
ls -adF ${pattern:-}*
else
ls -aF
fi
} # l
#-------------------------------------------------------------
# long list function
function ll () {
# Check for the *.* case and handle it separately
case $1 in
*.*)
ls -aldF $* | more
return
;;
esac
# Check if $1 is non null
pattern=${1:-""}
if [ "$pattern" != "" ]; then
# If $1 is nonzero length then add an asterisk to the end
# so that all files matching $** will be listed
local pattern=$*
ls -aldF ${pattern:-}* | more
else
ls -alF | more
fi
} # ll
#-------------------------------------------------------------
# long list time sorted newest first function
function llt () {
# Check for the *.* case and handle it separately
case $1 in
*.*)
ls -altdF $* | more
return
;;
esac
# Check if $1 is non null
pattern=${1:-""}
if [ "$pattern" != "" ]; then
# If $1 is nonzero length then add an asterisk to the end
# so that all files matching $** will be listed
local pattern=$*
ls -altdF ${pattern:-}* | more
else
ls -altF | more
fi
} # llt
#-------------------------------------------------------------
# Backup one directory level
function . () {
if [ $1 ]; then
source $1
else
cd ..; l
fi
}
#-------------------------------------------------------------
# Backup two directory levels
function .. () {
cd ../..; l
}
#-------------------------------------------------------------
# Backup three directory levels
function ... () {
cd ../../..; l
}
#-------------------------------------------------------------
# Backup four directory levels
function .... () {
cd ../../../..; l
}
#-------------------------------------------------------------
# Backup five directory levels
function ..... () {
cd ../../../../..; l
}
#-------------------------------------------------------------
# h
# History manipulation function. It displays the command history in reverse order.
# The number of commands displayed is set by $list_size. After the initial listing
# a "> " prompt appears. The user has three choices: 1) Press dot "." to exit
# doing nothing more, 2) Press "enter" to display the next listing of commands,
# or 3) Enter the number of a command which is executed. Since the command is
# executed from this function it isn't added to the command history.
function h () {
local list_size=10
# If $1 is a number then just execute it without displaying list
if [ $1 ]; then
local command=`fc -ln $1 $1`
echo $command
local command="eval `fc -ln $1 $1`"
$command
return
fi
# Initialize the bottom of the list
local bottom=$HISTSIZE
let bottom=HISTCMD-HISTSIZE
if [ $bottom -lt 1 ];then
bottom=1
fi
# Setup the start of the listing
local start=$HISTCMD
let start=start-list_size
if [ $start -lt $bottom ];then
start=$bottom
fi
# Setup the end of the listing
local stop=$HISTCMD
let stop=stop-1
# Display the first listing
fc -lr $start $stop
# Get user input on what to do next
local response=""
while [ "$response" = "" ]; do
read -a response -p ". to exit | enter for more | NNN to execute> "
# Display next listing if enter pressed
if [ "$response" = "" ]; then
let stop=start-1
if [ $stop -le $bottom ];then
stop=$bottom
fi
let start=start-list_size
if [ $start -lt $bottom ];then
start=$bottom
fi
fc -lr $start $stop
fi
done
# Exit if "." pressed
if [ "$response" = "." ]; then
return
fi
# Try to execute the line number entered by user
local command=`fc -ln $response $response`
echo $command
local command="eval `fc -ln $response $response`"
$command
# 051205 jrl - The following 4 lines used a temp file to execute the command
#echo "$command" >| /tmp/$$.command
#chmod 700 /tmp/$$.command
#/tmp/$$.command
#rm -f /tmp/$$.command
} # h
#-------------------------------------------------------------
# Find word passed in $1 in all files recursively starting in pwd
# This prints out relative path to file, line number, and text on that line.
function wf () {
# Use sed to replace field divider ":" with " | " so the output is easier to read
find . -type f \( -name "*" -o -name ".*" \) -a -exec fgrep -n "$1" {} /dev/null \; | sed -e 's/:/ | /g'
}
#-------------------------------------------------------------
# Find word passed in $1 in *.c files recursively starting in pwd
function wfc () {
# Use sed to replace field divider ":" with " | " so the output is easier to read
find . -type f \( -name "*.c" -o -name ".*.c" \) -a -exec fgrep -n "$1" {} /dev/null \; | sed -e 's/:/ | /g'
}
#-------------------------------------------------------------
# Find word passed in $1 in *.h files recursively starting in pwd
function wfh () {
# Use sed to replace field divider ":" with " | " so the output is easier to read
find . -type f \( -name "*.h" -o -name ".*.h" \) -a -exec fgrep -n "$1" {} /dev/null \; | sed -e 's/:/ | /g'
}
#-------------------------------------------------------------
# Find word passed in $1 in *.h and *.c files recursively starting in pwd
function wfch () {
# Use sed to replace field divider ":" with " | " so the output is easier to read
find . -type f \( -name "*.[ch]" -o -name ".*.[ch]" \) -a -exec fgrep -n "$1" {} /dev/null \; | sed -e 's/:/ | /g'
}
#-------------------------------------------------------------
# Find word passed in $1 in *.h and *.c files recursively starting in pwd
# Reverse the name from wfch to wfhc for the dislexically minded
function wfhc () {
# Use sed to replace field divider ":" with " | " so the output is easier to read
find . -type f \( -name "*.[ch]" -o -name ".*.[ch]" \) -a -exec fgrep -n "$1" {} /dev/null \; | sed -e 's/:/ | /g'
}
#-------------------------------------------------------------
# Find files recursively whose names contain the string in $1 starting in current directory
function ff () {
if [[ -z $1 ]]; then
echo "Find files recursively starting at \`pwd\` - usage: ff pattern"
return
fi
find . -type f \( -name "*$1*" -o -name ".$1*" -o -name ".*.*$1*" -o -name "*$1*.*" \) -print
}
#-------------------------------------------------------------
# Find directories recursively whose names contain the string in $1 starting in current directory
function fd () {
if [[ -z $1 ]]; then
echo "Find directories recursively starting at \`pwd\` - usage: fd pattern"
return
fi
find . -type d \( -name "*$1*" -o -name ".$1*" -o -name ".*.*$1*" -o -name "*$1*.*" \) -print
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment