Skip to content

Instantly share code, notes, and snippets.

@paulirish
Created January 2, 2012 19:17
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save paulirish/1551766 to your computer and use it in GitHub Desktop.
Save paulirish/1551766 to your computer and use it in GitHub Desktop.
my zsh prompt with titlebar text setting
#
# my zsh theme with iTerm titlebar manip.
# see video to see the bug I have.. any help would be HUGELY appreciated :)
# http://youtu.be/U-h9XCCq0q4
#
# also
# https://github.com/sjl/oh-my-zsh/
# http://sage.ucsc.edu/xtal/iterm_tab_customization.html
# ----------------------------------------------------------------------
# iTerm Tab and Title Customization
# from http://sage.ucsc.edu/xtal/iterm_tab_customization.html
function set_title_tab {
function settab {
if [[ $TERM_PROGRAM == iTerm.app && -z "$KONSOLE_DCOP_SESSION" ]];then
tab_label="$PWD:h:t/$PWD:t"
rlength="20"
echo -ne "\e]1;${(l:rlength:)tab_label}\a"
else
tab_label="$PWD:h:t/$PWD:t"
rlength="20"
if [[ -n "$KONSOLE_DCOP_SESSION" && ( -x $(which dcop) ) ]];then
dcop "$KONSOLE_DCOP_SESSION" renameSession "${(l:rlength:)tab_label}"
else
: # do nothing if tabs don't exist
fi
fi
}
function settitle {
title_lab=$PWD
echo -ne "\e]2;$title_lab\a"
}
function title_tab_chpwd { settab ; settitle }
title_tab_chpwd
if [[ "$TERM_PROGRAM" == "iTerm.app" ]];then
function title_tab_preexec { echo -ne "\e]1; $(history $HISTCMD | cut -b7- ) \a" }
function title_tab_precmd { settab }
else
function title_tab_preexec { echo -ne "\e]2; $(history $HISTCMD | cut -b7- ) \a" }
function title_tab_precmd { settitle }
fi
}
function prompt_char {
git branch >/dev/null 2>/dev/null && echo '±' && return
echo '○'
}
function battery_charge {
echo `$BAT_CHARGE` 2>/dev/null
}
function virtualenv_info {
[ $VIRTUAL_ENV ] && echo '('`basename $VIRTUAL_ENV`') '
}
function hg_prompt_info {
hg prompt --angle-brackets "\
< on %{$fg[magenta]%}<branch>%{$reset_color%}>\
< at %{$fg[yellow]%}<tags|%{$reset_color%}, %{$fg[yellow]%}>%{$reset_color%}>\
%{$fg[green]%}<status|modified|unknown><update>%{$reset_color%}<
patches: <patches|join( → )|pre_applied(%{$fg[yellow]%})|post_applied(%{$reset_color%})|pre_unapplied(%{$fg_bold[black]%})|post_unapplied(%{$reset_color%})>>" 2>/dev/null
}
ZSH_THEME_GIT_PROMPT_PREFIX=" on %{$fg[magenta]%}"
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%}"
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[green]%}!"
ZSH_THEME_GIT_PROMPT_UNTRACKED="%{$fg[green]%}?"
ZSH_THEME_GIT_PROMPT_CLEAN=""
function set_prompt_width {
function phil_precmd {
###
# Get APM info.
if which ibam > /dev/null; then
PR_APM_RESULT=`ibam --percentbattery`
elif which apm > /dev/null; then
PR_APM_RESULT=`apm`
fi
}
typeset -ga precmd_functions
precmd_functions+=phil_precmd
}
setopt extended_glob
preexec () {
if [[ "$TERM" == "screen" ]]; then
local CMD=${1[(wr)^(*=*|sudo|-*)]}
echo -n "\ek$CMD\e\\"
fi
}
setprompt () {
###
# Need this so the prompt will work.
setopt prompt_subst
###
# See if we can use colors.
autoload colors zsh/terminfo
if [[ "$terminfo[colors]" -ge 8 ]]; then
colors
fi
for color in RED GREEN YELLOW BLUE MAGENTA CYAN WHITE; do
eval PR_$color='%{$terminfo[bold]$fg[${(L)color}]%}'
eval PR_LIGHT_$color='%{$fg[${(L)color}]%}'
(( count = $count + 1 ))
done
PR_NO_COLOUR="%{$terminfo[sgr0]%}"
###
# See if we can use extended characters to look nicer.
typeset -A altchar
set -A altchar ${(s..)terminfo[acsc]}
PR_SET_CHARSET="%{$terminfo[enacs]%}"
PR_SHIFT_IN="%{$terminfo[smacs]%}"
PR_SHIFT_OUT="%{$terminfo[rmacs]%}"
PR_HBAR=${altchar[q]:--}
PR_ULCORNER=${altchar[l]:--}
PR_LLCORNER=${altchar[m]:--}
PR_LRCORNER=${altchar[j]:--}
PR_URCORNER=${altchar[k]:--}
###
# Decide if we need to set titlebar text.
case $TERM in
xterm*)
PR_TITLEBAR=$'%{\e]0;%(!.-=*[ROOT]*=- | .)%n@%m:%~ | ${COLUMNS}x${LINES} | %y\a%}'
;;
screen)
PR_TITLEBAR=$'%{\e_screen \005 (\005t) | %(!.-=[ROOT]=- | .)%n@%m:%~ | ${COLUMNS}x${LINES} | %y\e\\%}'
;;
*)
PR_TITLEBAR=''
;;
esac
###
# Decide whether to set a screen title
if [[ "$TERM" == "screen" ]]; then
PR_STITLE=$'%{\ekzsh\e\\%}'
else
PR_STITLE=''
fi
###
# APM detection
if which ibam > /dev/null; then
PR_APM='$PR_RED${${PR_APM_RESULT[(f)1]}[(w)-2]}%%(${${PR_APM_RESULT[(f)3]}[(w)-1]})$PR_LIGHT_BLUE:'
elif which apm > /dev/null; then
PR_APM='$PR_RED${PR_APM_RESULT[(w)5,(w)6]/\% /%%}$PR_LIGHT_BLUE:'
else
PR_APM=''
fi
###
# Finally, the prompt.
PROMPT='\
$PR_SET_CHARSET$PR_STITLE${(e)PR_TITLEBAR}\
%{$fg[magenta]%}%n%{$reset_color%} in %{$fg_bold[green]%}${PWD/#$HOME/~}%{$reset_color%}$(hg_prompt_info)$(git_prompt_info)
$(virtualenv_info)$(prompt_char)$(set_title_tab) '
# $(set_prompt_width)
# The Rightside prompt
RPROMPT=' $PR_SHIFT_IN$PR_BLUE$PR_SHIFT_OUT\
(%D{%a,%b%d})$PR_NO_COLOUR'
PS2='$PR_CYAN$PR_SHIFT_IN$PR_HBAR$PR_SHIFT_OUT\
$PR_BLUE$PR_SHIFT_IN$PR_HBAR$PR_SHIFT_OUT(\
$PR_LIGHT_GREEN%_$PR_BLUE)$PR_SHIFT_IN$PR_HBAR$PR_SHIFT_OUT\
$PR_CYAN$PR_SHIFT_IN$PR_HBAR$PR_SHIFT_OUT$PR_NO_COLOUR '
}
setprompt
@max-giro
Copy link

max-giro commented Aug 8, 2012

Just switched to zsh, but I had the same one with bash. The only solution I found is to add a new line (\n) at the end of the tab title creation.

Tried and here works. Just change to:

title_lab="${PWD}\n"

in settitle

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment