Source

dotfiles / zshrc

Full commit
# ~/.zshrc
source "$HOME/.shellrc"

# Import modules/stuff
autoload -U  compinit   && compinit
autoload -U  promptinit && promptinit
autoload -U  colors     && colors


### Variables
esc="$(echo -e '\e')"
up="${esc}[A"
down="${esc}[B"
bold="${esc}[1m"

### Hooks to update title
# Usage: set_title "new-title"
function set_title {
  title_set="${esc}]0"
  title_stop="$(echo -e '\007')"
  Title="$title_set;$@$title_stop"
  echo -n "$Title"
}

function rprompt {
# local exit_code=$?            # exit code of command
  local count=$(jobs | wc -l)   # no. of background jobs

  # Report no. of background jobs if >0
  if [ $count -gt 0 ]; then
    echo -n " %{$fg[cyan]%}⎇ %j %{$reset_color%}"
  fi
}

function exit_line {
  local exit_code=$?
  if [ $exit_code -ne 0 ]; then
    local w=$(( (COLUMNS - ${#exit_code}) / 2 ))
    local r=$(( COLUMNS - w - ${#exit_code} ))
    printf "%s%${w}s%s%${r}s%s" "%{$bg[red]$fg[white]$bold%}" '' "$exit_code" '' "%{$reset_color%}"
  fi
}

if [ $TERM != 'linux' ]; then
  function chpwd {
    pwd="$(pwd | sed s:$HOME:~:)"
    set_title "$USER@$HOST $pwd"
  }
  chpwd
fi

### Setup prompt
setopt PROMPT_SUBST

PROMPT='$(exit_line)%{$fg[$prompt_color]%}%n@%m%{$reset_color%} %~
%{$fg[yellow]%}%# %{$reset_color%}'
RPROMPT='%{$up%}$(rprompt)%{$down%}'
PS2="%{${fg[yellow]}%}» %{$reset_color%}"

# History
if [ $TERM != 'linux' ]; then
  export HISTSIZE=2000
  export HISTFILE="$HOME/.history"
  export SAVEHIST="$HISTSIZE"
fi
setopt hist_ignore_all_dups
#setopt extendedglob


#### Aliases ########################################################
# (zsh specific--also see .shellrc)
alias G='grep -P'
alias L='less -r'


#### Fix key bindings ###############################################
bindkey -v         # vi-like bindings

# Borrow a bunch of neat stuff from the default emacs-like bindings, that the
# vi-like bindings lack.
bindkey    "^A"    beginning-of-line
#bindkey    "^D"    delete-char-or-list
bindkey    '^E'    end-of-line
#bindkey    "^G"    send-break
bindkey    "^H"    backward-delete-char
bindkey    "^N"    down-line-or-history
#bindkey    "^O"    accept-line-and-down-history
bindkey    "^P"    up-line-or-history
#bindkey    "^R"    history-incremental-search-backward
bindkey    "^R"    history-incremental-pattern-search-backward
#bindkey    "^S"    history-incremental-search-forward
bindkey    "^V"    quoted-insert
bindkey    '^?'    backward-delete-char

# vi-"normal mode" bindings
bindkey -a "^A"    beginning-of-line
bindkey -a '^E'    end-of-line
bindkey -a '^K'    kill-whole-line

# Also fix a bunch of other keys
bindkey '^[[H'     beginning-of-line # <Home>
bindkey '^[[F'     end-of-line       # <End>
bindkey "^[[1;5D"  backward-word     # <C-left>
bindkey "^[[1;5C"  forward-word      # <C-right>

bindkey "^K"       kill-whole-line
bindkey "^U"       undo
bindkey "^['"      quote-line
bindkey "^[[39;5u" quote-line