Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
318 lines (280 sloc) 10.6 KB
# vim:set noexpandtab
#
# thanks:
# http://selena.deckelmann.usesthis.com/
# https://github.com/mathiasbynens/dotfiles/blob/master/.functions
# https://superuser.com/questions/180148/how-do-you-get-screen-to-automatically-connect-to-the-current-ssh-agent-when-re
# https://github.com/nummi/my-config/blob/master/.zsh/completion.zsh
# http://robots.thoughtbot.com/silver-searcher-tab-completion-with-exuberant-ctags
# http://iterm2.com/shell_integration.html
# http://sheerun.net/2014/03/21/how-to-boost-your-vim-productivity/
# https://github.com/xero/dotfiles
# https://github.com/eevee/rc
# http://nullprogram.com/blog/2017/06/19/
# External plugins {{{
source ~/.zsh/zsh-hl/zsh-syntax-highlighting.zsh
source ~/.zsh/zsh-hss/zsh-history-substring-search.zsh
# }}}
# XDG defaults {{{
: ${XDG_CONFIG_HOME:="$HOME/.config"}
: ${XDG_CACHE_HOME:="$HOME/.cache"}
: ${XDG_DATA_HOME:="$HOME/.local/share"}
: ${XDG_RUNTIME_DIR:="/tmp/$USER"}
export XDG_CONFIG_HOME XDG_CACHE_HOME XDG_DATA_HOME XDG_RUNTIME_DIR
[[ ! -d $XDG_RUNTIME_DIR ]] && mkdir -p $XDG_RUNTIME_DIR && chmod 0700 $XDG_RUNTIME_DIR
# mount tmpfs at /tmp. Do not use ZFS for XDG_RUNTIME_DIR because posix_fallocate.
# }}}
# Library installation to home {{{
export C_INCLUDE_PATH=$HOME/.local/include:$C_INCLUDE_PATH
export CPLUS_INCLUDE_PATH=$HOME/.local/include:$CPLUS_INCLUDE_PATH
export LIBRARY_PATH=$HOME/.local/lib:$LIBRARY_PATH
export PKG_CONFIG_PATH=$HOME/.local/lib/pkgconfig:$PKG_CONFIG_PATH
# }}}
# Color {{{
base16() {
local SCHEME="$*"
local BASE16_SHELL="$HOME/.zsh/base16-shell/scripts/base16-$SCHEME.sh"
if [[ -s $BASE16_SHELL ]]; then
source $BASE16_SHELL
export BASE16_SCHEME="$SCHEME"
echo -n $SCHEME > $XDG_CONFIG_HOME/base16/scheme
else
echo "Looks like $BASE16_SHELL does not exist :-("
fi
}
_base16() { compadd $(find ~/.zsh/base16-shell/scripts | xargs basename | sed -e "s/base16-//" -e "s/\.sh//") }
: ${BASE16_SCHEME:="$(cat $XDG_CONFIG_HOME/base16/scheme 2>/dev/null || echo default)"}
export BASE16_SCHEME
base16 $BASE16_SCHEME
# }}}
# Support for tools, programming languages, etc {{{
# Less
export LESS_TERMCAP_mb=$'\E[01;31m'
export LESS_TERMCAP_me=$'\E[0m'
export LESS_TERMCAP_se=$'\E[0m'
export LESS_TERMCAP_so=$'\E[38;5;246m'
export LESS_TERMCAP_ue=$'\E[0m'
export LESS_TERMCAP_us=$'\E[04;33;146m'
export LESS="-R"
export LESSHISTFILE="$XDG_CACHE_HOME/less_history"
export MANWIDTH="120"
# Tmux
export TMUX_TMPDIR="$XDG_RUNTIME_DIR"
# Go
export GOPATH=~
export GOBIN=~/.local/bin
[[ -e $commands[brew] ]] && export BREWGO=$(brew --prefix go)
# JS
[[ -d /usr/local/lib/node_modules ]] && export NODE_PATH=/usr/local/lib/node_modules
# Java
[[ -e /usr/libexec/java_home ]] && export JAVA_HOME=$(/usr/libexec/java_home)
[[ -d /usr/local/opt/android-sdk ]] && export ANDROID_HOME=/usr/local/opt/android-sdk
runjava() {
cp $1 /tmp/Main.java
(cd /tmp && javac Main.java && echo "--- compiled, running" && java Main)
}
# Ruby
[[ -d /usr/local/var/rbenv ]] && export RBENV_ROOT=/usr/local/var/rbenv
bforkify() {
bundle config local.$1 ~/src/github.com/myfreeweb/$1
}
bunforkify() {
bundle config --delete local.$1
}
# Python
export PYTHONDONTWRITEBYTECODE=true
export VIRTUALENV_DISTRIBUTE=true
export PIP_USE_MIRRORS=true
export PYTHONSTARTUP=$XDG_CONFIG_HOME/python/rc.py
# Perl
export PERL_MB_OPT="--install_base \"$HOME/.local\""
export PERL_MM_OPT="INSTALL_BASE=$HOME/.local"
export PERL5LIB=~/.local/lib/perl5:$PERL5LIB
# Compiling stuff on OS X
[[ -e $commands[brew] ]] && export ARCHFLAGS="-arch x86_64"
# OCaml
[[ -e $commands[opam] ]] && eval $(opam config env)
# Haskell
hsi() { stack ghci --ghc-options="-fno-hpc" --with-ghc intero }
:test() { hsi } # for the vimux/ghci trick
# Rust
#export RUSTUP_USE_HYPER=1 # https://github.com/rust-lang-nursery/rustup.rs/issues/1021
[[ -e $commands[rustc] ]] && export LD_LIBRARY_PATH="$(rustc --print sysroot)/lib:$LD_LIBRARY_PATH"
export CARGO_HOME="$XDG_DATA_HOME/cargo"
export CARGO_INSTALL_ROOT="$HOME/.local"
# }}}
# Aliases and functions {{{
[[ -e ~/.rc ]] && source ~/.rc
[[ ! -e $commands[gmake] ]] && alias gmake=make
dighost() { host $(dig $1 | grep ANSWER -C 1 | tail -n 1 | awk '{ print $5 }') }
drillhost() { host $(drill $1 | grep ANSWER -C 1 | tail -n 1 | awk '{ print $5 }') }
mcd() { mkdir -p $1 && cd $1; }
icd() { DIR=$(ls | $MENU) && cd $DIR && icd }
prj() { cd ~/src/$(ghq list | $MENU) }
hst() { eval $(history -n 1 | tail -r | $MENU --layout=bottom-up) }
mdown() { pandoc -s -f markdown -t man "$*" | mandoc | less } # read markdown files like manpages
if [[ -n $TMUX ]]; then
escseq() { printf "\033Ptmux;\033\033]$1\007\033\\" }
else
escseq() { printf "\033]$1\007" }
fi
precmd() {
# $? in prompt is wrong, can't pass %? to conditionals
local exit_status="$?"
if [[ $exit_status == 0 ]]; then SMILEY=')'; else SMILEY='('; fi
if [[ $PRIVATE == 1 ]]; then
SCOLOR=$bg[magenta]$fg_bold[black]
elif [[ ${HOST##*\.} == 'lan' || ${HOST##*\.} == 'local' || ${HOST##*\.} == 'localdomain' || $HOST =~ '^[^\.]+$' ]]; then
# green on *.lan, *.local, *.localdomain and hostnames without dots
SCOLOR=$fg_bold[green]
else
# red on everything else - that is, on production servers!
SCOLOR=$fg_bold[red]
fi
}
fancy-ctrl-z() {
if [[ $#BUFFER -eq 0 ]]; then
BUFFER=fg
zle accept-line
else
zle push-input
zle clear-screen
fi
}
# }}}
# Basic settings {{{
# Modules
autoload -U colors && colors
autoload -U compinit && zmodload -i zsh/complist
autoload -U edit-command-line && zle -N edit-command-line
autoload -U url-quote-magic && zle -N self-insert url-quote-magic
autoload -U select-word-style && select-word-style bash
autoload -U zmv
# Keybindings
zmodload zsh/terminfo
bindkey -e # Emacs style keys in shell
bindkey '^xe' edit-command-line
bindkey '^x^e' edit-command-line
bindkey "\e[3~" delete-char # Del
zle -N hst && bindkey '^R' hst
zle -N fancy-ctrl-z && bindkey '^Z' fancy-ctrl-z
bindkey '^[[A' history-substring-search-up
bindkey '^[[B' history-substring-search-down
#[[ -n $terminfo[kcuu1] ]] && bindkey $terminfo[kcuu1] history-substring-search-up
#[[ -n $terminfo[kcud1] ]] && bindkey $terminfo[kcud1] history-substring-search-down
# Stuff
setopt pushd_ignore_dups auto_pushd auto_name_dirs auto_cd \
transient_rprompt prompt_subst no_beep multios extended_glob interactive_comments
# History
setopt hist_ignore_dups hist_ignore_space hist_reduce_blanks hist_verify \
hist_expire_dups_first hist_find_no_dups share_history extended_history \
append_history inc_append_history nobanghist
# Completion
setopt menu_complete # Autoselect the first suggestion
setopt complete_in_word
setopt no_complete_aliases # Actually: completes aliases! (I guess that means "no ~separate functions~ for aliases")
unsetopt always_to_end
zstyle ':completion:*' squeeze-slashes true
zstyle ':completion:*' insert-tab pending
zstyle ':completion:*' expand "yes"
zstyle ':completion:*' matcher-list "m:{a-zA-Z}={A-Za-z}" # ignore case
zstyle ':completion:*' list-colors ""
zstyle ':completion:*' menu select=2 _complete _ignored _approximate
zstyle ':completion:*' group-name ''
zstyle ':completion:*' verbose yes
zstyle ':completion:*:default' list-prompt '%S%M matches%s'
zstyle ':completion:*:prefix:*' add-space true
zstyle ':completion:*:descriptions' format "|| %{${fg[yellow]}%}%d%{${reset_color}%}"
zstyle ':completion:*:messages' format $'\e[00;31m%d'
zstyle ':completion:*:manuals' separate-sections true
zstyle ':completion:*:manuals.(^1*)' insert-sections true
zstyle ':completion:*::::' completer _expand _complete _ignored _approximate
zstyle ':completion:*:match:*' original only
zstyle ':completion:*:approximate:*' max-errors 1 numeric
zstyle ':completion:*:cd:*' ignore-parents parent pwd
zstyle ':completion:*:rm:*' ignore-line yes
zstyle ':completion:*:*:*:processes' list-colors "=(#b) #([0-9]#) ([0-9a-z-]#)*=01;34=0=01"
zstyle ':completion:*:*:*:processes' command "ps -u $USER -o pid,user,comm -w -w"
zstyle ':completion:*:*:*:*:hosts' list-colors "=*=$color[cyan];$color[bg-black]"
zstyle ':completion:*:functions' ignored-patterns "_*"
zstyle ':completion:*:original' list-colors "=*=$color[red];$color[bold]"
zstyle ':completion:*:parameters' list-colors "=[^a-zA-Z]*=$color[red]"
zstyle ':completion:*:aliases' list-colors "=*=$color[green]"
fpath+=(
~/.zsh/zsh-completions/src(N)
$XDG_DATA_HOME/zsh/site-functions(N)
/usr/local/share/zsh/site-functions(N)
)
fignore+=.DS_Store
compinit -i
compdef mcd=cd
_ctagscomp() { (( CURRENT == 2 )) && compadd $(cut -f 1 tags ctags .git/tags tmp/tags 2>/dev/null | grep -v '!_TAG') }
compdef _ctagscomp ack
compdef _ctagscomp ag
compdef _ctagscomp pt
compdef _ctagscomp rg
compdef _ctagscomp sr
_loadkeys() { compadd $(find ~/.ssh -perm 0600 ! -type s | xargs basename) }
compdef _loadkeys loadkeys
compdef _base16 base16
HISTFILE=~/.tmp/history_zsh
HISTSIZE=40960
SAVEHIST=40960
setopt HIST_EXPIRE_DUPS_FIRST
ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets)
WORDCHARS='*?_-.[]~=&;!#$%^(){}<>' # Like default, but without / -- ^W must be useful in paths, like it is in vim, bash, tcsh
export LANG="en_US.UTF-8"
export LC_ALL="en_US.UTF-8"
export CLICOLOR="yes"
PROMPT='%{$SCOLOR%}${PWD/#~/~}%{$reset_color%} %{$fg_no_bold[yellow]%}:$SMILEY%{$reset_color%} '
cdpath=(~/src/github.com/myfreeweb)
typeset -U path # U for Unique, like a set; (N) == only if exists
path=(
.git/safe/../../bin
~/.local/bin(N) ~/bin(N) ~/.cask/bin(N)
$BREWGO/bin(N) $BREWGO/libexec/bin(N) /usr/local/go/bin(N)
$JAVA_HOME/bin(N)
$RBENV_ROOT/shims(N)
~/.gem/ruby/2.4/bin(N)
~/.gem/ruby/2.3/bin(N)
/usr/local/share/npm/bin(N)
/usr/local/sbin /usr/local/bin /sbin /bin $path
)
export PATH
# }}}
# Default programs {{{
# (must come after PATH)
[[ -e $commands[more] ]] && export PAGER=more
[[ -e $commands[less] ]] && export PAGER=less
alias pg=$PAGER
[[ -e $commands[vi] ]] && export EDITOR=vi VISUAL=vi
[[ -e $commands[vim] ]] && export EDITOR=vim VISUAL=vim
[[ -e $commands[nvim] ]] && export EDITOR=nvim VISUAL=nvim
alias vi=$VISUAL
[[ -e $commands[percol] ]] && export MENU=percol
[[ -e $commands[fzf] ]] && export MENU=fzf
[[ -e $commands[peco] ]] && export MENU=peco
alias mn=$MENU
[[ -e $commands[ack] ]] && export SEARCH=ack
[[ -e $commands[pt] ]] && export SEARCH=pt
[[ -e $commands[ag] ]] && export SEARCH=ag
[[ -e $commands[rg] ]] && export SEARCH=rg
alias sr=$SEARCH
# }}}
# GPG agent for SSH {{{
export GPG_TTY=$(tty)
[[ -e $commands[gpgconf] ]] && export SSH_AUTH_SOCK="$(gpgconf --list-dirs agent-ssh-socket)"
if [[ -e $commands[gpg-connect-agent] ]]; then
ssh() {
gpg-connect-agent updatestartuptty /bye
$commands[ssh] $*
}
scp() {
gpg-connect-agent updatestartuptty /bye
$commands[scp] $*
}
fi
# }}}
[[ $TERM == "st-256color" ]] && export TERM=xterm-256color
[[ -e ~/.zshrc.local ]] && source ~/.zshrc.local
[[ $PRIVATE == 1 ]] && unset HISTFILE