Skip to content

Instantly share code, notes, and snippets.

@agnoster
Last active December 10, 2024 10:16
Show Gist options
  • Save agnoster/3712874 to your computer and use it in GitHub Desktop.
Save agnoster/3712874 to your computer and use it in GitHub Desktop.
My ZSH Theme

agnoster.zsh-theme

A ZSH theme optimized for people who use:

  • Solarized
  • Git
  • Unicode-compatible fonts and terminals (I use iTerm2 + Menlo)

For Mac users, I highly recommend iTerm 2 + Solarized Dark

Compatibility

NOTE: In all likelihood, you will need to install a Powerline-patched font for this theme to render correctly.

To test if your terminal and font support it, check that all the necessary characters are supported by copying the following command to your terminal: echo "\ue0b0 \u00b1 \ue0a0 \u27a6 \u2718 \u26a1 \u2699". The result should look like this:

Character Example

What does it show?

  • If the previous command failed (✘)
  • User @ Hostname (if user is not DEFAULT_USER, which can then be set in your profile)
  • Git status
    • Branch () or detached head (➦)
    • Current branch / SHA1 in detached head state
    • Dirty working directory (±, color change)
  • Working directory
  • Elevated (root) privileges (⚡)

Screenshot

Future Work

I don't want to clutter it up too much, but I am toying with the idea of adding RVM (ruby version) and n (node.js version) display.

It's currently hideously slow, especially inside a git repo. I guess it's not overly so for comparable themes, but it bugs me, and I'd love to hear ideas about how to improve the performance.

Would be nice for the code to be a bit more sane and re-usable. Something to easily append a section with a given FG/BG, and add the correct opening and closing.

Also the dependency on a powerline-patched font is regrettable, but there's really no way to get that effect without it. Ideally there would be a way to check for compatibility, or maybe even fall back to one of the similar unicode glyphs.

# vim:ft=zsh ts=2 sw=2 sts=2
#
# agnoster's Theme - https://gist.github.com/3712874
# A Powerline-inspired theme for ZSH
#
# # README
#
# In order for this theme to render correctly, you will need a
# [Powerline-patched font](https://gist.github.com/1595572).
#
# In addition, I recommend the
# [Solarized theme](https://github.com/altercation/solarized/) and, if you're
# using it on Mac OS X, [iTerm 2](http://www.iterm2.com/) over Terminal.app -
# it has significantly better color fidelity.
#
# # Goals
#
# The aim of this theme is to only show you *relevant* information. Like most
# prompts, it will only show git information when in a git working directory.
# However, it goes a step further: everything from the current user and
# hostname to whether the last call exited with an error to whether background
# jobs are running in this shell will all be displayed automatically when
# appropriate.
### Segment drawing
# A few utility functions to make it easy and re-usable to draw segmented prompts
CURRENT_BG='NONE'
PRIMARY_FG=black
# Characters
SEGMENT_SEPARATOR="\ue0b0"
PLUSMINUS="\u00b1"
BRANCH="\ue0a0"
DETACHED="\u27a6"
CROSS="\u2718"
LIGHTNING="\u26a1"
GEAR="\u2699"
# Begin a segment
# Takes two arguments, background and foreground. Both can be omitted,
# rendering default background/foreground.
prompt_segment() {
local bg fg
[[ -n $1 ]] && bg="%K{$1}" || bg="%k"
[[ -n $2 ]] && fg="%F{$2}" || fg="%f"
if [[ $CURRENT_BG != 'NONE' && $1 != $CURRENT_BG ]]; then
print -n "%{$bg%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR%{$fg%}"
else
print -n "%{$bg%}%{$fg%}"
fi
CURRENT_BG=$1
[[ -n $3 ]] && print -n $3
}
# End the prompt, closing any open segments
prompt_end() {
if [[ -n $CURRENT_BG ]]; then
print -n "%{%k%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR"
else
print -n "%{%k%}"
fi
print -n "%{%f%}"
CURRENT_BG=''
}
### Prompt components
# Each component will draw itself, and hide itself if no information needs to be shown
# Context: user@hostname (who am I and where am I)
prompt_context() {
local user=`whoami`
if [[ "$user" != "$DEFAULT_USER" || -n "$SSH_CONNECTION" ]]; then
prompt_segment $PRIMARY_FG default " %(!.%{%F{yellow}%}.)$user@%m "
fi
}
# Git: branch/detached head, dirty status
prompt_git() {
local color ref
is_dirty() {
test -n "$(git status --porcelain --ignore-submodules)"
}
ref="$vcs_info_msg_0_"
if [[ -n "$ref" ]]; then
if is_dirty; then
color=yellow
ref="${ref} $PLUSMINUS"
else
color=green
ref="${ref} "
fi
if [[ "${ref/.../}" == "$ref" ]]; then
ref="$BRANCH $ref"
else
ref="$DETACHED ${ref/.../}"
fi
prompt_segment $color $PRIMARY_FG
print -Pn " $ref"
fi
}
# Dir: current working directory
prompt_dir() {
prompt_segment blue $PRIMARY_FG ' %~ '
}
# Status:
# - was there an error
# - am I root
# - are there background jobs?
prompt_status() {
local symbols
symbols=()
[[ $RETVAL -ne 0 ]] && symbols+="%{%F{red}%}$CROSS"
[[ $UID -eq 0 ]] && symbols+="%{%F{yellow}%}$LIGHTNING"
[[ $(jobs -l | wc -l) -gt 0 ]] && symbols+="%{%F{cyan}%}$GEAR"
[[ -n "$symbols" ]] && prompt_segment $PRIMARY_FG default " $symbols "
}
## Main prompt
prompt_agnoster_main() {
RETVAL=$?
CURRENT_BG='NONE'
prompt_status
prompt_context
prompt_dir
prompt_git
prompt_end
}
prompt_agnoster_precmd() {
vcs_info
PROMPT='%{%f%b%k%}$(prompt_agnoster_main) '
}
prompt_agnoster_setup() {
autoload -Uz add-zsh-hook
autoload -Uz vcs_info
prompt_opts=(cr subst percent)
add-zsh-hook precmd prompt_agnoster_precmd
zstyle ':vcs_info:*' enable git
zstyle ':vcs_info:*' check-for-changes false
zstyle ':vcs_info:git*' formats '%b'
zstyle ':vcs_info:git*' actionformats '%b (%a)'
}
prompt_agnoster_setup "$@"
@HackerTheMonkey
Copy link

In had a similar problem on Mac OSX using Iterm2, it has been sorted by changing the terminal font config to use any of the powerline fonts for non-ascii characters.

@petertom51
Copy link

I have a segment issue of the theme.
If I enter a git directory, the segment colour becomes white.
image

I use iTerm2 v3.0.2 + Solarized Dark
Any suggestion?
Thanks.

@hugohil
Copy link

hugohil commented Jul 21, 2016

The thing that worked for me was to disable the "Use different font for non-ASCII text" option in iTerm settings.
Hope this helps ( @petertom51 maybe ?)

preferences_and_1__hugo_hibou____sources_js_soixante_bbscope-front__zsh_

@ruichao-factual
Copy link

@petertom51 Maybe you need to change the contrast.
image

@xsenechal
Copy link

+1 for contrast 👍

@nbraud
Copy link

nbraud commented Aug 9, 2016

@agnoster I think this is missing a setopt PROMPT_SUBST

@Viijay-Kr
Copy link

Hi agnoster ..the theme is awesome ......I am trying to add remote url of the repo.Is there a space for contribution

@dead-sea256
Copy link

Hello! I don't see the custom root, when used "sudo -s". How to resolve this? Its Arch, gnome-terminal.
_003

@allansun
Copy link

allansun commented Oct 6, 2016

@petertom51 you should tick this box

preferences

@tkizm1
Copy link

tkizm1 commented Oct 8, 2016

virtualenv promot needed

@bbuivn
Copy link

bbuivn commented Oct 13, 2016

Hi there, still looking for the ability to show the time into the left prompt :)

@azat-io
Copy link

azat-io commented Jan 19, 2017

@quoniammm How did you solve your problem?

I've the same
screenshot from 2017-01-19 20-23-24

@HristoEftimov
Copy link

When I try to update my agnoster theme to show only the current directory: prompt_segment blue black '%d' doesn't work... it shows again the full path. Any ideas?

@ziluvatar
Copy link

To show only the current folder this worked for me:

prompt_dir() {
  prompt_segment blue black '%c'
}

@mpetazzoni
Copy link

You should change print -Pn to print -n on line 100 (https://gist.github.com/agnoster/3712874#file-agnoster-zsh-theme-L100) to avoid getting pwned by https://github.com/njhartwell/pw3nage

@xu3352
Copy link

xu3352 commented Mar 20, 2017

awesome!!!
after change iTerm2->Profiles->Text:Font and Non-ASCII Font, it's work for me

@darmawan01
Copy link

any link how to use agnoster.zsh-theme. i am newbie

@FVenneri
Copy link

I'm a almost new with agnoster, I would like to know if I can add a symbol to show if there are stashed items to the prompt, just to know it without the need to run git stash list.
If no, can you help me to customize it by myself?
Thanks

@gHashTag
Copy link

Opening the terminal I see a red cross, how to remove it?
2017-09-29 18 59 19

@cschell
Copy link

cschell commented Nov 17, 2017

Just dropping the link to the GitHub Repository of this theme, since all the development and discussions seem to take place there: https://github.com/agnoster/agnoster-zsh-theme

@himanshuxd
Copy link

himanshuxd commented Nov 26, 2017

@gHashTag Commet out the line in /home/username/.bash/themes/agnoster-bash/agnoster.bash that has $RETVAL

prompt_status() {
    local symbols
    symbols=()
    #[[ $RETVAL -ne 0 ]] && symbols+="$(ansi_single $(fg_color red))✘"
    [[ $UID -eq 0 ]] && symbols+="$(ansi_single $(fg_color yellow))⚡"
    [[ $(jobs -l | wc -l) -gt 0 ]] && symbols+="$(ansi_single $(fg_color cyan))⚙"

    [[ -n "$symbols" ]] && prompt_segment black default "$symbols"
}

See the bold line it is commented to prevent that cross.

@alexandar87
Copy link

Hi guys, i just got my new macbook its on Sierra and after installing theme the terminal didnt changed color and when i go to profiles i dont see anything added there... Any help?

@shiroamada
Copy link

@alexandar87 are u using iTerm 2 and Zsh? It should be works with additional font installed

@Script-Nomad
Copy link

It's unfortunate that gist's don't have issue reporting features. Anyhow I wanted to make note of something I discovered and subsequently fixed after the agnoster theme was initially borked for me. When you are using a distro that does not name its ifconfig interfaces according to the standard naming convention (I'm looking at you, Ubuntu >-<), it causes an error to appear below every carriage return you feed into the terminal.

To fix this, I recommend introducing a loop that checks for connected and configured interfaces by calling ifconfig -a and doing some parsing to pull a list of the configured interfaces. Here is how I did it using standard bash commands:

ifconfig -a | grep -i ': ' | cut -d ':' -f1

enp0s31f6
lo
vmnet1
vmnet8
wlp4s0

You can further narrow it down by pulling the ethernet interface which, even though it's not convention, universally starts with 'e' in the interface name, and the same is true for 'w' on wireless interfaces. If you introduce a new function in your agnoster theme that checks for this, it can save a lot of people some headache and confusion when first running the script. 👍

@wczmatthew
Copy link

there is a very short current dir in the theme of robbyrussel, just show the current folder's name. How is it work in the theme of agnoster???
1.in agnoster theme(PS: I have not done the power-line problem)
1841526714375_ pic
2. in robbyrussel theme. I want just display "HB说明" folder's name in the agnoster. Help!!!
1851526714555_ pic

Copy link

ghost commented Jul 7, 2018

Hey guys.
I'm using fish with agnoster theme.

I saw this:
image
in: https://github.com/oh-my-fish/oh-my-fish/blob/master/docs/Themes.md and

as @tonilaukka mentioned;

in your screenshot you ssh milly. Do you have your remote server set up with zsh/oh-my-zsh/agnoster-theme or how do you get a prompt like that?

Now I'm wondering how can I bring that ssh: tag (in picture) to my agnoster theme? Is that possible?

Regards.
@agnoster

@ran-dall
Copy link

For anyone trying to install this theme with ZPlugin. This is what I added in my .zshrc.

### Agnoster Theme
zplugin snippet OMZ::lib/git.zsh
zplugin light agnoster/agnoster-zsh-theme
setopt promptsubst 

@isathish
Copy link

Uploading Screenshot 2019-10-26 at 8.25.24 PM.png…
Still, I am facing the icon issue

@hypefi
Copy link

hypefi commented Jan 5, 2021

quick fix that worked for me :
cd ~/.oh-my-zsh/themes
vi agnoster.zsh-theme
replace (\ue0b0) with ▓ (\u2593) and  (\ue0a0) with ⎇ (\u2387)

@davidhcefx
Copy link

davidhcefx commented Feb 27, 2022

@agnoster I like your theme. But I noticed that in line 118, you wrote:

[[ $(jobs -l | wc -l) -gt 0 ]] && symbols+="%{%F{cyan}%}$GEAR"

I would however suggest changing it to %1(j.%{%F{cyan}%}$GEAR.) for better performance.

Proof-of-concept

$ GEAR="\u2699"
$ f1() { local s; [[ $(jobs -l | wc -l) -gt 0 ]] && s+="%{%F{cyan}%}$GEAR"; print -n "$s" }
$ f2() { local s; s+="%1(j.%{%F{cyan}%}$GEAR.)"; print -n "$s" }
$ timer() { date +%S.%N >> /tmp/timer.log }
$ timer_newline() { echo "" >> /tmp/timer.log }
$ precmd_functions=(timer)

# test original version (w/ and w/o background jobs)
$ PROMPT='$(f1) > '
$ <long press Enter>
$ timer_newline; cat &
$ <long press Enter>
$ timer_newline; kill %%

# test modified version (w/ and w/o background jobs)
$ PROMPT='$(f2) > '
$ <long press Enter>
$ timer_newline; cat &
$ <long press Enter>
$ timer_newline; kill %%

After some processing we got:

for test in open('/tmp/timer.log').read().strip().split('\n\n'):
    lines = test.split('\n')
    assert len(lines) >= 10, "Must have at least 10 logs. Try again and press Enter longer."
    lines = list(map(float, lines[-10:]))
    print(sum(lines[i + 1] - lines[i] for i in range(9)) / 10)

"""
0.34356011
0.3448782799999998
0.1385942700000001
0.13762055999999986
"""

Therefore it's an increase in performance!

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