ChatGPT Wrote my Shell Prompt

I have, for years now, wanted a cool shell prompt — something worth showing off on r/linux or even r/unixporn. Not enough to dig into the weeds and figure out how to write one myself, but enough that it came up every once in a while. Then, the other day, I had a great idea: why not just let ChatGPT make it for me?

After some fiddling, I came up with the following schema for my new prompt:

{user}@{hostname}:{directory} [{local branch} {upstream branch} {branch tracking symbol}|{local status}] {privilege character} 

User and Hostname #

If the previous command succeeded, {user} and {hostname} would be green but the at symbol (@) would be left black to differentiate the current username from the current hostname; if the previous command failed, those portions of the prompt would be colored red to indidcate that something had gone wrong.

Directory #

The directory variable would be replaced by the current working directory with $HOME abbreviated with a tilde (~). {directory} would always be light blue to make it easy to know where I was at all times.

git Information #

The variables in the square brackets, which present git information, would be determined according to a series of rules.

If the current working directory was not a git repository, this entire block would be ommitted. This would help keep my prompt clean when navigating the filesystem and debugging applications.

{local branch} would show the local branch if it existed. If there were unstaged changes in the local branch, the local branch would be colored orange. This visual cue would remind me to act on changes.

{upstream branch} would show the remote tracking branch. I typically work with multiple remotes, and on multiple occasions I have accidentally pushed code to the wrong location. This helps mitigate those types of accidents.

{branch tracking symbol} is determined by the following criteria (thanks Martin Gondermann):

{local status} is determined by the following criteria:

Privilege Character #

The privilege character would be a dollar sign ($) when running as an unprivileged user, and an octothorp (#) when running with elevated privileges. Note that the privilege character would appear on the second line. Although not necessary in most scenarios, when the username and hostname got long and the prompt had to display a lot of information for a complex git repository, this helped keep everything readable.

In practice, my prompt can look as simple as this:

zac@wk01:~/
$

Here I can easily see that I am logged in as zac on wk01, in the home directory, and the home directory is not a git repository.

If I move into the ~/tools/ folder, I can quickly ascertain that this is a git repository, I am on the master branch, and there are no unstaged changes.

zac@wk01:~/tools [master origin/master | ✔]
$

Having altered one of the scripts in the project, my prompt would change to indicate that the repository had 1 unstaged change.

zac@wk01:~/tools [master origin/master | ● 1 ✖ 1 ✚ 1 ✖ - 1 … 1]
$

I suspect there are some bugs in the prompt, particular in the {local status} formulation, but so far this has worked well for me. The underlying code is available on GitHub, in the shell-prompt repository.

Permalink.