Skip to main content

My Prompt: Starship

··3 mins
This post is part of the Terminals, Shells, and Prompts series.

In this post I’ll be go over my current prompt of choice: Starship. Starship is a cross platform prompt written in rust. It’s extremely fast, easy to extend, and not to mention it’s easy on the eyes. I use Starship on many OS’s but in this post I’ll focus on PowerShell because there are a few neat tricks.

You can see the latest copy of my config files here: HeyItsGilbert/dotfiles

My goals #

These are my overall goals with my particular setup.

  1. Configs that I can sync between computers of any OS.
  2. Allow flexibility to add machine/environment specific options (e.g. work).
  3. Allow ability to swap any component when I see the next new shiny thing.

Initializing #

Starship includes PowerShell instructions on it’s walk through guide (which is a good sign of support in my opinion). It’s as simple as the following:

Invoke-Expression (&starship init powershell)

Under the hood, starship is overwriting the prompt function. If you read the My Shell: PowerShell, you may recall how I load starship in combination with intializing my profile.

if (Get-Command 'starship' -ErrorAction SilentlyContinue) {
  function Invoke-Starship-PreCommand {
    if ($global:profile_initialized -ne $true) {
      $global:profile_initialized = $true
      Initialize-Profile
    }
  }
  Invoke-Expression (&starship init powershell)
}

This checks if starship is available and intializes it. But what’s that pre-command? It runs before the starship prompt renders. This is how I can make sure all my stuff is initialized.

Configuration #

Configuration is all done in a single starship.toml file.

My Current Prompt

Prompt Layout #

I was a long time user of powerline, and I created something similar. Starship is able to print unicode characters.

format = """
[\uE0B6](fg:purple)[$directory](bg:purple)\
[\uE0B0](fg:purple bg:bright-black)$git_branch\
[\uE0B0](fg:bright-black bg:green)$git_status\
[\uE0B0](fg:green bg:12)$time\
[\uE0B0](fg:12 bg:none) $all $character\
"""
add_newline = true

Transient Prompt #

Transient prompt allows you to change your prompt after you execute your command. This can be useful if you’d prefer a simpler prompt in your scrollback and only keep the relevent info on your current prompt. The example given on the site replaces the prompt with just the character.

Read more: TransientPrompt in PowerShell

Pro-Tip: Prompt Profiles #

I only just discovered this! The starship prompt command supports a --profile flag. You can use this to tweak your prompt in a given context (which PowerShell) is great a determining. But you can mix this with the transient prompt, and you can print a short prompt.

I wanted something that would print the directory and the time. So I created the following profile:

[profiles]
short = """
[\uE0B6](fg:purple)[$directory](bg:purple)\
[\uE0B0](fg:purple bg:12)$time\
[\uE0B0](fg:12 bg:none) $character
"""

Pro-Tip: EngineEvent PowerShell.OnIdle #

Recently on the PowerShell discord someone was asking about how they could continually update their prompt. Super Genius Jaykul recommended using the EngineEvent system to execute commands and gave this command.

Since my prompt is two lines long we need to remove the last two lines and go back the beginning.

Register-EngineEvent -SourceIdentifier PowerShell.OnIdle {
  Write-Host "$([char]27)[2A$([char]27)[0G$(prompt)" -NoNewline
}

So let’s dissect this what we’re writing. I’ll break each bit into it’s own line and add a comment explaining what it does.

$([char]27) # Escape Sequence
        [2A # Cursor Control: Move N lines up (2 in this case)
$([char]27) # Escape Sequence
        [0G # Cursor Control: Move to column N (0 in this case)
  $(prompt) # Print the prompt function again.

Why not `e? While that’s valid, it doesn’t work in Windows PowerShell. To work for both, we can use [char]27.


In the past few posts in the series I’ve briefly touched on some ANSI escape codes, and in my next post I’ll be going into more depth. There is so much to cover but I’ll be hitting a few highlights and pointing to a ton of references.

How’s the technology from 1979 still shapping our daily life? Stay tuned and find out!

References #

More in this series