diff --git a/install.sh b/install.sh index 69465e9..a76a694 100755 --- a/install.sh +++ b/install.sh @@ -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 () { diff --git a/load_shellrc.sh b/load_shellrc.sh deleted file mode 100644 index 11e8f70..0000000 --- a/load_shellrc.sh +++ /dev/null @@ -1 +0,0 @@ -source "$HOME/.dotfiles/zshrc.zsh" diff --git a/zsh/aliases.zsh b/zsh/aliases.zsh index 658cc2f..71c2a85 100644 --- a/zsh/aliases.zsh +++ b/zsh/aliases.zsh @@ -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" diff --git a/zsh/android-sdk.zsh b/zsh/android-sdk.zsh deleted file mode 100644 index 4526d51..0000000 --- a/zsh/android-sdk.zsh +++ /dev/null @@ -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 diff --git a/zsh/docker.zsh b/zsh/docker.zsh index 2c93173..bba8492 100644 --- a/zsh/docker.zsh +++ b/zsh/docker.zsh @@ -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)" } diff --git a/zsh/emacs.zsh b/zsh/emacs.zsh index 5160b6f..ff73b12 100644 --- a/zsh/emacs.zsh +++ b/zsh/emacs.zsh @@ -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 diff --git a/zsh/flutter.zsh b/zsh/flutter.zsh deleted file mode 100644 index 73cec75..0000000 --- a/zsh/flutter.zsh +++ /dev/null @@ -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 diff --git a/zsh/gnu-getopt.zsh b/zsh/gnu-getopt.zsh deleted file mode 100644 index e819395..0000000 --- a/zsh/gnu-getopt.zsh +++ /dev/null @@ -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 diff --git a/zsh/golang.zsh b/zsh/golang.zsh index 1c83732..54130c5 100755 --- a/zsh/golang.zsh +++ b/zsh/golang.zsh @@ -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 diff --git a/zsh/homebrew.zsh b/zsh/homebrew.zsh deleted file mode 100644 index 67f6459..0000000 --- a/zsh/homebrew.zsh +++ /dev/null @@ -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" diff --git a/zsh/kubernetes.zsh b/zsh/kubernetes.zsh index 22b30c0..c02b876 100644 --- a/zsh/kubernetes.zsh +++ b/zsh/kubernetes.zsh @@ -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 diff --git a/zsh/less.zsh b/zsh/less.zsh index 4dbc28d..6e66b41 100644 --- a/zsh/less.zsh +++ b/zsh/less.zsh @@ -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 diff --git a/zsh/macos.zsh b/zsh/macos.zsh new file mode 100644 index 0000000..ef05648 --- /dev/null +++ b/zsh/macos.zsh @@ -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 \ + 'enabled' + 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 +} diff --git a/zsh/nodejs.zsh b/zsh/nodejs.zsh index 60bf938..570d66c 100755 --- a/zsh/nodejs.zsh +++ b/zsh/nodejs.zsh @@ -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 diff --git a/zsh/osx.zsh b/zsh/osx.zsh deleted file mode 100644 index 7de968a..0000000 --- a/zsh/osx.zsh +++ /dev/null @@ -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 \ - 'enabled' - 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 diff --git a/zsh/ruby.zsh b/zsh/ruby.zsh index d02df35..6639f22 100755 --- a/zsh/ruby.zsh +++ b/zsh/ruby.zsh @@ -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 "$@" diff --git a/zsh/rust.zsh b/zsh/rust.zsh index 473f360..ee9956e 100644 --- a/zsh/rust.zsh +++ b/zsh/rust.zsh @@ -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 diff --git a/zsh/tmux.zsh b/zsh/tmux.zsh index 1ce80f6..0f10d78 100644 --- a/zsh/tmux.zsh +++ b/zsh/tmux.zsh @@ -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" diff --git a/zshenv b/zshenv new file mode 100644 index 0000000..64db438 --- /dev/null +++ b/zshenv @@ -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 diff --git a/zshrc.zsh b/zshrc similarity index 67% rename from zshrc.zsh rename to zshrc index 8533952..3a9c34e 100644 --- a/zshrc.zsh +++ b/zshrc @@ -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