This post intends to give a great start to customizing your prompt, that thing you’re looking at all day whilst cruising the depths of linux ;P

If you use oh-my-zsh or any one of the other frameworks, it’s quite easy to not care and just chose one of the provided themes. Below I’ll talk about some cool projects and sources of content for managing themes in your prompt.

Antigen

It’s even easier to dynamically switch themes using antigen, an open-source project which manages grabbing plugins and themes from github.

antigen theme inanimate/darkblood-modular darkblood-modular

Would automatically pull the theme I use from github and apply it. See more @ the readme.

The Gallery

Another great resource is the zsh theme gallery! It’s a pretty place I don’t get to visit too often which displays a terminal for each listed theme showing you exactly what it looks like.

example from zshthem.es

The Help of Others

There are tons of posts about peoples hacks on their prompts with in-depth info to help you decide or make your own.

Modularity

Whatever theme you chose, try to make it as modular as possible. For example, I use the darkblood theme:

However, the original is kind of gross:

PROMPT=$'%{$fg[red]%}?[%{$fg_bold[white]%}%n%{$reset_color%}%{$fg[red]%}@%{$fg_bold[white]%}%m%{$reset_color%}%{$fg[red]%}] [%{$fg_bold[white]%}/dev/%y%{$reset_color%}%{$fg[red]%}] %{$(git_prompt_info)%}%(?,,%{$fg[red]%}[%{$fg_bold[white]%}%?%{$reset_color%}%{$fg[red]%}])
%{$fg[red]%}?[%{$fg_bold[white]%}%~%{$reset_color%}%{$fg[red]%}]>%{$reset_color%} '
PS2=$' %{$fg[red]%}|>%{$reset_color%} '

So one day in 2013, I spent a few hours and made it way better…heres a snippet:

First, some of our elements

username_and_host(){
    ## Here we define the username@host part. Some color formatting because of @ symbol.
    echo -n "%n%{$reset_color%}%{$fg[red]%}@%{$fg_bold[white]%}%m"
}

git_stuffs(){
    ## Enter our git prompt
    if [[ "$(uname)" != "Darwin" ]]; then
        if [[ $(stat -L --file-system --format=%T .) != "nfs" ]]; then
            echo -n "%{$(git_prompt_info)%}%(?,,%{$fg[red]%}[%{$fg_bold[white]%}%?%{$reset_color%}%{$fg[red]%}])"
        fi
    fi
}

directory_loc(){
    ## print out the directory location
    echo -n "%~"
}

pointy_arrow(){
    ## This little guy goes just before prompt ends aka before cursor sits. Generally, this is after directory printing.
    ## I have left the reset color after this little guy since we don't want our text being colored.
    echo -n "%{$fg[red]%}> "
}

Then the functions that define what to include

build_firstline_prompt(){
    ## This defines the top left line of the prompt
    start_firstline_prompt
    base username_and_host
    if [ $STY ]; then
       base in_screen
    fi
    git_stuffs
}

build_secondline_prompt(){
    ## This defines the second line, just below the first (left side)
    start_secondline_prompt
    base directory_loc nospace  ## since this is the second to last printed out function (just before final declaration)
    pointy_arrow
    final
}

build_right_prompt(){
    ## Defining our right justified prompt (top)
    base fs_capacity
    base cur_fs
    base cur_date_time
    final
}

Finally, put it all together

PROMPT='$(build_firstline_prompt)
$(build_secondline_prompt)'
RPROMPT='$(build_right_prompt)'

And the result

  • in a screen session called stnftw
  • last command exited with error code 1
  • env is my dotfiles git repo which has no changes

Conclusion

There are a lot of customizations you can do to make your prompt. Just be careful to not get too caught up in all of the awesome things you can do ;)

Mario Loria is a builder of diverse infrastructure with modern workloads on both bare-metal and cloud platforms. He's traversed roles in system administration, network engineering, and DevOps. You can learn more about him here.