Skip to content

Instantly share code, notes, and snippets.

@bootleq
Created March 5, 2014 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bootleq/9364072 to your computer and use it in GitHub Desktop.
Save bootleq/9364072 to your computer and use it in GitHub Desktop.
Currently in use messy zshrc
# References:
# GRML - https://github.com/grml/grml-etc-core
# zsh-users - https://github.com/zsh-users
[[ $ZDOTDIR == '' ]] && export ZDOTDIR=$HOME
if [[ $ZDOTDIR == '/etc/users/bootleq' && -f $ZDOTDIR/.vimrc ]]; then
alias vim="vim -u $ZDOTDIR/.vimrc"
fi
# Options ref: http://zsh.sourceforge.net/Doc/Release/Options.html#SEC99
# unset default Windows environment variables.
unset TMP
unset TEMP
if [[ -n `locale -a | grep "zh_TW.utf8"` ]]; then
export LANG=zh_TW.UTF-8
fi
export OSTYPE=$OSTYPE
export PATH=$PATH
export EDITOR=vim
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
# automatically remove duplicates from these arrays
typeset -U path cdpath fpath manpath
path=($path "$HOME/bin")
# user completion plugins
if [[ -d ~/.zsh/completions ]]; then
fpath=(~/.zsh/completions $fpath)
# default dir is [DATADIR/zsh/VERSION/functions] where DATADIR is [PREFIX/share]
fi
# Skip all this for non-interactive shells
[[ -z "$PS1" ]] && return
# disable START/STOP output control (<C-S>, <C-Q>)
stty -ixon
# Cd path
if [[ $OSTYPE == 'cygwin' ]]; then
cdpath=(~ /cygdrive/d/repository)
else
cdpath=(~ /home/www ~/repository ~/vm)
fi
# Fix <S-Tab> mapping
if [[ $OSTYPE == 'linux-gnu' && -n $(command -v xmodmap >/dev/null 2>&1) ]]; then
xmodmap -e 'keycode 23 = Tab'
fi
# WORDCHARS
# default: *?_-[]~=/&;!#$%^(){}<>
WORDCHARS='_'
unsetopt BG_NICE
# grep for running process, like: 'any vim' (from grml-etc-core)
any() {
emulate -L zsh
if [[ -z "$1" ]] ; then
echo "any - grep for process(es) by keyword" >&2
echo "Usage: any <keyword>" >&2 ; return 1
else
local STRING=$1
local LENGTH=$(expr length $STRING)
local FIRSCHAR=$(echo $(expr substr $STRING 1 1))
local REST=$(echo $(expr substr $STRING 2 $LENGTH))
ps xauwww| grep "[$FIRSCHAR]$REST"
fi
}
# dirstack handling (from grml-etc-core)
DIRSTACKSIZE=${DIRSTACKSIZE:-20}
DIRSTACKFILE=${DIRSTACKFILE:-${HOME}/.zdirs}
if [[ -f ${DIRSTACKFILE} ]] && [[ ${#dirstack[*]} -eq 0 ]] ; then
dirstack=( ${(f)"$(< $DIRSTACKFILE)"} )
# "cd -" won't work after login by just setting $OLDPWD, so
[[ -d $dirstack[1] ]] && cd $dirstack[1] && cd $OLDPWD
fi
chpwd() {
local -ax my_stack
my_stack=( ${PWD} ${dirstack} )
builtin print -l ${(u)my_stack} >! ${DIRSTACKFILE}
}
# zsh 5.0 {{{
# if [[ $ZSH_VERSION > 5.0.0 ]]; then
# fi
# }}}
# Autoload functions {{{
autoload -U colors && colors
autoload -U edit-command-line
zle -N edit-command-line
# vcs_info
# autoload -Uz vcs_info
# zstyle ':vcs_info:*' enable git
# zstyle ':vcs_info:*' actionformats \
# '%F{5}(%f%s%F{5})%F{3}-%F{5}[%F{2}%b%F{3}|%F{1}%a%F{5}]%f '
# zstyle ':vcs_info:*' formats \
# '%F{5}(%f%s%F{5})%F{3}-%F{5}[%F{2}%b%F{5}]%f '
# zstyle ':vcs_info:(sv[nk]|bzr):*' branchformat '%b%F{1}:%F{3}%r'
# precmd () { vcs_info }
# ,. 插入最後插入的 word
autoload smart-insert-last-word
zle -N insert-last-word smart-insert-last-word
zstyle :insert-last-word match '*([^[:space:]][[:alpha:]/\\]|[[:alpha:]/\\][^[:space:]])*'
# }}} Autoload functions
# Status display {{{
# Prompts
# function git_prompt_info() {
# ref=$(git symbolic-ref HEAD 2> /dev/null) || return
# echo $'%{\e[32m%}(%{\e[0;32m%}'${ref#refs/heads/}$'%{\e[0;32m%})'
# }
if [[ $OSTYPE == 'cygwin' ]]; then
PS1=$'\n'"%{$fg[green]%}%T@%m %{$fg[yellow]%}%~ %{$reset_color%}"$'\n$ '
else
# {time}@{user name} {tail of ssh connection ip} {~pwd}
# {root => #, not-root => $ }
#
# 10:09@bootleq:77.141 /home/www/foobar
# $
if [[ (${$(netstat -tn | grep tcp -m 1 | awk '{print $4}')/:*/} =~ '(2\.15|102)$') ]]; then
PS1=$'\n'"%F{green}%T@%n %F{yellow}%~ %f"$'\n%0(#.%F{red}%B#%b%f.$)%E '
else
PS1=$'\n'"%F{green}%T@%n:${(j:.:)${(@s:.:)${$(netstat -tn | grep tcp -m 1 | awk '{print $4}')/:*/}}[3,4]} %F{yellow}%~ %f"$'\n%0(#.%F{red}%B#%b%f.$)%E '
fi
fi
# Term title
case $TERM in (*xterm*|*rxvt*|(dt|k|E)term)
precmd () { print -Pn "\e]0;%~\a" }
;;
esac
# }}} Status display
# History {{{
export HISTFILE=~/.zhistory
setopt APPEND_HISTORY
setopt HIST_VERIFY
setopt INC_APPEND_HISTORY
setopt EXTENDED_HISTORY
export HISTSIZE=8400
export SAVEHIST=8000
setopt HIST_FIND_NO_DUPS
setopt HIST_IGNORE_DUPS
setopt HIST_EXPIRE_DUPS_FIRST
setopt SHARE_HISTORY
setopt HIST_SAVE_NO_DUPS
# export HISTIGNORE="&:ls:[bf]g:exit:reset:clear:cd:cd ..:cd..:vim"
# }}} History
# Colors {{{
# xterm-color
if [ -e /usr/share/terminfo/x/xterm-256color ]; then
export TERM='xterm-256color'
else
export TERM='xterm-256color'
fi
# see http://www.bigsoft.co.uk/blog/index.php/2008/04/11/configuring-ls_colors
# di : directory color
# ow : OTHER_WRITABLE directory
export LS_COLORS='di=33:ow=100;33'
# export LS_COLORS=$LS_COLORS:di='33':ow='100;33'
# Colors for less pager (man pages)
export LESS_TERMCAP_mb=$'\E[01;31m' # begin blinking
export LESS_TERMCAP_md=$'\E[01;31m' # begin bold
export LESS_TERMCAP_me=$'\E[0m' # end mode
export LESS_TERMCAP_se=$'\E[0m' # end standout-mode
export LESS_TERMCAP_so=$'\E[01;44;33m' # begin standout-mode - info box
export LESS_TERMCAP_ue=$'\E[0m' # end underline
export LESS_TERMCAP_us=$'\E[01;32m' # begin underline
# }}} Colors
# ETC. {{{
export MAILCHECK=600
setopt notify
# }}} ETC.
# Aliases {{{
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias ls='ls -F -A --color=tty'
alias ll='ls -l'
alias cls='clear'
alias dir='ls -lha'
alias grep='grep --color -n -I --exclude-dir=".git"'
() {
# TODO --hidden
local common='noglob ag --nobreak --nogroup --noheading --smart-case --depth=27'
local colors='--color-path 35 --color-line-number 32 --color-match "1;31"'
alias ag=$common' '$colors
alias agjs=$common' '$colors' -G public/javascripts/.*-(debug|src).js'
}
alias sudo='sudo env PATH=$PATH'
alias gits='git status'
alias gitl='git log --date=iso'
alias gitc='git checkout'
alias gitb='git branch'
alias gitg='git log -i --all-match --grep'
alias gitaddall='git add --all'
alias gh='fc -l 0 | grep'
alias be='bundle exec'
alias pry='bundle exec pry'
alias rpy='bundle exec pry-remote'
alias railsc='rails console'
alias rake='noglob bundle exec rake'
alias rroutes='bundle exec rake routes'
alias rlog='bundle exec rake log:clear'
alias guard='bundle exec guard'
alias rdbmstatus='rake db:migrate:status'
alias rnginx='sudo service nginx restart'
alias rtouch='touch tmp/restart.txt'
# TODO unicorn restart
# kill -USR2 `cat #{unicorn_pid}`;
alias redis='noglob ./script/redis-client'
alias redisdev='noglob ./script/redis-client development -f \\A\"dev.+'
alias redisstg='noglob ./script/redis-client staging -f \\A\"staging'
alias redisprd='noglob ./script/redis-client production'
if [[ $OSTYPE == 'linux-gnu' ]]; then
alias explorer="nautilus . &!"
else
alias explorer="explorer . &!"
fi
alias exp='explorer'
alias vi='vim'
alias historyv='fc -l 0 | vim -R -'
alias :q=exit
alias :qa=exit
alias ..='cd ..'
alias -g HEAD\^='HEAD~1'
alias -g HEAD\^\^='HEAD\\^\\^'
alias -g ...='../..'
alias -g ....='../../..'
# Typo aliases
alias gti=git
# Auto extension aliases
alias -s tar='tar -xf'
alias -s bz2='tar -xjf'
alias -s gz='tar -xzf'
alias -s tgz='tar -xzf'
alias -s tbz2='tar -xjf'
# }}} Aliases
# Completion {{{
# http://d.hatena.ne.jp/voidy21/20090902/1251918174
setopt AUTO_LIST
setopt AUTO_MENU
setopt LIST_PACKED
setopt AUTO_PUSHD
setopt AUTO_PARAM_KEYS
setopt PUSHD_IGNORE_DUPS
setopt CORRECT
setopt globdots
autoload -Uz compinit
compinit
zstyle ':completion:*' completer _expand _complete _match
zstyle ':completion:*' completions 0
zstyle ':completion:*' use-cache true
zstyle ':completion:*' cache-path ~/.zsh/cache
zstyle ':completion:*' matcher-list 'm:{a-z}={A-Z}'
#zstyle ':completion:*:*files' ignored-patterns '*?.o' '*?~' '*\#'
zstyle ':completion:*:(all-|)files' ignored-patterns '(|*/)CVS'
zstyle ':completion:*:cd:*' ignored-patterns '(*/)#CVS'
zstyle ':completion:*:cd:*' tag-order local-directories path-directories
# ssh/scp host
# if [ -f ~/.ssh/known_hosts ]; then
# zstyle ':completion:*' hosts $( sed 's/[, ].*$//' $HOME/.ssh/known_hosts )
# zstyle ':completion:*:*:(ssh|scp):*:*' hosts `sed 's/^\([^ ,]*\).*$/\1/' ~/.ssh/known_hosts`
# fi
# ssh (from majutsushi-etc/zsh/zsh/zshrc.d/20_completion)
[[ -r ~/.ssh/known_hosts ]] && _ssh_hosts=(${${${${(f)"$(<$HOME/.ssh/known_hosts)"}:#[\|]*}%%\ *}%%,*}) || _ssh_hosts=()
hosts=(
"$_ssh_hosts[@]"
)
zstyle ':completion:*:hosts' hosts $hosts
zstyle ':completion:*:hosts' ignored-patterns 'ip6-*'
# scp Completion (from jdong's zshrc file v0.2.1)
zstyle ':completion:*:scp:*' tag-order \
files users 'hosts:-host hosts:-domain:domain hosts:-ipaddr"IP\ Address *'
zstyle ':completion:*:scp:*' group-order \
files all-files users hosts-domain hosts-host hosts-ipaddr
# zstyle ':completion:*:ssh:*' tag-order \
# users 'hosts:-host hosts:-domain:domain hosts:-ipaddr"IP\ Address *'
# zstyle ':completion:*:ssh:*' group-order \
# hosts-domain hosts-host users hosts-ipaddr
# zstyle '*' single-ignored show
zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([0-9]#)*=0=01;31'
zstyle ':completion:*:*:kill:*' menu yes select
# Group
zstyle ':completion:*:matches' group 'yes'
zstyle ':completion:*' group-name ''
zstyle ':completion:*:options' description 'yes'
zstyle ':completion:*:options' auto-description '%d'
#zstyle ':completion:*:descriptions' format $'\e[33;40m -- %d --\e[0m'
zstyle ':completion:*:messages' format $'\e[01;35m -- %d --\e[0m'
zstyle ':completion:*:warnings' format $'\e[01;31m -- No Matches Found --\e[0m'
zstyle ':completion:*:corrections' format $'\e[01;32m -- %d (errors: %e) --\e[0m'
zstyle ':completion:*:*:-command-:*' group-order builtins functions commands
zstyle ':completion:*:*:-command-:*' list-packed 'true'
# List
zmodload zsh/complist
#zstyle ':completion:*' glob 0
zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
zstyle ':completion:*:*' list-colors 'di=01;36'
#zstyle ':completion:*' list-prompt %SAt %p: Hit TAB for more, or the character to insert%s %L
zstyle ':completion:*' max-errors 2 numeric
zstyle ':completion:*' menu select=1 _complete _ignored _approximate
zstyle ':completion:*' select-prompt %SScrolling: at %p - %L%s
zstyle ':completion:*' substitute 0
# for debug
# zstyle ':completion:*' format '%BCompleting %d%b'
# zstyle ':completion:*' group-name ''
# autoload -U compinit && compinit
# zstyle ':completion:*' verbose yes
# echo ${(F)${(uo@)_comps}}|less
zstyle :compinstall filename '~/.zshrc'
# }}} Completion
# Bindkey {{{
# vi key binding
bindkey -v
# in vi normal mode
function zle-line-init zle-keymap-select {
RPS1=$'%{\e[38;5;166m%}'`[[ $KEYMAP == 'vicmd' ]] && print -- -- NORMAL --`$'%{\e[00m%}'
RPS2=$RPS1
zle reset-prompt
}
zle -N zle-line-init
zle -N zle-keymap-select
# buffer 為空時,按 tab 會填入 ./
function first-tab-dotslash(){
if [[ -n $BUFFER ]] ; then
zle expand-or-complete
else
BUFFER="./"
zle end-of-line
zle expand-or-complete
fi }
zle -N first-tab-dotslash
bindkey "\t" first-tab-dotslash
# TODO last eval result (like ruby console _)
# function last-eval-result(){
# if [[ -n $BUFFER ]] ; then
# LBUFFER="ee"$LBUFFER"`!!`"
# else
# fi }
# zle -N last-eval-result
# bindkey "" last-eval-result
# TODO Vim <C-A> <C-X> 數字變化
# function cycle-number-add(){
# if [[ -n $BUFFER ]] ; then
# backward-kill-word
# local cword
# cword=$LBUFFER
# else
# fi }
# zle -N cycle-number-add
# bindkey "" cycle-number-add
# sudo 上一個命令 (from grml.org)
sudo-command-line() {
[[ -z $BUFFER ]] && zle up-history
if [[ $BUFFER != sudo\ * ]]; then
BUFFER="sudo $BUFFER"
CURSOR=$(( CURSOR+5 ))
fi
}
zle -N sudo-command-line
bindkey "" sudo-command-line
killring_comp() {
local -a items all
all=($CUTBUFFER $killring)
for item in $all; do
if [[ $#item -gt 1 ]]; then
# TODO remove control chars
items+=$item
fi
done
compadd -U -x "Killring items:" $items
}
zle -C killring_comp menu-expand-or-complete killring_comp
bindkey ',fp' killring_comp
# 修理 ubuntu 上 up down 將游標移到前頭的行為
up-and-eol() { zle up-line-or-history ; zle end-of-line };
down-and-eol() { zle down-line-or-history ; zle end-of-line };
zle -N up-and-eol
zle -N down-and-eol
# init local bare git repository
gitinit-local-bare-repo() {
local url=$(git config --get "remote.$1.url")
[[ -z $url ]] && echo "Config {remote.$1.url} not exists." && return
! [[ -d $url ]] && mkdir -pv $url
if $(git ls-remote "$1" 2> /dev/null); then
echo 'Repository already exists. Abort.'
else
cd $url
git init --bare
cd -
fi
}
compdef '__git_remotes' gitinit-local-bare-repo
zle -N gitinit-local-bare-repo
cancel-whole-input() { BUFFER="" };
zle -N cancel-whole-input
reload-zshrc() { source ~/.zshrc };
zle -N reload-zshrc
bindkey "\\e[1;5D" backward-word
bindkey "\\e[1;5C" forward-word
bindkey "^[[3~" delete-char
bindkey "^[[A" up-and-eol
bindkey "^[[B" down-and-eol
bindkey ',%' vi-match-bracket
bindkey "^[[5~" history-search-backward # PageUp
bindkey "^[[6~" history-search-forward # PageDown
bindkey "^F" vi-find-next-char
bindkey "^Z" undo
bindkey '' redo
# bindkey "^Y" yank
bindkey '^[[Z' reverse-menu-complete # S-Tab
bindkey "^H" history-incremental-search-backward
bindkey "^_" backward-delete-word # C-BS
bindkey "" kill-whole-line
bindkey ",," cancel-whole-input
bindkey ",;" backward-delete-word
bindkey ",u" undo
# bindkey ",y" yank
# bindkey ",p" vi-put-after
bindkey ',gU' up-case-word
bindkey ',gu' down-case-word
bindkey ',.' insert-last-word
bindkey ',v' edit-command-line
bindkey -M vicmd -s ",," "i"
bindkey -M menuselect ',
' accept-and-menu-complete
bindkey "OH" beginning-of-line
bindkey "OF" end-of-line
bindkey "OD" backward-word
bindkey "OC" forward-word
# Cygwin
bindkey "" beginning-of-line
bindkey "" end-of-line
# TODO tmux 的會多送一個 ~
bindkey "^[[1~" beginning-of-line
bindkey "^[[H~" beginning-of-line
bindkey "^[[4~" end-of-line
bindkey "^[[F~" end-of-line
bindkey "^[[2~" overwrite-mode
bindkey "\\e[15;5~" reload-zshrc
bindkey "OP" run-help
# }}} Bindkey
# Plugins {{{
# https://github.com/zsh-users/zaw {{{
if [ -e $ZDOTDIR/repository/zaw/zaw.zsh ]; then
source $ZDOTDIR/repository/zaw/zaw.zsh
bindkey ",fz" zaw
bindkey ",fb" zaw-bookmark
bindkey ",fg" zaw-git-files
bindkey ",fh" zaw-history
bindkey ",ft" zaw-tmux
bindkey ",ff" zaw-open-file
# bindkey "^H" zaw-perldoc
bindkey ",fa" zaw-ack
bindkey ",fc" zaw-cdr
autoload -U filter-select; filter-select -i
bindkey -M filterselect '
' accept-search # alternative action
bindkey -M filterselect ',,' send-break
bindkey -M filterselect "OH" beginning-of-line
bindkey -M filterselect "OF" end-of-line
bindkey -M filterselect "OD" backward-word
bindkey -M filterselect "OC" forward-word
bindkey -M filterselect "^J" down-line-or-history
bindkey -M filterselect " " up-line-or-history
# bindkey -M filterselect '^@' set-mark-command
# bindkey -M filterselect "^I" down-line-or-history
zstyle ':filter-select' max-lines 25
zstyle ':filter-select' case-insensitive yes
# https://gist.github.com/negipo/1858503
function zaw-src-ssh() {
if [[ $? == 0 ]]; then
candidates=(`cat ~/.ssh/known_hosts | ruby -ne 'puts $_.sub(/^(.*?)[ ,].*$/){$1}' | uniq`)
fi
actions=("zaw-src-ssh-command" "zaw-callback-append-to-buffer")
act_descriptions=("ssh" "append to edit buffer")
options=("-m")
}
function zaw-src-ssh-command () {
BUFFER="ssh $1"
zle accept-line
}
zaw-register-src -n ssh zaw-src-ssh
fi
# }}}
# https://github.com/zsh-users/zsh-history-substring-search {{{
if [ -e $ZDOTDIR/repository/zsh-history-substring-search/zsh-history-substring-search.zsh ]; then
source $ZDOTDIR/repository/zsh-history-substring-search/zsh-history-substring-search.zsh
bindkey '\e[A' history-substring-search-up # Up
bindkey '\e[B' history-substring-search-down # Down
fi
# }}}
# http://qiita.com/yuyuchu3333/items/b10542db482c3ac8b059 {{{
# if [ -e $ZDOTDIR/.zsh/scripts/chpwd_for_zsh.sh ]; then
# source $ZDOTDIR/.zsh/scripts/chpwd_for_zsh.sh
# fi
# }}}
# https://github.com/hchbaw/en.zsh {{{
# if [ -e ~/repository/en.zsh/en.zsh ]; then
# source ~/repository/en.zsh/en.zsh
# fi
# }}}
# https://github.com/zsh-users/zsh-syntax-highlighting {{{
if [ -e $ZDOTDIR/repository/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh ]; then
source $ZDOTDIR/repository/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets pattern cursor)
# No underlined
ZSH_HIGHLIGHT_STYLES[precommand]='fg=green'
ZSH_HIGHLIGHT_STYLES[path]='fg=yellow'
ZSH_HIGHLIGHT_STYLES[path_prefix]=''
ZSH_HIGHLIGHT_STYLES[path_approx]='fg=yellow'
fi
# }}}
# https://github.com/bootleq/vim-gitdiffall {{{
if [ -e $ZDOTDIR/scripts/gitdiffall.zsh ]; then
source $ZDOTDIR/scripts/gitdiffall.zsh
fi
# }}}
# }}} Plugins
# tmux {{{
if which tmux 2>&1 >/dev/null; then
# create session 0 if not exists
if ! tmux has-session -t 0; then
tmux new-session
fi
fi
# }}} tmux
# export RSENSE_HOME=$HOME/bin/rsense-0.3
# path=($path "$RSENSE_HOME/bin")
#emulate sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment