Skip to content

Instantly share code, notes, and snippets.

@syui
Created May 3, 2014 01:14
Show Gist options
  • Save syui/11489944 to your computer and use it in GitHub Desktop.
Save syui/11489944 to your computer and use it in GitHub Desktop.
# zsh_prompt {{{
setopt prompt_subst
setopt prompt_percent
setopt transient_rprompt
color256()
{
local red=$1; shift
local green=$2; shift
local blue=$3; shift
echo -n $[$red * 36 + $green * 6 + $blue + 16]
}
fg256()
{
echo -n $'\e[38;5;'$(color256 "$@")"m"
}
bg256()
{
echo -n $'\e[48;5;'$(color256 "$@")"m"
}
zstyle ':vcs_info:*' max-exports 3
zstyle ':vcs_info:hg:*' get-revision true
zstyle ':vcs_info:hg:*' use-simple true
autoload -Uz is-at-least
zstyle ':vcs_info:git:*' check-for-changes true
zstyle ':vcs_info:git:*' stagedstr "⇊"
zstyle ':vcs_info:git:*' unstagedstr "⭠"
zstyle ':vcs_info:git:*' formats '%K{green}%F{black}⮀%k%f%%F{white}%K{green} %s %f%k%K{blue}%F{green}⮀%k%f%%F{white}%K{blue} %b %f%k%K{black}%F{blue}⮀%k%f%%F{white}%K{black} %c%u %f%k'
autoload -Uz vcs_info
prompt_bar_left_self="%{%F{white}%K{020}%} %n%{%k%f%}%{%F{white}%K{020}%}@%{%k%f%}%{%F{white}%K{020}%}%m %{%k%f%}%{%B%F{020}%K{020}%}%{%b%f%k%}%K{026}%F{blue}⮀%k%f%{%B%F{white}%K{026}%} [%~] %{%k%f%b%}%{%k%f%}%K{069}%F{026}⮀%k%f%(?.%F{white}%K{069}%} COMP %k%f.%B%K{069}%F{red}%} ERROR %b%k%f)%{%K{045}%F{069}⮀%k%f%F{white}%K{045}%} LV.%h %{%k%f%}%K{black}%F{045}⮀%k%f"
prompt_bar_left="${prompt_bar_left_self} ${prompt_bar_left_status} ${prompt_bar_left_date}"
prompt_left='%{%F{white}%K{black}%} $SHELL %{%k%f%}%{%K{white}%F{black}⮀%k%f%B%F{black}%K{white}%} %# %{%b%k%f%f%}%K{black}%F{white}⮀%k%f > '
count_prompt_characters()
{
print -n -P -- "$1" | sed -e $'s/\e\[[0-9;]*m//g' | wc -m | sed -e 's/ //g'
}
update_prompt()
{
local bar_left_length=$(count_prompt_characters "$prompt_bar_left")
local bar_rest_length=$[COLUMNS - bar_left_length]
local bar_left="$prompt_bar_left"
local bar_right_without_path="${prompt_bar_right:s/%d//}"
local bar_right_without_path_length=$(count_prompt_characters "$bar_right_without_path")
bar_right=${prompt_bar_right:s/%d/%(C,%${max_path_length}<...<%d%<<,)/}
bar_right="%${bar_rest_length}<<${separator}${bar_right}%<<"
prompt_bar_left_2="%K{white}%F{black}⮀%k%f%{%F{black}%K{white}%} %l %K{black}%F{white}⮀%k%f%{%k%f%}%{%F{white}%K{black}%} $LANG %{%k%f%}%F{black}⮀"
PROMPT="${bar_left}${bar_right}${prompt_bar_left_2}"$'\n'"${prompt_left}"
case "$TERM_PROGRAM" in
Apple_Terminal)
PROMPT="${PROMPT}"
;;
esac
LANG=C vcs_info >&/dev/null
if [ -n "$vcs_info_msg_0_" ]; then
PROMPT="${bar_left}${bar_right}${vcs_info_msg_0_}${prompt_bar_left_2}"$'\n'"${prompt_left}"
fi
}
precmd_functions=($precmd_functions update_prompt)
# zsh_prompt }}}
# zsh_prompt_mode {{{
EMACS_INSERT=`bindkey -lL main | cut -d ' ' -f 3`
EMACS_INSERT="%K{black}%F{011}⮂%k%f%K{011}%F{075} % $EMACS_INSERT %k%f"
VIM_INSERT="%K{011}%F{075}⮂%k%f%K{075}%F{026} % INSERT %k%f%K{075}%F{black}⮂%k%f"
RPROMPT="$EMACS_INSERT$VIM_INSERT"
function zle-keymap-select {
EMACS_INSERT=`bindkey -lL main | cut -d ' ' -f 3`
if echo $EMACS_INSERT | grep emacs > /dev/null 2>&1;then
EMACS_INSERT="%K{black}%F{011}⮂%k%f%K{011}%F{034} % $EMACS_INSERT %k%f"
VIM_NORMAL="%K{011}%F{125}⮂%k%f%K{125}%F{015} % NORMAL %k%f%K{125}%F{black}⮂%k%f"
VIM_INSERT="%K{011}%F{075}⮂%k%f%K{075}%F{026} % INSERT %k%f%K{075}%F{black}⮂%k%f"
else
EMACS_INSERT="%K{black}%F{034}⮂%k%f%K{034}%F{011} % $EMACS_INSERT %k%f"
VIM_NORMAL="%K{034}%F{125}⮂%k%f%K{125}%F{015} % NORMAL %k%f%K{125}%F{black}⮂%k%f"
VIM_INSERT="%K{034}%F{075}⮂%k%f%K{075}%F{026} % INSERT %k%f%K{075}%F{black}⮂%k%f"
fi
RPROMPT="$EMACS_INSERT${${KEYMAP/vicmd/$VIM_NORMAL}/(main|viins)/$VIM_INSERT}"
zle reset-prompt
}
zle -N zle-keymap-select
# zsh_prompt_mode }}}
# zsh_prompt_bindkey {{{
function airchrome-bindmode-emacs () {
bindkey -e
EMACS_INSERT=`bindkey -lL main | cut -d ' ' -f 3`
if echo $EMACS_INSERT | grep emacs > /dev/null 2>&1;then
EMACS_INSERT="%K{black}%F{011}⮂%k%f%K{011}%F{034} % $EMACS_INSERT %k%f"
VIM_NORMAL="%K{011}%F{125}⮂%k%f%K{125}%F{015} % NORMAL %k%f%K{125}%F{black}⮂%k%f"
VIM_INSERT="%K{011}%F{075}⮂%k%f%K{075}%F{026} % INSERT %k%f%K{075}%F{black}⮂%k%f"
else
EMACS_INSERT="%K{black}%F{034}⮂%k%f%K{034}%F{011} % $EMACS_INSERT %k%f"
VIM_NORMAL="%K{034}%F{125}⮂%k%f%K{125}%F{015} % NORMAL %k%f%K{125}%F{black}⮂%k%f"
VIM_INSERT="%K{034}%F{075}⮂%k%f%K{075}%F{026} % INSERT %k%f%K{075}%F{black}⮂%k%f"
fi
RPS1="$EMACS_INSERT${${KEYMAP/vicmd/$VIM_NORMAL}/(main|viins)/$VIM_INSERT}"
RPS2=$RPS1
zle reset-prompt
}
zle -N airchrome-bindmode-emacs
bindkey -v '^e' airchrome-bindmode-emacs
bindkey -a '^e' airchrome-bindmode-emacs
function airchrome-bindmode-vi () {
bindkey -v
EMACS_INSERT=`bindkey -lL main | cut -d ' ' -f 3`
if echo $EMACS_INSERT | grep emacs > /dev/null 2>&1;then
EMACS_INSERT="%K{black}%F{011}⮂%k%f%K{011}%F{034} % $EMACS_INSERT %k%f"
VIM_NORMAL="%K{011}%F{125}⮂%k%f%K{125}%F{015} % NORMAL %k%f%K{125}%F{black}⮂%k%f"
VIM_INSERT="%K{011}%F{075}⮂%k%f%K{075}%F{026} % INSERT %k%f%K{075}%F{black}⮂%k%f"
else
EMACS_INSERT="%K{black}%F{034}⮂%k%f%K{034}%F{011} % $EMACS_INSERT %k%f"
VIM_NORMAL="%K{034}%F{125}⮂%k%f%K{125}%F{015} % NORMAL %k%f%K{125}%F{black}⮂%k%f"
VIM_INSERT="%K{034}%F{075}⮂%k%f%K{075}%F{026} % INSERT %k%f%K{075}%F{black}⮂%k%f"
fi
RPS1="$EMACS_INSERT${${KEYMAP/vicmd/$VIM_NORMAL}/(main|viins)/$VIM_INSERT}"
RPS2=$RPS1
zle reset-prompt
}
zle -N airchrome-bindmode-vi
bindkey -e '^v' airchrome-bindmode-vi
bindkey -v
bindkey -M viins 'jj' vi-cmd-mode
# zsh_prompt_bindkey }}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment