Starship Custom Prompt Components

This is a small addition to my previous post about ZSH prompt configuration with Starship.rs.

The last time I tried it, I couldn’t get the custom component configuration to work properly. In the meantime, I found a way to at least make a small custom component—custom time format—to be rendered in the prompt. The config is shown below:

format = """\
${custom.datetime} $directory $username$hostname $git_branch
$character """

[custom.datetime]
command = "date +'%F (%T)'"
when = "true"
format = '[$symbol($output)](bold)'

There are three important things about this:

  1. your format configuration has to include ${custom.NAME} where NAME is the name of your custom prompt component;
  2. you have to provide a block labeled [custom.NAME] which provides a condition via when because it will not render otherwise; and,
  3. in the custom command’s format string, the $output variable can be referenced to include the output of the command passed via command.

Multiple custom components are possible, you just have to prefix every block that declares one with custom. (e.g.[custom.ANOTHER_CUSTOM_COMPONENT])and reference it with ${custom.ANOTHER_CUSTOM_COMPONENT} in the top-level format property.

The command passed to when must be a shell command which returns 0 whenever the condition is met. As in the configuration above, we can trivially set when = "true" to use the true command (which always returns 0) and the custom prompt component will be shown every time the prompt is rendered.

Other possible conditions are

  • The current directory contains a file whose name is in files
  • The current directory contains a directory whose name is in directories
  • The current directory contains a file whose extension is in extensions
  • The current Operating System (std::env::consts::OS) matchs with os field if defined.

See the custom commands documentation on Starship.rs.

Here’s my updated prompt with some more changes including the custom time command.

ZSH prompt with Starship custom command

The starship.toml which produces the prompt is publically available on my Github. Feel free to steal! ;)