Skip to content

Instantly share code, notes, and snippets.

@rfong
Last active December 19, 2019 20:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rfong/473be78b8f057ec9f39c to your computer and use it in GitHub Desktop.
Save rfong/473be78b8f057ec9f39c to your computer and use it in GitHub Desktop.
nonsensitive parts of my OSX bash profile
#!/bin/bash
### WHEREAMI
# Useful prompt
export PS1='$(whoami)@$(hostname):$(pwd)$ '
# Rename a tab
function title {
echo -ne "\033]0;"$*"\007"
}
# Auto rename current tab to current directory
function upd-title {
export PROMPT_COMMAND='echo -ne "\033]0;${PWD/#$HOME}\007"'
# Or, if you have a code directory you usually work in and want to truncate
# from prompt:
#CURR_PWD=${PWD/#$HOME/~}
#export PROMPT_COMMAND='echo -ne "\033]0;${CURR_PWD#~/my_code_dir/}\007"'
}
upd-title
### NAVIGATION
# color
alias ls='ls -G'
alias grep='grep --color'
# cd+ls, only the most used pair of commands of all time ever
function cl() {
cd $@; upd-title; ls;
}
# edit/source profile
alias epf='vim ~/.bashrc'
alias spf='source ~/.bashrc'
# moreinfo
alias ll='ls -al'
# up
alias .='cl ..'
alias ..='cl ../..'
alias ...='cl ../../..'
alias ....='cl ../../../..'
alias .....='cl ../../../../..'
alias back='cl -'
# How to get dir of script being called
# "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
### TEXT/FILE MANIPULATION
# squeeze consecutive whitespace & trim beginning
alias reduce="tr -s ' ' | sed -e 's/^[[:space:]]*//'"
# extract first nonwhitespace field
alias firstarg="reduce | cut -d' ' -f1"
# replace <search> <replacement> <location>
function replace() {
if [ -z "$3" ]; then
$3="*"
fi
perl -pi -e "s/$1/$2/g" $3
}
# zero indexed
alias even="awk 'NR % 2 == 0'"
alias odd="awk 'NR % 2 == 1'"
# pipe things to me (`cat data.json | prettyjson`)
alias prettyjson="python -m json.tool"
### NETWORKISH
alias openports='sudo ufw status'
alias listeners='netstat -an | grep LISTEN'
function wifi() {
security find-generic-password -ga "$@" | grep "password:"
}
# Gimme the process id listening on a port.
function proconport() {
PORT=$1
sudo netstat -tulpn | grep :$PORT | grep -Po '(?<=\s)\d+(?=\/.*$)'
}
# Search processes
function whatsup() {
ps aux | grep $@
}
# Dump info about what's listening on port
function whatsonport() {
ps aux | grep `proconport $1`
}
### FOOTAGE MANAGEMENT
# Useful for fixing JPG/jpg bugs. Warning: slightly janky
alias lowercase-extensions='for f in *; do mv "$f" "$f.tmp"; mv "$f.tmp" "`echo $f | tr "[:upper:]" "[:lower:]"`"; done'
# Concat .mov files
function ffmpeg-concat() {
ffmpeg -f concat -i $1.txt -c copy $1.mov
}
# Bulk preview resizer to streamline photographic consent acquisition
function resize-jpgs() {
size=${1:-640}
resize_dir=__small
mkdir $resize_dir
exts=("JPG" "jpg" "jpeg")
for ext in "${exts[@]}"; do
cp *.$ext $resize_dir;
cd $resize_dir
sips -Z $size *.$ext;
cd ..
done
}
function video2gif() {
# $1 = video input file
name=${1%.*}
delay=${2:-10}
width=640
mkdir $name-gif
ffmpeg -i $1 -vf scale=$width:-1,fps=10 $name-gif/ffout%03d.png
convert -loop 0 -delay $delay $name-gif/ffout*.png $name.gif
rm -r $name-gif/
}
function gif2video() {
# $1 = gif input
# $2 = name of output
width=640
ffmpeg -f gif -i $1 -c:v libx264 -vf scale=$width:-2,format=yuv420p $2
}
### SASSY
# SASS convert a single file
# $1 = input file
# optional $2 = input syntax
function sassify() {
input=$1
if [ -z "$2" ]; then
from='css'
else
from=$2
fi
output=`rev <<< "$input" | cut -d"." -f2- | rev`
sass-convert --from $from --to sass $input $output.sass
}
# Watch input.sass and continuously compile it to input.css
function sasswatch() {
input=$1
output="${input%.*}"
sass --watch $input:$output.css
}
# Cautiously convert just scss at this directory level
function killscss() {
for f in *.scss; do
sassify $f scss
rm $f
done
}
# Deep convert all scss
function killallscss() {
path="${1:-.}" # search under $1 or default to current dir
for f in `find $path -name *.scss`; do
sassify $f scss
rm $f
done
}
### VERSIONING FUN TIMES
alias git-praise='git blame'
alias git-current-branch="git branch | grep \* | cut -d' ' -f2-"
alias git-current-commit="git rev-parse HEAD"
# Like --name-status for grep results / unique file names only
function git-grepf() {
git grep $@ | cut -d':' -f1 | sort -u
}
function grepf() {
grep $@ | cut -d':' -f1 | sort -u
}
# Deep trace commits on a change that may have been moved from another file
function git-deep-trace() {
TEXT=$1
if [ -z ${TEXT} ]; then
echo "Must provide text to trace"
return 1
fi
HASHES=`git --no-pager log --pretty="%H" -G"${TEXT}"`
for hash in $HASHES; do
git show --summary $hash
done
}
# to help me figure out which branches are trash (hint: most of them)
function git-branch-age() {
seen_master=0
for br in `git branch`; do
br=`echo $br | xargs`
if [ $seen_master -eq 1 ]; then
br_seconds=`head -1 .git/logs/refs/heads/$br | cut -d' ' -f5`
if [ -z $br_seconds ]; then
printf "%30s\t--\n" $br
else
br_datestr=`date -r $br_seconds +"%Y-%m-%d"`
printf "%30s\t%s\n" $br ${br_datestr}
fi
elif [ $br == 'master' ]; then
seen_master=1
fi
done
}
### DEVELOPMENT UTILITIES
alias py3='python3'
alias pym='python -m'
alias killpyc="find . -name '*.pyc' -delete"
alias killswp="find . -name '.*.swp' -delete && find . -name '.*.swo' -delete"
# Start simpleserver on specified port or default to 8080
function pyserv() {
python -m SimpleHTTPServer ${1:-8080}
}
function py3serv() {
python3 -m http.server ${1:-8080}
}
# pipe an input file to python lol
function usaco {
# <py file> <input file>
cat $2 | python $1
}
function checkjson {
cat $@ | python -m json.tool
}
function http-post() {
# $1 = url
# $2 = json data
curl -H "Content-Type: application/json" -X POST -d $2 $1
}
# Attach bash shell to a running docker container
function dockbash() {
docker exec -it $1 bash
}
### DEVELOPMENT ENVIRONMENTS
# venv
alias venv='source ./venv/bin/activate'
# node
alias sequelize='node_modules/.bin/sequelize'
# heroku
export PATH="/usr/local/heroku/bin:$PATH"
# rvm
export PATH="$PATH:$HOME/.rvm/bin" # Add RVM to PATH for scripting
# arcanist
export PATH="$PATH:~/arcanist/bin/"
alias fudiff='arc diff --nolint --nounit'
# docker
alias docker-container-cleanup="docker ps -qa | xargs docker rm; docker images | grep none | awk '{print $3}' | xargs docker rmi"
# WARNING: this destroys postgres images in docker
alias docker-volumes-cleanup='docker run -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/docker:/var/lib/docker --rm martin/docker-cleanup-volumes --dry-run'
#!/bin/bash
# Oddly specific things I sometimes wanna do on ubuntu machines
alias check-nginx-conf='sudo nginx -c /etc/nginx/nginx.conf -t'
# first container matching this service name
function getcontainer() {
# $1 = service name
docker ps | grep $1 | head -n1 | cut -d' ' -f1
}
# attach interactive bash shell to matching docker container
function dockshell() {
# $1 = service name
container=`getcontainer $1`
if [ -z "$container" ]; then
echo "'${1}' is not running";
else
docker exec -it `getcontainer` bash
fi;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment