[zsh] Update shell init: rc

This commit is contained in:
Eryn Wells 2021-12-31 11:54:55 -08:00
parent b09d523218
commit c30b46a88b
26 changed files with 479 additions and 462 deletions

10
zsh/func/binary_exists Normal file
View file

@ -0,0 +1,10 @@
# .zshrc
# vim:ft=zsh:
# Eryn Wells <eryn@erynwells.me>
function binary_exists
{
return $(hash $1 1>/dev/null 2>&1)
}
binary_exists "$@"

View file

@ -0,0 +1,24 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
autoload darwin_icloud_drive_path
function darwin_configure_screenshots_directory
{
icloud=`darwin_icloud_drive_path`
if [[ ! -d "$icloud" ]]; then
shell-log -l error "iCloud directory doesn't exist: $icloud"
return 1
fi
# Put screenshots in iCloud Drive, in a directory according to hostname
local name=`hostname -s | tr A-Z a-z`
local screenshots_dir="$icloud/Screenshots/$name"
shell-log "Setting screenshot directory: $screenshots_dir"
mkdir -p "$screenshots_dir"
defaults write com.apple.screencapture screenshots_diration "$screenshots_dir"
return 0
}
darwin_configure_screenshots_directory "$@"

View file

@ -0,0 +1,9 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
function darwin_icloud_drive_path
{
echo "$HOME/Library/Mobile Documents/com~apple~CloudDocs"
}
darwin_icloud_drive_path "$@"

21
zsh/func/darwin_init_once Normal file
View file

@ -0,0 +1,21 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
autoload darwin_icloud_drive_path
autoload darwin_configure_screenshots_directory
function darwin_init_once
{
# Dim dock icons of apps that have been hidden.
shell-log "Auto-hiding Dock"
defaults write com.apple.Dock showhidden -boolean yes
killall Dock
darwin_configure_screenshots_directory
# See https://techstuffer.com/this-app-is-damaged-error-macos-sierra/
shell-log "Disabling overly restrictive Gatekeeper"
sudo spctl --master-disable
}
darwin_init_once "$@"

14
zsh/func/g Normal file
View file

@ -0,0 +1,14 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
function g
{
if [[ $# -gt 0 ]]; then
git $@
else
git status --short --branch
fi
return $?
}
g "$@"

View file

@ -0,0 +1,17 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
autoload binary_exists
function init_app_environments
{
# NetHack options
# use color in the terminal
binary_exists nethack && export NETHACKOPTIONS="color"
# Default ledger file
local ledgerFile="$HOME/Documents/Ledger/personal.ledger"
[[ -e "$ledgerFile" ]] && LEDGER_FILE="$ledgerFile"
}
init_app_environments "$@"

43
zsh/func/init_rc_aliases Normal file
View file

@ -0,0 +1,43 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
autoload binary_exists
function init_rc_aliases
{
shell-log -l 'debug' "Setting up shell aliases"
alias j='jobs'
alias h='history'
alias df='df -h'
alias du='du -h'
alias e='emacs'
alias chux='chmod u+x'
alias chuw='chmod u+w'
alias chur='chmod u+r'
alias cho="chown $USER"
alias today='date +%Y-%m-%d'
alias addkey="ssh-agent ~/.ssh/id_rsa"
alias pprint="python -c 'import sys,pprint; pprint.pprint(eval(sys.stdin.read()))'"
alias pprint-json="python -c 'import sys,json;print json.dumps(json.load(sys.stdin), indent=2)'"
alias gp='g p origin $gitbranch'
alias gpf='g p -f origin $gitbranch'
alias bx='bundle exec'
binary_exists ledger && alias l='ledger'
binary_exists gpg2 && alias gpg='gpg2'
shell-log -l debug 'Setting up suffix aliases'
alias -s c='vim'
alias -s tex='vim'
alias -s txt='vim'
alias -s xml='vim'
alias -s jar='java -jar'
}
init_rc_aliases "$@"

View file

@ -0,0 +1,43 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
function init_rc_configure_completion
{
shell-log 'Initializing completion system'
autoload -U compinit
compinit
# Cache completions
zstyle ':completion::complete:*' use-cache 1
zstyle ':completion::complete:*' cache-path ~/.zsh/cache
zstyle ':completion:*:default' list-colors ${(s.:.)LS_COLORS}
# For rm, cp, and mv don't complete if file is on the line already
zstyle ':completion:*:rm:*' ignore-line yes
zstyle ':completion:*:cp:*' ignore-line yes
zstyle ':completion:*:mv:*' ignore-line yes
# Remove trailing slashes in directory arguments
zstyle ':completion:*' squeeze-slashes true
# Never select parent directory
zstyle ':completion:*:cd:*' ignore-parents parent pwd
# Expand partial paths
zstyle ':completion:*' expand 'yes'
# Show a pretty menu of killable processes
zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([0-9]#)*=0=01;31'
zstyle ':completion:*:*:kill:*' menu yes select
# Complete man pages by section
zstyle ':completion:*:manuals' separate-sections true
zstyle ':completion:*:manuals.*' insert-sections true
# Use git completion for the g function
compdef g='git'
}
init_rc_configure_completion "$@"

View file

@ -0,0 +1,41 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
function init_configure_ls
{
local has_gnu_ls
local ls_options
if [[ ! -e "$1" ]]; then
shell-log -l error 'Missing required path to ls binary'
return
fi
if $1 --version 2>&1 | grep GNU 1>/dev/null; then
has_gnu_ls=1
ls_options='--color=auto'
else
has_gnu_ls=0
ls_options='-G'
fi
shell-log -l debug "Setting up aliases for $1"
alias ls="$1 $ls_options"
alias la="$1 -A $ls_options"
alias ll="$1 -l $ls_options"
alias l.="$1 -d $ls_options .*"
local dircolors_bin=`whence -p dircolors || whence -p gdircolors`
if [[ $has_gnu_ls -eq 1 && -n "$dircolors_bin" ]]; then
if [[ -e "$HOME/.dircolors/$SYS.cfg" ]]; then
dircolors="$HOME/.dircolors/$SYS.cfg"
else
dircolors="$HOME/.dircolors/default.cfg"
fi
shell-log -l debug "Setting up dircolors: `basename $dircolors`"
eval `$dircolors_bin $dircolors`
fi
}
init_configure_ls "$@"

View file

@ -0,0 +1,37 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
function init_rc_configure_prompt
{
local theme=$1
if [[ -z "$1" ]]; then
theme=loquacious
fi
shell-log -l debug 'Configuring vcs_info'
autoload -U add-zsh-hook
autoload -Uz vcs_info
zstyle ':vcs_info:*' disable p4 bzr cdv darcs mtn svk tla cvs svn
zstyle ':vcs_info:*' enable git
zstyle ':vcs_info:git:general:*' formats '%b'
# Export the current Git branch before every prompt.
function export_gitbranch {
vcs_info general
if [[ "$gitbranches[1]" != "${vcs_info_msg_0_}" ]]; then
export gitbranches=(${vcs_info_msg_0_} $gitbranches[1,4])
fi
export gitbranch=${vcs_info_msg_0_}
}
add-zsh-hook precmd export_gitbranch
shell-log "Configuring prompt with '$theme' theme"
autoload -U promptinit
promptinit
prompt $theme
}
init_rc_configure_prompt "$@"

View file

@ -0,0 +1,27 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
function init_rc_configure_zle
{
shell-log 'Configuring ZLE'
local mode=$1
if [[ -z "$mode" ]]; then
mode=emacs
fi
function configure_zle_emacs { }
function configure_zle_vim { }
shell-log -l debug "Using $mode command line editing mode"
if [[ $mode == 'vim' ]]; then
bindkey -v
configure_zle_vim
zle -A .backward-delete-char vi-backward-delete-char
elif [[ $mode == 'emacs' ]]; then
bindkey -e
configure_zle_emacs
fi
}
init_rc_configure_zle "$@"

38
zsh/func/init_rc_darwin Normal file
View file

@ -0,0 +1,38 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
autoload binary_exists
function init_rc_darwin
{
shell-log 'Initializing Interactive Environment for macOS'
alias acls='command ls -le'
# These things might have been installed by Homebrew, and I like the GNU
# versions better.
binary_exists gdircolors && alias dircolors='gdircolors'
binary_exists gfind && alias find='gfind'
binary_exists gnuindent && alias indent='gnuindent'
binary_exists gls && init_rc_configure_ls `which gls`
# ldd doesn't exist on OS X, but otool -L does the same thing.
alias ldd='otool -L'
local sounds=/System/Library/Sounds
alias glass="afplay $sounds/Glass.aiff"
alias funk="afplay $sounds/Funk.aiff"
autoload +X darwin_init_once
autoload +X darwin_icloud_drive_path
autoload +X darwin_configure_screenshots_directory
# Open a file/folder in Finder
alias reveal='open -R'
alias -s app='open'
alias -s xcodeproj='open -a Xcode'
}
init_rc_darwin "$@"

13
zsh/func/init_rc_linux Normal file
View file

@ -0,0 +1,13 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
function init_rc_linux
{
shell-log 'Initializing Interactive Environment for Linux'
alias iptls='sudo iptables --line-numbers -nv -L'
alias ip6tls='sudo ip6tables --line-numbers -nv -L'
alias rlx="xrdb $HOME/.Xdefaults"
}
init_rc_linux "$@"

View file

@ -0,0 +1,25 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
autoload load_module
function init_zsh_functions
{
local myfpath="$HOME/.zsh/func"
shell-log "Loading functions in $myfpath"
for func in $myfpath/*; do
[[ ! -e "$func" || -d "$func" ]] && continue
local functionName=`basename $func`
[[ "$functionName" =~ "prompt_*" ]] && continue
[[ "$functionName" =~ "init_*" ]] && continue
shell-log -l debug "Loading $functionName"
autoload +X $functionName
done
load_module makers
}
init_zsh_functions "$@"

23
zsh/func/init_zsh_history Normal file
View file

@ -0,0 +1,23 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
function init_zsh_history
{
shell-log 'Setting up history'
setopt \
APPEND_HISTORY \
EXTENDED_HISTORY \
INC_APPEND_HISTORY \
HIST_FIND_NO_DUPS \
HIST_IGNORE_SPACE \
HIST_NO_STORE \
HIST_IGNORE_DUPS \
HIST_REDUCE_BLANKS
HISTSIZE=1000000
SAVEHIST=1000000
HISTFILE="$HOME/.zhistory"
}
init_zsh_history "$@"

18
zsh/func/init_zsh_options Normal file
View file

@ -0,0 +1,18 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
function init_zsh_options
{
shell-log 'Setting shell options'
# Report seconds since shell was invoked in milliseconds
typeset -F SECONDS
# See zshoptions(1)
setopt EXTENDED_GLOB \
MULTIOS \
AUTO_REMOVE_SLASH \
COMPLETE_IN_WORD
}
init_zsh_options "$@"

31
zsh/func/load_module Normal file
View file

@ -0,0 +1,31 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
function load_module
{
local mod=$1
local modpath
for p in $fpath; do
modpath=$p/$mod
[[ -d $modpath ]] && break
modpath=''
done
if [[ -z "$modpath" ]]; then
shell-log -l error "Couldn't find path to module: $mod"
return 1
fi
fpath+=($modpath)
shell-log "Loading module: $mod"
for func in `ls $modpath`; do
shell-log "Loading function: $func"
autoload $func
done
return 0
}
load_module "$@"

15
zsh/func/vi Normal file
View file

@ -0,0 +1,15 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
function vi
{
if whence -cp nvim 1>/dev/null 2>&1; then
command nvim $@
elif whence -cp vim 1>/dev/null 2>&1; then
command vim $@
elif whence -cp vi 1>/dev/null 2>&1; then
command vi $@
fi
}
vi "$@"