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"

### 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"
}

# Pretty-printed backgrounded jobs count for the RPROMPT
function rprompt_jobs {
}

# Pretty-printed exit code for the RPROMPT
function rprompt_exitcode {
  if [ $proc_save != 0 ]; then
    echo -n "%{$fg[yellow] %}%?%{$fg[red]%} ✖%{$reset_color%}"
  else
    echo -n "%{$fg[green] %}✔%{$reset_color%}"
  fi
}

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 "
  fi

  # Report exit code
  if [ $exit_code -ne 0 ]; then
    echo -n "%{$fg[yellow] %}%?%{$fg[red]%} ✖%{$reset_color%}"
  else
    echo -n "%{$fg[green] %}✔%{$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='%{$fg[$prompt_color]%}%n@%m%{$reset_color%} %~
%{${fg[yellow]}%}%# %{$reset_color%}'
RPROMPT='%{$up%}$(rprompt)%{$down%}'
PS2="${fg[yellow]}» $reset_color"

# see also: `precmd` above (for pseudo-RPROMPT)

# History
export HISTSIZE=2000
export HISTFILE="$HOME/.history"
export SAVEHIST="$HISTSIZE"
setopt hist_ignore_all_dups


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


#### 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