Refactor shell setup into ~/.zshenv and ~/.zshrc files

It was time to split the environment related stuff out to ~/.zshenv,
leaving my ~/.zshrc file specifically for setup of interactive shells.
This commit is contained in:
2020-02-22 14:27:19 +00:00
parent d283fc9474
commit 80c77c8c2b
20 changed files with 230 additions and 203 deletions

View File

@@ -32,8 +32,10 @@ SYMLINKS=(
rubocop.yml
tmux
tmux.conf
zshenv
zshrc
)
LOAD_FILES=(zshrc)
LOAD_FILES=()
#
@@ -67,11 +69,6 @@ install_symlinks () {
for i in "${SYMLINKS[@]}"; do
symlink "$SYMLINK_PATH/$i" "$TARGET/.$i"
done
# Symlink shell init file for bash and zsh
for i in "${LOAD_FILES[@]}"; do
symlink "$DOTFILES_LINK/load_shellrc.sh" "$TARGET/.$i"
done
}
install_private () {

View File

@@ -1 +0,0 @@
source "$HOME/.dotfiles/zshrc.zsh"

View File

@@ -27,6 +27,20 @@ alias di="colordiff"
alias devnullsmtp="java -jar $DOTBIN/DevNullSmtp.jar"
alias open_ports="sudo lsof -i -P | grep --color=never -i \"listen\""
# Homebrew
if (( $+commands[brew] )); then
alias br="brew"
alias ca="brew cask"
alias cask="brew cask"
alias bb="brew bundle"
alias bbg="brew bundle --global"
fi
# Flutter
if (( $+commands[flutter] )); then
alias fl="flutter"
fi
# Flush DNS cache
alias flush_dns="dscacheutil -flushcache"

View File

@@ -1,11 +0,0 @@
#
# Android SDK environment setup.
#
if [ -d "$HOME/Library/Android/sdk" ]; then
export ANDROID_HOME="$HOME/Library/Android/sdk"
path_append "$ANDROID_HOME/emulator"
path_append "$ANDROID_HOME/tools"
path_append "$ANDROID_HOME/tools/bin"
path_append "$ANDROID_HOME/platform-tools"
fi

View File

@@ -7,5 +7,5 @@ alias dc="docker-compose"
alias co="docker-compose"
docker_remove_exited () {
docker rm $(docker ps -f="status=exited" -q)
docker rm "$(docker ps -f='status=exited' -q)"
}

View File

@@ -2,7 +2,7 @@
# Emacs
#
# OS X systems.
# macOS systems
if [ -f "/Applications/Emacs.app/Contents/MacOS/Emacs" ]; then
alias emacs="env TERM=screen-24bit /Applications/Emacs.app/Contents/MacOS/Emacs -nw"
fi
@@ -10,8 +10,3 @@ fi
if [ -f "/Applications/Emacs.app/Contents/MacOS/bin/emacsclient" ]; then
alias emacsclient="env TERM=screen-24bit /Applications/Emacs.app/Contents/MacOS/bin/emacsclient"
fi
# *nix systems.
if [ -d "/opt/emacs/bin" ]; then
path_append "/opt/emacs/bin"
fi

View File

@@ -1,12 +0,0 @@
#
# Flutter environment setup
#
if [ -d "/opt/flutter/bin" ]; then
path_append "/opt/flutter/bin"
alias fl="flutter"
if [ -d "/opt/flutter/bin/cache/dart-sdk/bin" ]; then
path_append "/opt/flutter/bin/cache/dart-sdk/bin"
fi
fi

View File

@@ -1,7 +0,0 @@
#
# Use gnu-getopt if it's available
#
if [ -f "/usr/local/opt/gnu-getopt/bin/getopt" ]; then
path_prepend "/usr/local/opt/gnu-getopt/bin"
fi

View File

@@ -1,10 +1,7 @@
#
# Go (golang) environment setup.
# Go (golang) setup.
#
export GOPATH="$HOME/.go"
path_prepend "$GOPATH/bin"
install_go_global_packages () {
local packages=(
github.com/akavel/up

View File

@@ -1,12 +0,0 @@
#
# Homebrew setup.
#
export HOMEBREW_NO_ANALYTICS=1
# Aliases
alias br="brew"
alias ca="brew cask"
alias cask="brew cask"
alias bb="brew bundle"
alias bbg="brew bundle --global"

View File

@@ -6,8 +6,6 @@ alias kc="kubectl"
alias hl="helm"
alias mk="minikube"
export KUBECONFIG="$HOME/.kube/config:.kube/config"
if [ $commands[kubectl] ]; then
if (( $+commands[kubectl] )); then
eval "$(kubectl completion zsh)"
fi

View File

@@ -5,7 +5,7 @@
alias le="less"
# Enable syntax highlighting via source-highlight
if [ $commands[src-hilite-lesspipe.sh] ]; then
if (( $+commands[src-hilite-lesspipe.sh] )); then
export LESSOPEN="| src-hilite-lesspipe.sh %s"
export LESS=" -R "
fi

55
zsh/macos.zsh Normal file
View File

@@ -0,0 +1,55 @@
#
# macOS Specific
#
# Fix wifi issues on OS X 10.10.x Yosemite.
# - from: https://medium.com/@mariociabarra/wifried-ios-8-wifi-performance-issues-3029a164ce94
alias fix_wifi="sudo ifconfig awdl0 down"
alias unfix_wifi="sudo ifconfig awdl0 up"
# Disable the system built-in cmd+ctrl+d global hotkey to lookup word in
# dictionary on OS X. Must reboot after running.
# - from: ://apple.stackexchange.com/a/114269
osx-disable-lookup-word-hotkey () {
defaults write com.apple.symbolichotkeys AppleSymbolicHotKeys -dict-add 70 \
'<dict><key>enabled</key><false/></dict>'
echo "Command-Control-D hotkey disabled. Please reboot to take effect."
}
#
# Power management
#
# Set all relevant power management settings to force the machine to save a
# sleep image and immediately enter "standby" along with FileVault destroying
# disk decryption keys.
pm-hibernate () {
sudo pmset -a hibernatemode 25
sudo pmset -a standby 1
sudo pmset -a standbydelayhigh 0
sudo pmset -a standbydelaylow 0
sudo pmset -a autopoweroffdelay 0
sudo pmset -a destroyfvkeyonstandby 1
}
# Restore all settings modified by pm-hibernate to their defaults, effectively
# restoring default sleep behavior for macOS laptops.
pm-safesleep () {
sudo pmset -a hibernatemode 3
sudo pmset -a standbydelayhigh 86400
sudo pmset -a standbydelaylow 0
sudo pmset -a autopoweroffdelay 28800
sudo pmset -a destroyfvkeyonstandby 0
}
# Trigger hibernation now.
hibernate () {
pm-hibernate
sudo pmset sleepnow
}
# Trigger a safe-sleep now.
safesleep () {
pm-safesleep
sudo pmset sleepnow
}

View File

@@ -40,8 +40,3 @@ install_node_global_packages () {
npm install -g "${packages[@]}"
}
# Load nvm if it's available
if [ -f "$HOME/.nvm/nvm.sh" ]; then
source "$HOME/.nvm/nvm.sh"
fi

View File

@@ -1,69 +0,0 @@
#
# OSX Related
#
if [[ "$(uname)" == "Darwin" ]]; then
# Fix wifi issues on OS X 10.10.x Yosemite.
# - from: https://medium.com/@mariociabarra/wifried-ios-8-wifi-performance-issues-3029a164ce94
alias fix_wifi="sudo ifconfig awdl0 down"
alias unfix_wifi="sudo ifconfig awdl0 up"
# Disable the system built-in cmd+ctrl+d global hotkey to lookup word in
# dictionary on OS X. Must reboot after running.
# - from: ://apple.stackexchange.com/a/114269
osx-disable-lookup-word-hotkey() {
defaults write com.apple.symbolichotkeys AppleSymbolicHotKeys -dict-add 70 \
'<dict><key>enabled</key><false/></dict>'
echo "Command-Control-D hotkey disabled. Please reboot to take effect."
}
# Show hidden files in Finder.
show_files() {
defaults write com.apple.finder AppleShowAllFiles YES
killall Finder "/System/Library/CoreServices/Finder.app"
}
# Don't show hidden files in Finder.
hide_files() {
defaults write com.apple.finder AppleShowAllFiles NO
killall Finder "/System/Library/CoreServices/Finder.app"
}
#
# Power management
#
# Set all relevant power management settings to force the machine to save a
# sleep image and immediately enter "standby" along with FileVault destroying
# disk decryption keys.
pm-hibernate() {
sudo pmset -a hibernatemode 25
sudo pmset -a standby 1
sudo pmset -a standbydelayhigh 0
sudo pmset -a standbydelaylow 0
sudo pmset -a autopoweroffdelay 0
sudo pmset -a destroyfvkeyonstandby 1
}
# Restore all settings modified by pm-hibernate to their defaults, effectively
# restoring default sleep behavior for macOS laptops.
pm-safesleep() {
sudo pmset -a hibernatemode 3
sudo pmset -a standbydelayhigh 86400
sudo pmset -a standbydelaylow 0
sudo pmset -a autopoweroffdelay 28800
sudo pmset -a destroyfvkeyonstandby 0
}
# Trigger hibernation now.
hibernate() {
pm-hibernate
sudo pmset sleepnow
}
# Trigger a safe-sleep now.
safesleep() {
pm-safesleep
sudo pmset sleepnow
}
fi

View File

@@ -40,6 +40,7 @@ alias scu="RAILS_ENV=cucumber bundle exec spring cucumber"
alias va="vagrant"
# Bundler aliases
alias bch="bundle check"
alias bcn="bundle clean"
alias bco="bundle console"
alias be="bundle exec"
@@ -50,10 +51,6 @@ alias bp="bundle package"
alias bu="bundle update"
# lazy-load rbenv
if [ -d "$HOME/.rbenv/shims" ]; then
path_prepend "$HOME/.rbenv/shims"
fi
rbenv() {
eval "$(command rbenv init -)"
rbenv "$@"

View File

@@ -2,11 +2,6 @@
# Rust environment setup.
#
# Rustup
if [ -d "$HOME/.cargo/bin" ]; then
path_prepend "$HOME/.cargo/bin"
fi
install_rust_global_packages() {
local packages=(
rls

View File

@@ -5,11 +5,6 @@ alias tmn="tm new"
alias tml="tm ls"
alias tmm="tmn -s main"
# Custom Install
if [ -d "/opt/tmux/bin" ]; then
path_prepend "/opt/tmux/bin"
fi
# Tmux Completion
if [ -f "/usr/local/etc/bash_completion.d/tmux" ]; then
source "/usr/local/etc/bash_completion.d/tmux"

125
zshenv Normal file
View File

@@ -0,0 +1,125 @@
#
# ZSH Environment Setup
#
# Ensure values in path variable are unique.
typeset -U path
# Path helpers
path_list () {
print -l "${(@)path}"
}
path_remove () {
path=("${(@)path:#$1}")
}
path_append () {
if [ -d "$1" ]; then
path+="$1"
fi
}
path_prepend () {
if [ -d "$1" ]; then
path=("$1" "${(@)path:#$1}")
fi
}
# ==============================================================================
# System Environment Setup
# ==============================================================================
DOTFILES="$HOME/.dotfiles"
DOTBIN="$DOTFILES/bin"
DOTZSH="$DOTFILES/zsh"
# Editors
export EDITOR="emacsclient-wrapper"
export GEM_EDITOR="mate"
# Locale Setup
export LC_ALL="en_US.UTF-8"
export LANG="en_US.UTF-8"
# Ensure TMPDIR is the same for local and remote ssh logins
if [[ "$TMPDIR" == "/var/folders/"* ]] || [[ "$TMPDIR" == "" ]]; then
export TMPDIR="/tmp/user-$USER"
mkdir -p "$TMPDIR"
fi
# Ensure basic systems paths are in desired order.
path_prepend "/sbin"
path_prepend "/bin"
path_prepend "/usr/sbin"
path_prepend "/usr/bin"
path_prepend "/usr/local/sbin"
path_prepend "/usr/local/bin"
# Add dotfiles' bin directory to PATH
path_prepend "$DOTBIN"
# Add user's bin directory to PATH
path_prepend "$HOME/bin"
# ==============================================================================
# Private Dotfiles Environment
# ==============================================================================
DOTPFILES="$DOTFILES/private"
if [ -f "$DOTPFILES/zshenv" ]; then
source "$DOTPFILES/zshenv"
fi
# ==============================================================================
# Third-party Environment Setup
# ==============================================================================
# Android SDK environment setup.
if [ -d "$HOME/Library/Android/sdk" ]; then
export ANDROID_HOME="$HOME/Library/Android/sdk"
path_append "$ANDROID_HOME/emulator"
path_append "$ANDROID_HOME/tools"
path_append "$ANDROID_HOME/tools/bin"
path_append "$ANDROID_HOME/platform-tools"
fi
# Flutter environment setup
path_append "/opt/flutter/bin"
path_append "/opt/flutter/bin/cache/dart-sdk/bin"
# Use gnu-getop if available
path_prepend "/usr/local/opt/gnu-getopt/bin"
# Go (golang) environment setup
export GOPATH="$HOME/.go"
path_prepend "$GOPATH/bin"
# Homebrew setup
export HOMEBREW_NO_ANALYTICS=1
# Kubernetes setup
export KUBECONFIG="$HOME/.kube/config:.kube/config"
# Use custom emacs install if available
path_prepend "/opt/emacs/bin"
# Use custom tmux install if available
path_prepend "/opt/tmux/bin"
# Ruby setup
path_prepend "$HOME/.rbenv/shims"
# Rust setup
path_prepend "$HOME/.cargo/bin"
# ==============================================================================
# Local Overrides
# ==============================================================================
if [ -f "$HOME/.zshenv.local" ]; then
source "$HOME/.zshenv.local"
fi

View File

@@ -2,52 +2,11 @@
# Z-Shell Init
#
if [ -n "$0" ] && [ -f "$0" ]; then
DOTFILES="`dirname \"$0\"`"
elif [ -d "$HOME/.dotfiles" ]; then
DOTFILES="$HOME/.dotfiles"
# Fix PATH re-ordering done by macOS, by loading ~/.zshenv again.
if [[ "$(uname)" == "Darwin" ]] && [ -x "/usr/libexec/path_helper" ]; then
source "$HOME/.zshenv"
fi
# ==============================================================================
# Environment variables
# ==============================================================================
# Export path variables.
DOTPFILES="$DOTFILES/private"
DOTBIN="$DOTFILES/bin"
DOTZSH="$DOTFILES/zsh"
# Path helpers.
source "$DOTZSH/path_helpers.zsh"
# Ensure /usr/local/bin is before various system-paths
path_prepend "/usr/local/bin"
# Editors
export EDITOR="emacsclient-wrapper"
export GEM_EDITOR="mate"
# Locale Setup
export LC_ALL="en_US.UTF-8"
export LANG="en_US.UTF-8"
# ensure bin and sbin paths from /usr/local are in PATH
path_add_after "/usr/local/sbin" "/usr/local/bin"
# ensure bin and sbin paths from /usr are in PATH
path_add_after "/usr/sbin" "/usr/bin"
# Add user's bin directory to PATH
path_prepend "$HOME/bin"
# Add dotfiles' bin directory to PATH
path_prepend "$DOTBIN"
# Ensure TMPDIR is the same for local and remote ssh logins
if [[ $TMPDIR == "/var/folders/"* ]] || [[ $TMPDIR == "" ]]; then
export TMPDIR="/tmp/user-$USER"
mkdir -p "$TMPDIR"
fi
# ==============================================================================
# zplug
@@ -87,6 +46,16 @@ fi
zplug load
# ==============================================================================
# Private Dotfiles
# ==============================================================================
if [ -f "$DOTPFILES/zshrc" ]; then
source "$DOTPFILES/zshrc"
fi
# ==============================================================================
# Completion
# ==============================================================================
@@ -97,29 +66,26 @@ autoload -Uz +X bashcompinit && bashcompinit
fpath=("$DOTZSH/completion" "${fpath[@]}")
# ==============================================================================
# Load custom scripts
# Tool specific setup
# ==============================================================================
# Aliases
source "$DOTZSH/aliases.zsh"
# OS specific
source "$DOTZSH/osx.zsh"
source "$DOTZSH/linux.zsh"
if [[ "$(uname)" == "Darwin" ]]; then source "$DOTZSH/macos.zsh"; fi
if [[ "$(uname)" == "Linux" ]]; then source "$DOTZSH/linux.zsh"; fi
# Utils
source "$DOTZSH/emacs.zsh"
source "$DOTZSH/git.zsh"
source "$DOTZSH/gnu-getopt.zsh"
source "$DOTZSH/homebrew.zsh"
source "$DOTZSH/less.zsh"
source "$DOTZSH/tmux.zsh"
# Development
source "$DOTZSH/android-sdk.zsh"
source "$DOTZSH/docker.zsh"
source "$DOTZSH/flutter.zsh"
source "$DOTZSH/golang.zsh"
source "$DOTZSH/google-cloud.zsh"
source "$DOTZSH/kubernetes.zsh"
@@ -132,6 +98,7 @@ if [ -f "$DOTPFILES/shellrc.sh" ]; then
source "$DOTPFILES/shellrc.sh"
fi
# ==============================================================================
# Basic Z-Shell settings
# ==============================================================================
@@ -144,3 +111,12 @@ unsetopt share_history
# Disable attempted correction of commands (is wrong 98% of the time).
unsetopt correctall
# ==============================================================================
# Local Overrides
# ==============================================================================
if [ -f "$HOME/.zshrc.local" ]; then
source "$HOME/.zshrc.local"
fi