# shellcheck shell=bash # 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_CONFIG="$HOME/.local/etc" export LOCAL_PREFIX="/usr/local" if command -v brew &>/dev/null; then LOCAL_PREFIX="$(brew --prefix)" export HOMEBREW_NO_ANALYTICS=1 export HOMEBREW_NO_AUTO_UPDATE=1 fi MANPATH="$(unset MANPATH; manpath)" export MANPATH export PAGER=less export PYTHONSTARTUP="$XDG_CONFIG_HOME/python/startup.py" # shellcheck disable=SC2016 # This expression is to be interpreted by vim, not bash. 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 # shellcheck disable=SC2123 # PATH is being intentionally manipulated here. # shellcheck disable=SC1091 # /etc/profile is provided by macOS. [ "$(uname)" == "Darwin" ] && { PATH=""; source /etc/profile; } _prepend_path() { # 1: dir to add, 2: path variable to manipulate if [ -d "$1" ] && [ -n "$2" ]; then local _path="${!2}" # get path variable value case ":$_path:" in *":$1:"*) :;; # dir already in path, noop (:) *) _path="$1${_path:+:}$_path";; # prepend (adding : if not empty) esac printf -v "$2" "%s" "$_path" # write back to path variable fi } # Add custom bin dirs to PATH if they exist and are not already in PATH. while read -r dir; do _prepend_path "$dir" PATH; done </dev/null ############################################################################## # Customize shell aliases ############################################################################## # ls: make `ls` group directories first if supported. # lsc: force `ls` to use color output (e.g. for piping into `less`). if ls --group-directories-first --color=auto &>/dev/null 2>&1; then # GNU ls alias ls="ls -hF --group-directories-first --color=auto" alias lsc="ls --color=always" else # BSD ls (e.g. macOS) alias ls="ls -hF -G" alias lsc="/usr/bin/env CLICOLOR_FORCE=1 ls" fi alias la="ls -a" alias ll="ls -l" alias lla="ls -la" alias llc="lsc -l" 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' ############################################################################## # Terminal color management ############################################################################## # Useful terminal control sequences: # # Sequence Abbrev Description # ----- ----- ----------------------------- # ESC P DCS Device Control String # ESC [ CSI Control Sequence Introducer # ESC \ ST String Terminator # ESC ] OSC Operating System Control # # # More info at: # * https://www.xfree86.org/4.8.0/ctlseqs.html # * https://vt100.net/ # * https://iterm2.com/utilities/imgcat # In tmux, OSC sequences can be forwarded to the underlying terminal by: # * Wrapping the sequence with `DCS tmux; ST` # * Replacing ESC in with ESC ESC. _osc_start() { if [ -n "$TMUX" ]; then printf "\ePtmux;\e\e]" else printf "\e]" fi } # Either ST or BEL (\a) can be used to end an OSC sequence. However, tmux # requires ST to end its wrapping DCS. _osc_end() { if [ -n "$TMUX" ]; then printf "\a\e\x5c" # 0x5c == backslash else printf "\a" fi } _update_colors() { export BACKGROUND="$1" if [ "$BACKGROUND" = "light" ]; then _set_terminal_colors $Base01_RGB $Base3_RGB $Red_RGB # fg bg cursor PS1_SEP_COLOR=$(tput setaf $Base1) else _set_terminal_colors $Base1_RGB $Base03_RGB $Red_RGB # fg bg cursor PS1_SEP_COLOR=$(tput setaf $Base01) fi if [ -n "$TMUX" ] && [ -f "$XDG_CONFIG_HOME/tmux/tmux-colors.conf" ]; then tmux set-environment -g BACKGROUND "$BACKGROUND" tmux source-file "$XDG_CONFIG_HOME/tmux/tmux-colors.conf" fi local ls_colors="$HOME/.config/dircolors/solarized-$BACKGROUND" if type dircolors &>/dev/null && [ -f "$ls_colors" ]; then eval "$(dircolors "$ls_colors")" fi } _set_terminal_colors() { # 1: foreground, 2: background, 3: cursor if [ -n "$ITERM_SESSION_ID" ]; then # iTerm2 _set_iterm2_colors "$1" "$2" "$3" else # assume xterm _set_xterm_colors "$1" "$2" "$3" fi } # Documentation for iTerm2 at # https://iterm2.com/documentation-escape-codes.html # under the heading "Change the color palette". _set_iterm2_colors() { # 1: foreground, 2: background, 3: cursor local fg="$1" bg="$2" cursor="$3" local key rgb while read -r key rgb; do _osc_start printf "1337;SetColors=%s=%s" "$key" "${rgb}" _osc_end done < 4;c;spec # c -> index of the ANSI color to change [0..15] # spec -> RGB value of color as rgb:RR/GG/BB _osc_start printf "4;%s;rgb:%s/%s/%s" "$c" "${rgb:0:2}" "${rgb:2:2}" "${rgb:4:2}" _osc_end done < foreground (10), background (11), or cursor (12) # Pt -> RGB value of color as rgb:RR/GG/BB _osc_start printf "%s;rgb:%s/%s/%s" "$Ps" "${rgb:0:2}" "${rgb:2:2}" "${rgb:4:2}" _osc_end done <