Skip to content

Instantly share code, notes, and snippets.

@pythoninthegrass
Last active May 2, 2024 19:21
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pythoninthegrass/883d0554915579788e4f81c2ff378c34 to your computer and use it in GitHub Desktop.
Save pythoninthegrass/883d0554915579788e4f81c2ff378c34 to your computer and use it in GitHub Desktop.
Bash shell config (~/.bashrc) for Intel and ARM Macs
[[ -s ~/.bashrc ]] && source ~/.bashrc
# Verify that shell is interactive
if [[ $- != *i* ]]; then return; fi
# shellcheck disable=all
# # .bashrc profiling start
# export BASH_XTRACEFD=5
# PS4=":${BASH_SOURCE[0]##*/}:$LINENO+"; set -x
# arch
[[ $(sysctl -n machdep.cpu.brand_string) =~ "Apple M" ]] && arch='arm64' || arch='i386'
# homebrew
[[ $arch = 'arm64' ]] && BREW_PREFIX="/opt/homebrew" || BREW_PREFIX="/usr/local/bin"
export HOMEBREW_NO_INSTALL_CLEANUP=1
export HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK=1
# make
export MAKEFLAGS="--jobs $(sysctl -n hw.ncpu)"
# bash location
arm() { arch -arm64 /opt/homebrew/bin/bash; }
intel() { arch -x86_64 /usr/local/bin/bash; }
# PATH
## BAK (macOS): export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin" || source /etc/profile
export ASDF_DIR="$HOME/.asdf"
export BUN="$HOME/.bun/bin"
export BUN_INSTALL="$HOME/.bun"
export GEM_HOME="$HOME/gems"
export KREW_ROOT="$HOME/.krew"
export MODULAR_HOME="$HOME/.modular"
export MOJO_PATH=$(${BREW_PREFIX}/bin/modular config mojo.path)
export MOJO_BIN="$MOJO_PATH/bin"
export N_PREFIX="$HOME/.n"
export NPM_GLOBAL="$HOME/.npm-global/bin"
export RUST_WITHOUT="rust-docs"
export PATH="$RBENV_DIR/shims:$HOME/.cargo/bin:$ASDF_DIR/shims:$MOJO_BIN:$BUN:$BUN_INSTALL/bin:$HOME/.local/bin:$BREW_PREFIX/bin:$HOME/.rd/bin:$KREW_ROOT/bin:$GEM_HOME/bin:$N_PREFIX/bin:$BREW_PREFIX/bin:/usr/local/opt/gnu-getopt/bin:/usr/local/bin:/usr/local:/usr/local/sbin:$HOME/bin:/usr/bin:/bin:/usr/sbin:/sbin:$PATH"
export PATH=$(echo -n $PATH | awk -v RS=: -v ORS=: '!x[$0]++' | sed "s/\(.*\).\{1\}/\1/")
# shell completions
[[ -f "$ASDF_DIR/asdf.sh" ]] && . "$ASDF_DIR/asdf.sh"
[[ -f "$HOME/.cargo/env" ]] && . "$HOME/.cargo/env"
[[ -f "$HOME/.fzf.bash" ]] && . "$HOME/.fzf.bash"
[[ -f "$HOME/.gitleaks.bash" ]] && . "$HOME/.gitleaks.bash"
[[ -f "$HOME/.hugo.bash" ]] && . "$HOME/.hugo.bash"
# golang
# * cf. https://gist.github.com/felipemeamaral/7ef508e59a42c4fa0d0b74f219f726c1
export GOPATH="$ASDF_DIR/shims"
export GOROOT="$(go env GOROOT)"
export GOCACHE=$HOME/go/cache
export GOPROXY="https://proxy.golang.org,direct"
export PATH="$GOPATH/bin:$PATH"
# rbenv aliases
eval "$(rbenv init - bash)"
# Vim Default Editor
export EDITOR="lvim"
# Tame ansible-lint
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
# GLOBAL BASH HISTORY
# Avoid duplicates
HISTCONTROL=ignoredups:erasedups
# When the shell exits, append to the history file instead of overwriting it
shopt -s histappend
# After each command, append to the history file and reread it
PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND$'\n'}history -a; history -c; history -r"
# Forward search in bash (step forward via ctrl-s)
stty -ixon
# Set default blocksize for ls, df, du
export BLOCKSIZE=1k
# python
# macOS: verify brew symlinks via `brew link --overwrite python`
export PYTHONSTARTUP="$HOME/.config/startup.py"
pip_install() { python -m pip install --user $@; }
pip_uninstall() { python -m pip uninstall -y $@; }
eval "$(register-python-argcomplete pipx)"
export PIPX_DEFAULT_PYTHON="$ASDF_DIR/shims/python"
# thefuck
eval "$(thefuck --alias)"
alias f='fuck'
export THEFUCK_RULES='sudo:no_command'
export THEFUCK_REQUIRE_CONFIRMATION='false'
# starship
colorscript -e random # crunch, random
eval "$(starship init bash)"
# k8s
alias k="kubectl"
alias kc="kubectl config use-context"
alias kns='kubectl config set-context --current --namespace'
alias kgns="kubectl config view --minify --output 'jsonpath={..namespace}' | xargs printf '%s\n'"
export KUBECONFIG="$HOME/.kube/config:$HOME/.kube/k3s.yml:$HOME/.kube/minikube.yml"
source <(kubectl completion bash) # `kc local` if it complains about 'Remote kubernetes server unreachable'
complete -o default -F __start_kubectl k
alias mk="minikube"
# eval $(minikube -p minikube docker-env)
# source <(minikube completion bash)
# bash completion
# ! have to source after kubectl to avoid `_get_comp_words_by_ref` error
[[ -r "/opt/homebrew/etc/profile.d/bash_completion.sh" ]] && . "/opt/homebrew/etc/profile.d/bash_completion.sh"
# argo
alias argo="argo -n argo" # set namespace to 'argo'
# ALIAS
if [[ -f "$HOME/.bash_aliases" ]]; then source "$HOME/.bash_aliases"; fi
alias rsync='rsync -arvW --progress --stats --ignore-existing' # archive, recursive, verbose, whole-file
alias vim='lvim' # lunarvim
alias mkdir='mkdir -pv' # Preferred 'mkdir' implementation
alias ll='ls -FGlAhp' # Preferred 'ls' implementation
alias ..='cd ../' # Go back 1 directory level
alias ...='cd ../../' # Go back 2 directory levels
alias diff='diff -W $(( $(tput cols) - 2 ))' # Full Terminal Width in Side-by-Side Mode
alias flushdns='sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder' # Flush DNS ofc
alias ip="ipconfig getifaddr $(networksetup -listallhardwareports | awk '/Hardware Port: Wi-Fi/{getline; print $2}')" # Private IP address
alias ex-ip='curl icanhazip.com' # Your public IP address
alias open-ports='sudo lsof -i | grep LISTEN' # All listening connections
alias show-blocked='sudo ipfw list' # All ipfw rules inc/ blocked IPs
alias bat='bat --paging=never' # cat clone via go
alias dd='gdd'
alias lzd='lazydocker' # Docker TUI
alias youtube-dl='yt-dlp'
# man pager
export MANPAGER="sh -c 'col -bx | bat -l man -p'"
# FUNCTIONS
cd() { builtin cd "$@"; ll; } # Always list directory contents upon 'cd'
iface() { route get 0.0.0.0 2>/dev/null | awk '/interface: / {print $2}'; } # Network interface
mkpasswd() { docker run -it --rm alpine mkpasswd -m sha-512 "$@"; } # Pull mkpasswd and hash password
lg()
{
export LAZYGIT_NEW_DIR_FILE=~/.lazygit/newdir
lazygit "$@"
if [ -f $LAZYGIT_NEW_DIR_FILE ]; then
cd "$(cat $LAZYGIT_NEW_DIR_FILE)"
rm -f $LAZYGIT_NEW_DIR_FILE > /dev/null
fi
}
# aws
export AWS_VAULT_PROMPT=terminal
export AWS_DEFAULT_PROFILE="dev.use1"
# silence zsh shenanigans on 10.15+
if [[ $(sw_vers -buildVersion) =~ "21G72" ]]; then
export BASH_SILENCE_DEPRECATION_WARNING=1
fi
# playwright (firewall prompt)
alias sign-playwright="sudo codesign --force --deep \
--sign - $HOME/Library/Caches/ms-playwright/chromium-*/chrome-mac/Chromium.app"
# terraform
alias tf='AWS_PROFILE="" terraform'
alias tfi='AWS_PROFILE="" terraform init -backend-config=./state.conf'
alias tfa='AWS_PROFILE="" terraform apply'
alias tfp='AWS_PROFILE="" terraform plan'
alias tfpn='AWS_PROFILE="" terraform plan -refresh=false'
# multipass
alias m='multipass'
# zoxide (`cd` killer)
eval "$(zoxide init --cmd cd bash)"
# # .bashrc profiling end
# set +x

intel & arm setup

env arch -x86_64 /bin/bash
/usr/sbin/softwareupdate --install-rosetta --agree-to-license
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
rm /usr/local/bin/bash  # if alias was previously installed
brew install bash
ln -s /usr/local/Cellar/bash/5.1.16/bin/bash /usr/local/bin/bash
arm
arch; which bash
intel
arch; which bash
git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
~/.fzf/install
asdf plugin add zoxide https://github.com/nyrst/asdf-zoxide.git
asdf install zoxide latest
asdf global zoxide latest

python ipython repl w/rich

# install ipython, rich et al
python -m pip install -r ~/.config/requirements.txt

# create ~/.config/startup.py

rust

# install
asdf plugin-add rust https://github.com/asdf-community/asdf-rust.git
asdf install rust latest

# rust cargo env
mkdir -p ~/.cargo/env && cp env $_
[filter "lfs"]
clean = git-lfs clean -- %f
smudge = git-lfs smudge -- %f
process = git-lfs filter-process
required = true
[user]
name = pythoninthegrass
email = 4097471+pythoninthegrass@users.noreply.github.com
[alias]
switch = !legit switch
sync = !legit sync
publish = !legit publish
unpublish = !legit unpublish
undo = !legit undo
branches = !legit branches
parent = "!git show-branch | grep '*' | grep -v \"$(git rev-parse --abbrev-ref HEAD)\" | head -n1 | sed 's/.*\\[\\(.*\\)\\].*/\\1/' | sed 's/[\\^~].*//' #"
amend = commit --amend --no-edit
[credential]
helper = cache --timeout=28800
[maintenance]
repo = ~/git/automate_boring_stuff
repo = ~/git/meetup_bot
repo = ~/git/mvp
repo = ~/git/pre-commit-hooks
repo = ~/git/python_template
[http]
postBuffer = 524288000
[core]
pager = delta
excludesfile = /Users/lance/.gitignore
[interactive]
diffFilter = delta --color-only
[delta]
navigate = true
[init]
defaultBranch = main
[merge]
conflictstyle = diff3
tool = meld
[diff]
tool = meld
colorMoved = default
[pull]
rebase = false
[fetch]
prune = true
[push]
autoSetupRemote = true
client_secret.json
.env
scratch.*
*.pem
# MACOS
# General
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
# SOURCES:
# https://gist.github.com/spicycode/1229612
# https://github.com/olalonde/dotfiles/blob/master/tmux.conf
# https://github.com/samoshkin/tmux-config/blob/master/tmux/tmux.conf
# https://unix.stackexchange.com/a/255343
# General
set -g default-terminal "screen-256color"
set -g history-limit 20000
set -g buffer-limit 20
set -sg escape-time 0
set -g display-time 1500
set -g remain-on-exit off
set -g repeat-time 300
set -g status off
setw -g allow-rename on
setw -g automatic-rename on
setw -g aggressive-resize on
setw -g mode-mouse on
setw -g monitor-activity on
# Set parent terminal title to reflect current window in tmux session
set -g set-titles on
set -g set-titles-string "#I:#W"
# Start index of window/pane with 1, because we're humans, not computers
set -g base-index 1
setw -g pane-base-index 1
# Enable mouse support
set -g mouse on
# Copy mode, scroll and clipboard
set -g @copy_use_osc52_fallback on
set -g status-keys vi
setw -g mode-keys vi
# Unbind default key bindings, we're going to override
# unbind "\$" # rename-session
# unbind , # rename-window
# unbind % # split-window -h
# unbind '"' # split-window
# unbind } # swap-pane -D
# unbind { # swap-pane -U
# unbind [ # paste-buffer
# unbind ]
# unbind "'" # select-window
# unbind n # next-window
# unbind p # previous-window
# unbind l # last-window
# unbind M-n # next window with alert
# unbind M-p # next window with alert
# unbind o # focus thru panes
# unbind & # kill-window
# unbind "#" # list-buffer
# unbind = # choose-buffer
# unbind z # zoom-pane
# unbind M-Up # resize 5 rows up
# unbind M-Down # resize 5 rows down
# unbind M-Right # resize 5 rows right
# unbind M-Left # resize 5 rows left
# unbind all keys
unbind-key -a
source-file ~/.tmux.reset.conf
# rebind prefix (C-b) to Control-Space
set -g prefix C-Space
bind-key C-Space select-pane -t :.+
# Edit configuration and reload
bind C-e new-window -n 'tmux.conf' "sh -c '\${EDITOR:-vim} ~/.tmux.conf && tmux source ~/.tmux.conf && tmux display \"Config reloaded\"'"
# Reload tmux configuration
bind C-r source-file ~/.tmux.conf \; display "Config reloaded"
# new window and retain cwd
bind c new-window -c "#{pane_current_path}"
# Prompt to rename window right after it's created
set-hook -g after-new-window 'command-prompt -I "#{window_name}" "rename-window '%%'"'
# Rename session and window
bind r command-prompt -I "#{window_name}" "rename-window '%%'"
bind R command-prompt -I "#{session_name}" "rename-session '%%'"
# Split panes
bind | split-window -h -c "#{pane_current_path}"
bind _ split-window -v -c "#{pane_current_path}"
# Select pane and windows
bind -r C-[ previous-window
bind -r C-] next-window
bind -r [ select-pane -t :.-
bind -r ] select-pane -t :.+
bind -r Tab last-window # cycle thru MRU tabs
bind -r C-o swap-pane -D
# Zoom pane
bind + resize-pane -Z
# Link window
bind L command-prompt -p "Link window from (session:window): " "link-window -s %% -a"
# Swap panes back and forth with 1st pane
# When in main-(horizontal|vertical) layouts, the biggest/widest panel is always @1
bind \ if '[ #{pane_index} -eq 1 ]' \
'swap-pane -s "!"' \
'select-pane -t:.1 ; swap-pane -d -t 1 -s "!"'
# Kill pane/window/session shortcuts
bind x kill-pane
bind X kill-window
bind C-x confirm-before -p "kill other windows? (y/n)" "kill-window -a"
bind Q confirm-before -p "kill-session #S? (y/n)" kill-session
# Merge session with another one (e.g. move all windows)
# If you use adhoc 1-window sessions, and you want to preserve session upon exit
# but don't want to create a lot of small unnamed 1-window sessions around
# move all windows from current session to main named one (dev, work, etc)
bind C-u command-prompt -p "Session to merge with: " \
"run-shell 'yes | head -n #{session_windows} | xargs -I {} -n 1 tmux movew -t %%'"
# Detach from session
bind d detach
bind D if -F '#{session_many_attached}' \
'confirm-before -p "Detach other clients? (y/n)" "detach -a"' \
'display "Session has only 1 client attached"'
# Hide status bar on demand
bind C-s if -F '#{s/off//:status}' 'set status off' 'set status on'
#!/usr/bin/env bash
tmux -f /dev/null -L temp start-server \; list-keys | \
sed -r \
-e "s/bind-key(\s+)([\"#~\$])(\s+)/bind-key\1\'\2\'\3/g" \
-e "s/bind-key(\s+)([\'])(\s+)/bind-key\1\"\2\"\3/g" \
-e "s/bind-key(\s+)([;])(\s+)/bind-key\1\\\\\2\3/g" \
-e "s/command-prompt -I #([SW])/command-prompt -I \"#\1\"/g" \
> ~/.tmux.reset.conf
#!/bin/sh
# rustup shell setup
# affix colons on either side of $PATH to simplify matching
case ":${PATH}:" in
*:"$HOME/.cargo/bin":*)
;;
*)
# Prepending path in case a system-installed rustc needs to be overridden
export PATH="$HOME/.cargo/bin:$PATH"
;;
esac
ansible-core==2.14.4
ansible-lint==6.14.3
ansible==7.3.0
appnope==0.1.3
asttokens==2.1.0
backcall==0.2.0
colorama==0.4.6
commonmark==0.9.1
cryptography==40.0.1
filelock==3.10.7
decorator==5.1.1
executing==1.2.0
icecream==2.1.3
ipython==8.11.0
jedi==0.18.1
jinja2==3.1.2
matplotlib-inline==0.1.6
numpy==1.23.4
pandas==1.5.1
parso==0.8.3
pathvalidate==2.5.2
pexpect==4.8.0
pickleshare==0.7.5
pipx==1.2.0
prompt-toolkit==3.0.32
ptyprocess==0.7.0
pure-eval==0.2.2
pygments==2.13.0
pypng
pyqrcode
python-dateutil==2.8.2
pytz==2022.6
pyyaml==6.0
rich==13.3.3
six==1.16.0
stack-data==0.6.0
traitlets==5.5.0
wcwidth==0.2.5
yamllint==1.30.0
#!/usr/bin/env python3
# https://ipython.readthedocs.io/en/stable/interactive/reference.html#ipython-as-your-default-python-environment
# https://github.com/Textualize/rich#rich-repl
import os, IPython
from rich.jupyter import print
os.environ['PYTHONSTARTUP'] = '' # Prevent running this again
IPython.start_ipython()
raise SystemExit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment