# Return immediately if non-interactive (makes FTP clients happy) [[ "$-" == *i* ]] || return ############################################################################## # Customize environment ############################################################################## export XDG_CACHE_HOME="$HOME/.cache" export XDG_CONFIG_HOME="$HOME/.config" export XDG_DATA_HOME="$HOME/.local/share" export EDITOR="vim" export INPUTRC="$XDG_CONFIG_HOME/readline/inputrc" export LANG="en_US.UTF-8" export LANGUAGE="en_US" export LC_ALL="en_US.UTF-8" export LC_CTYPE="en_US.UTF-8" export LESS="-i -j.49 -M -R -z-2" export LESSHISTFILE="$XDG_CACHE_HOME/less/history" export LESSHISTSIZE=1000 export LOCAL_PREFIX="/usr/local" [ command -v brew &>/dev/null ] && LOCAL_PREFIX=$(brew --prefix) export MANPATH="$(MANPATH='' manpath)" export PAGER=less export PYTHONSTARTUP="$XDG_CONFIG_HOME/python/startup.py" export VIMINIT='let $MYVIMRC="$XDG_CONFIG_HOME/vim/vimrc" | source $MYVIMRC' ############################################################################## # Customize PATH (and MANPATH) ############################################################################## # Prevent path_helper from messing with the PATH when starting tmux. # See: https://superuser.com/a/583502 [ "$(uname)" == "Darwin" ] && { PATH=""; source /etc/profile; } prepend_dir() { # 1: dir to add, 2: variable to manipulate [ -d "$1" -a -n "$2" ] || return local list="${!2}" # capture current value list=${list#"$1:"} # remove dir from beginning list=${list//":$1:"/:} # remove dir from middle list=${list%":$1"} # remove dir from end printf -v "$2" "$1${list:+":$list"}" # add in front (use : only if empty) } # Add custom bin dirs to PATH if they exist and are not already in PATH. while read -r dir; do prepend_dir "$dir" PATH; done </dev/null ############################################################################## # Customize shell aliases ############################################################################## # Make `ls` group directories first if supported. if ls --group-directories-first &>/dev/null; then alias ls="ls -hF --group-directories-first --color=auto" # GNU else alias ls="ls -hF -G" # BSD fi # Force `ls` to use color output (e.g. for piping into `less`). if ls --color=auto &>/dev/null; then alias lsc="ls --color=always" # GNU else alias lsc="/usr/bin/env CLICOLOR_FORCE=1 ls" # BSD fi alias la="ls -a" alias ll="ls -l" alias llc="lsc -l" alias lla="ls -la" alias llac="lsc -la" alias grep="grep --color=auto"; alias egrep="egrep --color=auto"; alias fgrep="fgrep --color=auto"; alias path='echo $PATH | tr -s ":" "\n"' alias mpath='echo $MANPATH | tr -s ":" "\n"' alias timer='echo "Timer started. Stop with Ctrl-D." && date && time cat && date' alias tmux='tmux -f "$XDG_CONFIG_HOME/tmux/tmux.conf"' # Head and tail as much as fits on screen alias head='head -n $((${LINES:-15}-5))' alias tail='tail -n $((${LINES:-15}-5))' # A few options to get public IP address on command line. The dig solution # below using the OpenDNS resolver doesn't work when connected to # ExpressVPN because all DNS requests are handled by the ExpressVPN DNS # servers and the OpenDNS DNS resolver is blocked. alias ipinfo="curl -s ipinfo.io" alias myip="curl -s https://ifconfig.co" #alias myip="curl -s https://ifconfig.me" #alias myip="dig +short myip.opendns.com @resolver1.opendns.com" alias light='_update_colors light' alias dark='_update_colors dark' _send_osc() { local OSC ST if [ -n "$TMUX" ]; then OSC=$'\ePtmux;\e\e]' ST=$'\e\e\\\e\\'; else OSC=$'\e]' ST=$'\e\\' fi echo -n "$OSC$1$ST" } _update_colors() { local cursor fg bg n rgb export BACKGROUND="$1" if [ "$BACKGROUND" = "light" ]; then PS1_SEP_COLOR=$(tput setaf $Base1) else PS1_SEP_COLOR=$(tput setaf $Base01) fi cursor=$Red_RGB if [ "$BACKGROUND" = "light" ]; then fg=$Base01_RGB bg=$Base3_RGB else fg=$Base1_RGB bg=$Base03_RGB fi if [ -n "$ITERM_SESSION_ID" ]; then # iTerm2 while read -r n rgb; do _send_osc "P$n$rgb"; done </dev/null && [ -f "$ls_colors" ]; then eval "$(dircolors "$ls_colors")" fi } ############################################################################## # Add shell functions ############################################################################## tree() { command tree --dirsfirst -FI '.git|Spotlight-V100|.fseventsd' "$@"; } ltree() { tree -C "$@" | less -R; } # Combined mkdir and cd mkcd() { mkdir -p -- "$1" && cd -P -- "$1" || return; } # Colorized `man` man() { local rst standout bold underline rst=$(tput sgr0) if [ "$BACKGROUND" = "light" ]; then standout=$(tput -S <<<"$(echo -e "setaf $Base3\nsetab $Cyan")") bold=$(tput -S <<<"$(echo -e "setaf $Blue")") underline=$(tput -S <<<"$(echo -e "setaf $Base02\nsmul")") else standout=$(tput -S <<<"$(echo -e "setaf $Base03\nsetab $Cyan")") bold=$(tput -S <<<"$(echo -e "setaf $Yellow")") underline=$(tput -S <<<"$(echo -e "setaf $Base3\nsmul")") fi LESS_TERMCAP_so=$standout \ LESS_TERMCAP_md=$bold \ LESS_TERMCAP_us=$underline \ LESS_TERMCAP_se=$rst \ LESS_TERMCAP_me=$rst \ LESS_TERMCAP_ue=$rst \ GROFF_NO_SGR=1 \ command man "$@" } solarized() { local rst name name_rgb rgb fg bg rst=$(tput sgr0) for name in Red Orange Yellow Green Cyan Blue Violet Magenta Base{0{3..0},{0..3}} do name_rgb=${name}_RGB i= rgb=${!name_rgb} fg=$(tput setaf "${!name}") bg=$(tput setab "${!name}") printf "$bg%6s$rst $fg#$rgb %2d $name$rst\n" '' "${!name}" done } # Print all 256 colors colortest() { local i j rst rst=$(tput sgr0) for i in $(seq 0 15); do for j in $(seq 0 15); do local n=$(( 16 * i + j )) printf "$(tput setab $n) %3d " $n done echo "$rst" done } ############################################################################## # Run external customizations ############################################################################## stty -ixon # disable ctrl-s and ctrl-q _update_colors "${BACKGROUND:-dark}" # Enable available completion helpers while read -r d; do [ -d "$d" ] && for f in "$d"/*; do [ -f "$f" ] && . "$f"; done; done <