~/.zshenv
file is read for all shells, regardless of login state or not,
and it’s a pretty good place to put PATH stuff and other env variables that
don’t cause a lot of interaction.
[[ -o interactive ]] && echo "+++Reading .zshenv"
MANPATH=/opt/local/man:/usr/local/man:$MANPATH
WORDCHARS='*?_[]~=&;!#$%^(){}'
# default is: *?_-.[]~=/&;!#$%^(){}<>
# other: "*?_-.[]~=&;!#$%^(){}<>\\"
WORDCHARS=${WORDCHARS:s,/,,}
LEDGER_FILE=$HOME/ledger.dat; export LEDGER_FILE
export EDITOR=nano # to be overwritten later
export PAGER=less
# Update path with local ~/bin and cabal's bin dir
export PATH=~/bin:~/.cabal/bin:/usr/local/bin:/usr/local/sbin:$PATH
# Node/npm
export PATH=$PATH:~/node_modules/.bin
# Cask
export PATH=$PATH:~/.cask/bin
# rbenv
export PATH=~/.rbenv/bin:$PATH
# Virtualenvwrapper environment home
export WORKON_HOME=~/.venvs
if [ -d "$WORKON_HOME" ]; then
mkdir -p "$WORKON_HOME"
fi
# Read sdkman config for all shells
if [ -f ~/.sdkman/bin/sdkman-init.sh ]; then
source ~/.sdkman/bin/sdkman-init.sh
fi
# history
HISTFILE=$HOME/.zsh-history
HISTSIZE=10000
SAVEHIST=5000
I read OS and machine-specific things in .zshenv, usually because they’re pretty small and just set up paths.
## Sourcing OS-specific things
OS=$(uname -s); export OS
if [[ -f ~/.zsh.d/zsh.${OS} ]]; then
if [[ ! -z $ZSHDEBUG ]]; then
echo +++ ~/.zsh.d/zsh.${OS}
fi
source ~/.zsh.d/zsh.${OS}
fi
## Sourcing machine-specific things
export HOSTPREFIX=`hostname | cut -d. -f1 | sed 's/.*/\L&/'`
if [[ -f ~/.zsh.d/zsh.${HOSTPREFIX} ]]; then
if [[ ! -z $ZSHDEBUG ]]; then
echo +++ ~/.zsh.d/zsh.${HOSTPREFIX}
fi
source ~/.zsh.d/zsh.${HOSTPREFIX}
fi
Vagrant seems to want to use libvirt instead of VirtualBox on my machines, so I want to force it to use virtualbox.
export VAGRANT_DEFAULT_PROVIDER=virtualbox
Finally, if this a dumb term (running M-x shell
in Emacs, unset some things
and make it really dumb.
## With Emacs 23, I've found this needs to go in ~root/.zshrc too to
## help with Tramp hangs.
[[ $TERM == "dumb" ]] && unsetopt zle && PS1='$ '
[[ ! $TERM == "dumb" ]] && TERM=xterm-256color
~/.zprofile
, but I should…
# Handle dumb terms
[[ $TERM == "dumb" ]] && unsetopt zle && PS1='$ ' && return
echo -n "+++Reading .zshrc"
[[ -o interactive ]] && echo -n " (for interactive use)"
echo .
# Used for reporting how load loading takes
zmodload zsh/datetime
start=$EPOCHREALTIME
# for $PATH see ~/.zshenv
# report things that take more than 5 seconds
export REPORTTIME=5
# 10 second poll time for autossh
export AUTOSSH_POLL=10
# don't show load in prompt by default
export SHOW_LOAD=false
# start with a pre-title of nothing
export PRETITLE=""
# "persistent history"
# just write important commands you always need to ~/.important_commands
if [[ -r ~/.important_commands ]] ; then
fc -R ~/.important_commands
fi
# support colors in less
export LESS_TERMCAP_mb=$'\E[01;31m'
export LESS_TERMCAP_md=$'\E[01;31m'
export LESS_TERMCAP_me=$'\E[0m'
export LESS_TERMCAP_se=$'\E[0m'
export LESS_TERMCAP_so=$'\E[01;44;33m'
export LESS_TERMCAP_ue=$'\E[0m'
export LESS_TERMCAP_us=$'\E[01;32m'
# zsh completion
if [ -d ~/.zsh/zsh-completions ] ; then
fpath=(~/.zsh/zsh-completions/src $fpath)
fi
autoload -U compinit zrecompile
zsh_cache=${HOME}/.zsh-cache
if [ $UID -eq 0 ]; then
compinit
else
compinit -d $zsh_cache/zcomp-$HOST
for f in ~/.zshrc $zsh_cache/zcomp-$HOST; do
zrecompile -p $f && rm -f $f.zwc.old
done
fi
zstyle ':completion:::::' completer _complete _approximate
zstyle ':completion:*' use-cache on
zstyle ':completion:*' cache-path ~/.zsh-cache
zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
zstyle ':completion:*' hosts $ssh_hosts
zstyle ':completion:*:my-accounts' users-hosts $my_accounts
zstyle ':completion:*:other-accounts' users-hosts $other_accounts
zstyle -e ':completion:*:approximate:*' max-errors 'reply=( $(( ($#PREFIX + $#SUFFIX) / 3 )) )'
zstyle ':completion:*:descriptions' format "- %d -"
zstyle ':completion:*:corrections' format "- %d - (errors %e})"
zstyle ':completion:*:default' list-prompt '%S%M matches%s'
zstyle ':completion:*' group-name ''
zstyle ':completion:*:manuals' separate-sections true
zstyle ':completion:*:manuals.(^1*)' insert-sections true
zstyle ':completion:*' verbose yes
zstyle ':completion:*' file-list list=20 insert=10
### OPTIONS ###
setopt multios # allow pipes to be split/duplicated
# ^^ try this: cat foo.clj > >(fgrep java | wc -l) > >(fgrep copy | wc -l)
setopt auto_cd
setopt extended_glob
setopt append_history
setopt extended_history
setopt share_history
setopt histignorealldups
setopt nohup
setopt longlistjobs
setopt notify
# I use dvorak, so correct spelling mistakes that a dvorak user would make
setopt dvorak
autoload -U url-quote-magic
zle -N self-insert url-quote-magic
# Source z.sh if available
if [ -s ~/bin/z.sh ] ; then
source ~/bin/z.sh ;
fi
# Use zsh syntax highlighting if available
if [ -s ~/.zsh/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh ] ; then
source ~/.zsh/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
fi
# Source ~/.zsh.d/*
setopt EXTENDED_GLOB
for zshrc in ~/.zsh.d/[0-9][0-9]*[^~] ; do
if [[ ! -z $ZSHDEBUG ]]; then
echo +++ $(basename $zshrc)
fi
source $zshrc
done
unsetopt EXTENDED_GLOB
end=$EPOCHREALTIME
printf "+++Loaded files in %0.4f seconds\n" $(($end-$start))
.zsh.d
and loaded by order. This includes
- aliases
- functions
- OS-specific things
- host specific things
First, aliases, for which I have many!
# colorful ls for whichever platform
if ls -F --color=auto >&/dev/null; then
alias ls="ls --color=auto -F"
else
alias ls="ls -GF"
fi
# various ls helpers
alias l.='ls -d .*'
alias ll='ls -lh'
alias l='ls -lh'
alias la='ls -alh'
alias lr='ls -lR'
# colorize greps
alias grep='grep --color=auto'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
# make less a little more sane
alias less='less -RX'
# various port forwarding and hole-punching
alias scsetup='sudo socat -d -d TCP4-listen:6666,fork OPENSSL:typoet.com:443,cert=host.pem,verify=0'
alias scsetup2='sudo socat -d -d TCP4-listen:7777,fork OPENSSL:blackex:443,cert=host.pem,verify=0'
# reverse proxy & keepopen
alias prox='ssh -nNT -R 4444:localhost:22 writequit.org'
alias autoprox='autossh -M 22000 -nNT -R 4444:localhost:22 writequit.org'
alias awq='autossh -M 23000 writequit.org'
alias aiv='autossh -M 24000 ivalice'
# open elinks quickly
alias el='TERM=xterm-color elinks'
# datetime aliases
alias dt='gdate "+%Y-%m-%dT%H:%M:%S.%3N%zZ"'
# Elasticsearch's basic_date_time
alias bdt='gdate "+%Y%m%dT%H%M%S.%3N%z"'
alias epoch='date +%s'
# jump start to magit
alias magit='emacs -f magit-status'
# simple-extract
alias se="tar zxvf"
alias ga="git annex"
# download manager
alias aria2c='aria2c -c -x5 -s10 -m0'
# sync org files
alias org2ivalice='rsync -azP --delete ~/org/ ivalice-local:~/org'
alias ivalice2org='rsync -azP --delete ivalice-local:~/org/ ~/org'
alias xanadu2org='rsync -azP --delete xanadu:~/org/ ~/org'
alias org2xanadu='rsync -azP --delete ~/org/ xanadu:~/org'
# start a master tmux
alias tmaster='tmux -2 -u -S /tmp/mastermux -f .tmux.master.conf'
Next, some functions, when shell aliases just won’t do!
# functions
function history-all { history -E 1 }
# function to fix ssh agent
function fix-agent() {
disable -a ls
export SSH_AUTH_SOCK=`ls -t1 $(find /tmp/ -uid $UID -path \\*ssh\\* -type s 2> /dev/null) | head -1`
enable -a ls
}
## TODO make these scripts instead of functions
# Check if a URL is up
function chk-url() {
curl -sL -w "%{http_code} %{url_effective}\\n" "$1" -o /dev/null
}
# Tunnel ES from somewhere to here locally on port 9400
function es-tunnel() {
autossh -M0 $1 -L 9400:localhost:9200 -CNf
}
# Tunnel logstash/kibana locally
function kibana-tunnel() {
autossh -M0 $1 -L 9292:localhost:9292 -CNf
}
# Delete a branch locally and on my (dakrone) fork
function del-branch() {
git branch -D $1
git push dakrone :$1
}
# look up a process quickly
function pg {
# doing it again afterwards for the coloration
ps aux | grep -F -i $1 | grep -F -v grep | grep -F -i $1
}
# cd back up to the highest level git repo dir
# thanks Dan!
function cds () {
ORIGINAL_PWD=`pwd`
while [ ! -d ".git" -a `pwd` != "/" ]
do
cd ..
done
if [ ! -d ".git" ]
then
cd $ORIGINAL_PWD
fi
}
bindkey -e
bindkey "^?" backward-delete-char
bindkey "^H" backward-delete-char
bindkey "^[[3~" backward-delete-char
bindkey "^[[1~" beginning-of-line
bindkey "^[[4~" end-of-line
bindkey '^r' history-incremental-search-backward
bindkey "^[[5~" up-line-or-history
bindkey "^[[6~" down-line-or-history
bindkey "^A" beginning-of-line
bindkey "^E" end-of-line
bindkey "^W" backward-delete-word
bindkey "^k" kill-line
bindkey ' ' magic-space # also do history expansion on space
bindkey '^I' complete-word # complete on tab, leave expansion to _expand
bindkey -r '^j' #unbind ctrl-j, I hit it all the time accidentaly
bindkey -r '^[x' # remove M-x for emacs-things
~/.ssh/id_rsa
.
eval $(keychain --eval --agents ssh -Q id_rsa)
autoload colors
colors
git_branch() {
git branch --no-color 2>/dev/null | grep '^*' | colrm 1 2
# $pipestatus[1] for the git exit code
}
autoload -Uz vcs_info
if [[ ! $TERM = "dumb" ]]; then
zstyle ":vcs_info:*" check-for-changes true
zstyle ":vcs_info:*" stagedstr "%F{green}*"
zstyle ":vcs_info:*" unstagedstr "%F{yellow}*"
zstyle ":vcs_info:(sv[nk]|bzr):*" branchformat "%b%F{1}:%F{yellow}%r%{$reset_color%}"
zstyle ":vcs_info:*" enable git svn bzr hg
precmd () {
if [[ -z $(git ls-files --other --exclude-standard 2> /dev/null) ]] {
zstyle ":vcs_info:*" formats "%b%c%u%{$reset_color%}"
} else {
zstyle ":vcs_info:*" formats "%b%c%u%F{red}*%{$reset_color%}"
}
vcs_info
}
else
zstyle ":vcs_info:*" check-for-changes true
zstyle ":vcs_info:*" stagedstr "*"
zstyle ":vcs_info:*" unstagedstr "*"
zstyle ":vcs_info:(sv[nk]|bzr):*" branchformat "%b:%r"
zstyle ":vcs_info:*" enable git svn bzr hg
precmd () {
if [[ -z $(git ls-files --other --exclude-standard 2> /dev/null) ]] {
zstyle ":vcs_info:*" formats "%b%c%u"
} else {
zstyle ":vcs_info:*" formats "%b%c%u*"
}
vcs_info
}
fi
# HTTP verbs
alias get='curl -s -XGET'
alias post='curl -s -XPOST'
alias put='curl -s -XPUT'
alias delete='curl -s -XDELETE'
if [ -f ~/.globalrc ]; then
export GTAGSCONF=$HOME/.globalrc
elif [ -f /usr/local/share/gtags/gtags.conf ] ; then
export GTAGSCONF=/usr/local/share/gtags/gtags.conf
fi
export GTAGSLABEL=ctags
I need to set up the rbenv wrapper so I can have sane ruby building. If it exists, anyway.
if [ -f ~/.rbenv/bin/rbenv ]; then
eval "$(rbenv init -)"
fi
So virtualenvwrapper is a handy thing for managing virtualenv sessions, but it needs to be sourced if available.
Use pip install virtualenvwrapper
to install it
if whence -cp virtualenvwrapper.sh > /dev/null 2>&1; then
source `whence -cp virtualenvwrapper.sh`
fi
I’m checking this out…
if [ -f ~/.opam/opam-init/init.zsh ]; then
. ~/.opam/opam-init/init.zsh > /dev/null 2> /dev/null || true
fi
I used to have a nethack pet (the dog) in it too, that would randomly wander around, but yeah, it didn’t last.
When used, it looks something like this (with more colors):
~/src/elasticsearch (git) ac32f3d3 * master [origin/master +1/-2] (1 stashed)
»
And it’s not too unreadable…
autoload -U add-zsh-hook
autoload -U colors && colors
autoload -Uz vcs_info
setopt prompt_subst
local gray="%{$fg_bold[black]%}"
local green="%{$fg_bold[green]%}"
local blue="%{$fg[blue]%}"
local red="%{$fg[red]%}"
local yellow="%{$fg[yellow]%}"
zstyle ':vcs_info:*' enable git svn cvs hg
zstyle ':vcs_info:git*:*' get-revision true
zstyle ':vcs_info:git*:*' check-for-changes true
# hash changes branch misc
zstyle ':vcs_info:git*' formats "(%s) %8.8i ${green}%c${red}%u${gray} %b%m"
zstyle ':vcs_info:git*' actionformats "(%s|${yellow}%a${gray}) %8.8i ${green}%c${red}%u${gray} %b%m"
zstyle ':vcs_info:git*+set-message:*' hooks git-st git-stash
# Show remote ref name and number of commits ahead-of or behind
function +vi-git-st() {
local ahead behind remote
local -a gitstatus
# Are we on a remote-tracking branch?
remote=${$(git rev-parse --verify ${hook_com[branch]}@{upstream} \
--symbolic-full-name 2>/dev/null)/refs\/remotes\/}
if [[ -n ${remote} ]] ; then
ahead=$(git rev-list ${hook_com[branch]}@{upstream}..HEAD 2>/dev/null | wc -l | tr -d " ")
(( $ahead )) && gitstatus+=( "${green}+${ahead}${gray}" )
behind=$(git rev-list HEAD..${hook_com[branch]}@{upstream} 2>/dev/null | wc -l | tr -d " ")
(( $behind )) && gitstatus+=( "${red}-${behind}${gray}" )
if [[ -n ${gitstatus} ]] ; then
hook_com[branch]="${hook_com[branch]} [${remote} ${(j:/:)gitstatus}]"
else
hook_com[branch]="${hook_com[branch]} [${remote}]"
fi
fi
}
# Show count of stashed changes
function +vi-git-stash() {
local -a stashes
if [[ -s ${hook_com[base]}/.git/refs/stash ]] ; then
stashes=$(git stash list 2>/dev/null | wc -l | tr -d " ")
hook_com[misc]+=" (${stashes} stashed)"
fi
}
function colorSetup {
# A script to make using 256 colors in zsh less painful.
# P.C. Shyamshankar <sykora@lucentbeing.com>
# Copied from http://github.com/sykora/etc/blob/master/zsh/functions/spectrum/
typeset -Ag FX FG BG
FX=(
reset "%{�[00m%}"
bold "%{�[01m%}" no-bold "%{�[22m%}"
italic "%{�[03m%}" no-italic "%{�[23m%}"
underline "%{�[04m%}" no-underline "%{�[24m%}"
blink "%{�[05m%}" no-blink "%{�[25m%}"
reverse "%{�[07m%}" no-reverse "%{�[27m%}"
)
for color in {000..255}; do
FG[$color]="%{�[38;5;${color}m%}"
BG[$color]="%{�[48;5;${color}m%}"
done
# Show all 256 colors with color number
function spectrum_ls() {
for code in {000..255}; do
print -P -- "$code: %F{$code}Test%f"
done
}
# Show all 256 colors where the background is set to specific color
function spectrum_bls() {
for code in {000..255}; do
((cc = code + 1))
print -P -- "$BG[$code]$code: Test %{$reset_color%}"
done
}
}
# Initialize colors for setprompt2
colorSetup
# old-prompt
PROMPT='$FG[032]%~ $FG[237]${vcs_info_msg_0_}
$FG[105]%(?..${red}%?$FG[105] )%(!.#.»)%{$reset_color%} '
add-zsh-hook precmd vcs_info
Here’s a commented out (but much less extravagant) version of a prompt that I keep around, just in case.
# Simple prompt setup
# if not_in_cloud; then
# # PROMPT='%n@%m %w %* %! %? %B%3~%b(${vcs_info_msg_0_})%# '; export PROMPT
# PROMPT='%n@%m %? %B%3~%b(${vcs_info_msg_0_})%# '; export PROMPT
# else
# PROMPT='%n@%m %? %~%# '; export PROMPT
# fi
99-dumb.zsh
to ensure it’s loaded last.
# Things for dumb terminals
if [[ "$EMACSx" == "tx" || "$TERM" == "dumb" ]]; then
unsetopt zle
#unfunction precmd
export DISABLE_AUTO_TITLE=true
export ZSH_HIGHLIGHT_MAXLENGTH=0
else
alias ag="ag --pager='less -FRX'"
fi
~/.zshenv
by looking for
~/.zsh.d/zsh.$OS
Where $OS
is something like ‘Darwin’ or ‘Linux’
And then also loading
~/.zsh.d/zsh.$HOSTPREFIX
Which $HOSTPREFIX
is the output of
hostname | cut -d. -f1 | sed 's/.*/\L&/'
which essentially calls hostname
, takes only the first part and lowercases it.
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
export EMACS_HOME="/Applications/Emacs.app/Contents/MacOS"
export ERC_HOME="/Applications/ERC.app/Contents/MacOS"
export GNUS_HOME="/Applications/Gnus.app/Contents/MacOS"
if [ -s /usr/local/bin/emacs ]; then
alias emacs='TERM=xterm-256color emacs'
alias hb_emacs='/usr/local/bin/emacs'
fi
#function ec() { TERM=xterm-256color PATH=$EMACS_HOME/bin:$PATH emacsclient -t $@ }
alias e="TERM=xterm-256color PATH=$EMACS_HOME/bin:$PATH $EMACS_HOME/Emacs -nw"
alias ec="emacsclient"
#function el() { ps ax|grep Emacs }
function ekill() { emacsclient -e '(kill-emacs)' }
alias emacs="TERM=xterm-256color PATH=$EMACS_HOME/bin:$PATH $EMACS_HOME/Emacs -nw"
alias gemacs="TERM=xterm-256color PATH=$EMACS_HOME/bin:$PATH $EMACS_HOME/Emacs 2>&1 > /dev/null &"
alias erc="TERM=xterm-256color PATH=$ERC_HOME/bin:$PATH $ERC_HOME/Emacs 2>&1 > /dev/null &"
alias gnus="TERM=xterm-256color PATH=$GNUS_HOME/bin:$PATH $GNUS_HOME/Emacs 2>&1 > /dev/null &"
# for connection to a running emacs
export EDITOR="emacsclient"
export ALTERNATIVE_EDITOR="TERM=xterm-256color PATH=$EMACS_HOME/bin:$PATH $EMACS_HOME/Emacs -nw"
# Use MacVim's vim for terminal sessions, since it has everything compiled in.
alias vim='/Applications/MacVim.app/Contents/MacOS/Vim'
# Remove ctrl+y from the keybinds for delayed suspend
stty dsusp undef
# awesome
alias gps="ps -c -r -ax -o command,pid,pcpu,time | sed 's/\(PID *\)%/\1 %/' | head -n 11 && echo && ps -c -m -ax -o command,pid,pmem,rss=RSIZE | sed 's/\(.\{23\}\)/\1 /' | head -n 9"
alias tmux='tmux -2 -f .tmux.osx.conf'
# A function to mimic Linux's strace, whichout running the program as root
function strace {
sudo dtruss -f sudo -u `whoami` $*
}
# make emacs have 256 colors
alias -g emacs='TERM=xterm-256color /usr/local/bin/emacs -nw'
alias -g ec="/home/hinmanm/bin/emacsclient"
function ekill() { emacsclient -e '(kill-emacs)' }
export EDITOR="emacs -nw"
alias tmux='tmux -2'
# awesome
alias gps='ps -eo cmd,fname,pid,pcpu,time --sort=-pcpu | head -n 11 && echo && ps -eo cmd,fname,pid,pmem,rss --sort=-rss | head -n 9'
There’s probably more I need to put here, but for now it’s empty.
# Thulcandra had better be a linux machine...
if [[ $OS == "Linux" ]]; then
export JAVA_HOME=/opt/jdk1.8.0_60
export PATH=$JAVA_HOME/bin:$PATH
fi
# Xanadu had better be a linux machine...
if [[ $OS == "Linux" ]]; then
export JAVA_HOME=/opt/jdk1.8.0_51
export PATH=$JAVA_HOME/bin:$PATH
fi
# Ivalice had better be a linux machine...
if [[ $OS == "Linux" ]]; then
export JAVA_HOME=/opt/jdk1.8.0_45
export PATH=$JAVA_HOME/bin:$PATH
fi