# Create a new directory and enter it function md() { mkdir -p "$@" && cd "$@" } # Use Git’s colored diff when available hash git &>/dev/null if [ $? -eq 0 ]; then function diff() { git diff --no-index --color-words "$@" } fi # Create a data URL from an image (works for other file types too, if you tweak the Content-Type afterwards) dataurl() { echo "data:image/${1##*.};base64,$(openssl base64 -in "$1")" | tr -d '\n' } # Start an HTTP server from a directory, optionally specifying the port function server() { local port="${1:-8000}" open "http://localhost:${port}/" # Set the default Content-Type to `text/plain` instead of `application/octet-stream` # And serve everything as UTF-8 (although not technically correct, this doesn’t break anything for binary files) python -c $'import SimpleHTTPServer;\nmap = SimpleHTTPServer.SimpleHTTPRequestHandler.extensions_map;\nmap[""] = "text/plain";\nfor key, value in map.items():\n\tmap[key] = value + ";charset=UTF-8";\nSimpleHTTPServer.test();' "$port" } # Get gzipped file size function gz() { echo "orig size (bytes): " cat "$1" | wc -c echo "gzipped size (bytes): " gzip -c "$1" | wc -c } # Test if HTTP compression (RFC 2616 + SDCH) is enabled for a given URL. # Send a fake UA string for sites that sniff it instead of using the Accept-Encoding header. (Looking at you, ajax.googleapis.com!) function httpcompression() { encoding="$(curl -LIs -H 'User-Agent: Mozilla/5 Gecko' -H 'Accept-Encoding: gzip,deflate,compress,sdch' "$1" | grep '^Content-Encoding:')" && echo "$1 is encoded using ${encoding#* }" || echo "$1 is not using any encoding" } # Gzip-enabled `curl` function gurl() { curl -sH "Accept-Encoding: gzip" "$@" | gunzip } # Syntax-highlight JSON strings or files function json() { if [ -p /dev/stdin ]; then # piping, e.g. `echo '{"foo":42}' | json` python -mjson.tool | pygmentize -l javascript else # e.g. `json '{"foo":42}'` python -mjson.tool <<< "$*" | pygmentize -l javascript fi } # All the dig info function digga() { dig +nocmd "$1" any +multiline +noall +answer } # Escape UTF-8 characters into their 3-byte format function escape() { printf "\\\x%s" $(printf "$@" | xxd -p -c1 -u) echo # newline } # Decode \x{ABCD}-style Unicode escape sequences function unidecode() { perl -e "binmode(STDOUT, ':utf8'); print \"$@\"" echo # newline } # Get a character’s Unicode code point function codepoint() { perl -e "use utf8; print sprintf('U+%04X', ord(\"$@\"))" echo # newline }