Last active
August 5, 2026 05:06
-
-
Save m-thomson/680080f984af10fb0e2a3af80ac60094 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| ######################################################## | |
| ## .bash_reuse | |
| ######################################################## | |
| ## - Cross platform interactive bash stuff goes here | |
| ######################################################## | |
| ## This file contains aliases and functions that can be | |
| ## re-used across Mac and Linux. Source it from .bashrc | |
| ## It can also be used in a Keyboard Maestro macro (or similar) | |
| ## to "type" commands into a terminal window. | |
| ######################################################## | |
| ## Usage: | |
| ## $ echo "Sourcing https://gist.githubusercontent.com/m-thomson/680080f984af10fb0e2a3af80ac60094/raw/.bash_reuse" | |
| ## $ source <(curl -s https://gist.githubusercontent.com/m-thomson/680080f984af10fb0e2a3af80ac60094/raw/.bash_reuse) | |
| ######################################################## | |
| ## Update gist: | |
| ## $ gist -u 680080f984af10fb0e2a3af80ac60094 bash.stow/.bash_reuse | |
| ## (gist is a ruby gem installed via brew install gist) | |
| ## Note: A post-commit hook does this automatically | |
| ######################################################## | |
| # -- This file defines interactive Bash conveniences so it | |
| # -- should only execute if shell is interactive | |
| [ -n "${BASH_VERSION:-}" ] || return 0 | |
| [[ $- == *i* ]] || return 0 | |
| echo "💫 Sourcing .bash_reuse (Remember: use 'alias' to show shell aliases and 'funcs' to show functions)" >&2 | |
| echo "🐚 Bash v${BASH_VERSION} on ${OSTYPE}" >&2 | |
| ######################################################## | |
| # Bash Config (Cross platform) | |
| ######################################################## | |
| # -- Large history | |
| HISTSIZE=100000 | |
| HISTFILESIZE=200000 | |
| # -- Ignore duplicates + commands starting with space | |
| HISTCONTROL=ignoreboth | |
| # -- Append instead of overwrite | |
| shopt -s histappend | |
| # -- Timestamp every command | |
| HISTTIMEFORMAT="%F %T " | |
| # -- Write to file + reload after every command | |
| PROMPT_COMMAND='history -a; history -n' | |
| # -- Bash autocomplete ignoring case https://superuser.com/a/435127/396106 | |
| bind "set completion-ignore-case on" | |
| # -- Check the window size after each command and update the values of LINES and COLUMNS. | |
| shopt -s checkwinsize | |
| ######################################################## | |
| # Environment (Cross platform) | |
| ######################################################## | |
| export EDITOR='nano' | |
| ######################################################## | |
| # Aliases (Cross platform) | |
| ######################################################## | |
| # -- Note: on MacOS, ls is also aliased to use gnu ls | |
| alias l='ls -CXAGFh --group-directories-first --color=auto' | |
| alias ll='ls -XAGFhl --group-directories-first --color=auto' | |
| alias la='ls -A' | |
| # Options explained: | |
| # -C list entries by columns | |
| # -X sort alphabetically by entry extension | |
| # -A, --almost-all do not list implied . and .. | |
| # -G, --no-group in a long listing, don't print group names | |
| # -l use a long listing format | |
| # -h, --human-readable with -l and/or -s, print human readable sizes | |
| # -F, --classify append indicator (one of */=>@|) to entries | |
| # -- Redo the last command with sudo. Two versions depending on mood :) | |
| alias fuck='sudo $(fc -nl -1)' | |
| alias please='sudo $(fc -nl -1)' | |
| # -- Misc | |
| alias ..="cd .." | |
| alias h="history" | |
| alias g="grep -E -i" | |
| alias m="more" | |
| alias hig='history|grep -E -i' | |
| alias tre="tree -C -F --dirsfirst --filelimit 20" | |
| alias treed="tree --si -C -s --du -F|grep -F '/'" | |
| alias zscat="zstd -cd" | |
| alias path='echo $PATH|tr ":" "\n"|sort --ignore-case' | |
| alias usage="echo 'List size of immediate subfolders'>&2; du -h --max-depth=1" | |
| alias hosts="sudo nano /etc/hosts; echo 'Remember to flush DNS cache if needed (alias: flush-dns) '" | |
| alias funcs="declare -f|grep -E '^[a-zA-Z0-9][-a-zA-Z0-9_]* \(\)'|grep -v '^nvm_'" | |
| alias bat="bat --theme=Dracula" | |
| # -- Docker | |
| alias drps="docker ps --format \"table {{.ID}}\t{{.Image}}\t{{.Names}}\t{{.Ports}}\"|sed -r 's/:::([^,])+(, |$)//g'" | |
| # -- Use less as a replacement for head and tail commands. +G = go to end -S = Prevent text wrap -n = Prevent line numbers | |
| alias head2='less -S -n' | |
| alias tail2='less -S +G -n' | |
| # -- Allow quick editing (and reload) of bash configs. | |
| alias bash-profile="nano ~/.bash_profile && echo Use \\'bash-profile-reload\\' to update current session to latest edits" | |
| alias bash-profile-reload="source ~/.bash_profile" | |
| alias bash-rc="nano ~/.bashrc && echo Use \\'bash-rc-reload\\' to update current session to latest edits" | |
| alias bash-rc-reload="source ~/.bashrc" | |
| # -- tmux | |
| alias ta='tmux attach' | |
| # -- just | |
| alias jmenu="just --choose" | |
| alias j="just --unstable" | |
| # -- Send our private key to a remote machine | |
| alias sendpvtkey="ssh-copy-id -i ~/.ssh/id_ed25519.pub " | |
| # -- Fairnames | |
| alias fn='cd "$FN_CODE_DIR"' | |
| # -- Clear DNS cache on linux (macOS has it's own version) | |
| alias flush-dns="sudo systemd-resolve --flush-caches && echo 'Cache cleared. This assumes you are using systemd-resolved DNS services';" | |
| ######################################################## | |
| # Functions (Cross platform) | |
| ######################################################## | |
| function showlan { | |
| ## -- I'm not crazy about this but its the only native way | |
| ## -- Perhaps a custom script is in order. | |
| ## -- See https://www.npmjs.com/package/bonjour-service | |
| if [[ "$OSTYPE" == *darwin* ]]; then | |
| local wait="${1:-2}" | |
| local -a services=("_ssh._tcp:SSH" "_sftp-ssh._tcp:SFTP" "_smb._tcp:SMB" "_http._tcp:Web" "_printer._tcp:Printer" "_ipp._tcp:IPP") | |
| local item type label | |
| for item in "${services[@]}"; do | |
| type="${item%%:*}" | |
| label="${item#*:}" | |
| printf '\n== %s (%s) ==\n' "$label" "$type" | |
| perl -e 'alarm shift; exec @ARGV' "$wait" dns-sd -B "$type" local 2>/dev/null | awk '/Add/ { print; fflush() }' | |
| done | |
| else | |
| avahi-browse -rtp --all | perl -pe 's/\\([0-9]{3})/chr($1)/ge' | grep -E ';IPv4;.*;192\.' | cut -d ';' -f 4-9 | sort -t ';' -k 2 | grep -vE "(Workstation;|\._tcp|\._udp|@)" | tr ';' '\t' | column -t -s $'\t' | |
| fi | |
| } | |
| ######################################################## | |
| # End of .bash_reuse | |
| ######################################################## |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment