Linux Fu: A Warp Speed Prompt

If you spend a lot of time at the command line, you probably have either a very basic prompt or a complex, information-dense prompt. If you are in the former camp, or you just want to improve your shell prompt, have a look at Starship. It works on the most common shells on most operating systems, so you can use it everywhere you go, within reason. It has the advantage of being fast and you can also customize it all that you want.

What Does It Look Like?

It is hard to explain exactly what the Starship prompt looks like. First, you can customize it almost infinitely, so there’s that. Second, it adapts depending on where you are. So, for example, in a git-controlled directory, you get info about the git status unless you’ve turned that off. If you are in an ssh session, you’ll see different info than if you are logged in locally.

However, here’s a little animation from their site that will give you an idea of what you might expect:

Installation

The web site says you need a Nerd Font in your terminal. I didn’t remember doing that on purpose, but apparently I had one already.

Next, you just have to install using one of the methods they provide, which depends on your operating system. For Linux, you can run the installer:


curl -sS https://starship.rs/install.sh | sh

Sure, you should download it first and look to make sure it won’t reformat your hard drive or something, but it was fine when we did it.

Finally, you have to run an init command. How you do that depends on your shell and they have plenty of examples. There’s even a way to use it with cmd.exe on Windows!

Customization

The default isn’t bad but, of course, you are going to want to change things. Oddly, the system doesn’t create a default configuration file. It just behaves a certain way if it doesn’t find one. You must make your own ~/.config/starship.toml file. You can change where the file lives using an environment variable, if you prefer, but you still have to create it.

The TOML file format has sections like an INI file. Just be aware that any global options have to come before any section (that is, there’s no [global] tag). If you put things towards the bottom of the file, they won’t seem to work and it is because they have become part of the last tag.

There are a number of modules and each module reads data from a different section. For example, on my desktop I have no need for battery status so:

[battery]
disabled = true

Strings

In the TOML file you can use single or double quotes. You can also triple a quote to make a string break lines (but the line breaks are not part of the string). The single quotes are treated as a literal, while double quotes require escape characters for special things.

You can use variables in strings like $version or $git_branch. You can also place part of a string in brackets and then formating for the string in parenthesis immediately following. For example:

'[off](fg:red bold)'

Finally, you can have a variable print only if it exists:

 '(#$id)'

If $id is empty, this does nothing. Otherwise, it will print the # and the value.

Globals and Modules

You can find all the configuration options — and there are many — in the Starship documentation. Of primary interest is the global format variable. This sets each module that is available. However, you can also use $all to get all the otherwise unspecified modules. By default, the format variable starts with $username $hostname. Suppose you wanted it to be different. You could write:

format='$hostname ! $username $all'

You’ll find many modules that show the programming language used for this directory, version numbers, and cloud information. You can shut things off, change formatting, or rearrange. Some user-submitted customizations are available, too. Can’t find a module to do what you want? No problem.

Super Custom

I wanted to show the status of my watercooler, so I created a custom section in the TOML file:


[custom.temp]
command = 'temp-status|grep temp|cut -d " " -f 7'
when = true
format='$output°'

The command output winds up in, obviously, $output. In this case, I always want the module to output and the format entry prints the output with a degree symbol after it. Easy!

Of Course, There are Always Others

There are other prompt helpers out there, especially if you use zsh (e.g., Oh My Zsh). However, if you aren’t on zsh, your options are more limited. Oh My Posh is another cross-shell entry into the field. Of course, you don’t absolutely need any of these. They work because shells give you variables like PS1 and PROMPT_COMMAND, so you can always roll your own to be as simple or complex as you like. People have been doing their own for a very long time.

If you want to do your own for bash, you can get some help online. Or, you could add help to bash, too.

26 thoughts on “Linux Fu: A Warp Speed Prompt

    1. Fully agree. Never, ever do this. It’s a security disaster. You essentially give full control over your user account to the downloaded script. That script could easily create an autostart entry that starts a credential exfiltration tool whenever you log in. And with that you are completely owned.

      So do yourself a favor and whenever you see “[curl|wget] | ” then just download the script manually, verify that it doesn’t compromise security and once you are 100% sure that it is sane run the script with the respective tool.

      1. The people that control starship.rs could just as well put a credential exfiltration tool into the starship software itself.

        Security wise curl into (ba)sh doesn’t matter at all. Either you trust starship.rs and then you can execute whatever code you get from them, including install.sh. Or you don’t and likewise don’t execute anything from them, neither a install.sh script nor their prompt software (or check every piece of code you execute, not just install.sh).

        1. I always grab it first. It isn’t so much that I’m worried about starship.rs per se. But they could have been compromised or I could have accidentally put in some phishing site starship.rss or whatever. I agree it is pretty statistically unlikely, but it is like clicking a link in an e-mail. Yeah, you can probably click it but yeah you should probably check it as carefully as you can, too.

    2. As I mentioned, I don’t do that either. I grab it, open it, look at it, and then run it if I’m satisfied with what it is doing. I also won’t answer sudo prompts in such a case ;-) But it is what they have in their installation instructions.

  1. Hurrying while working in shell is fucking stupid. I always take my time to look at what I’m doing, and for some really spicy commands I’d prefix it with # first to make sure a mistake or accidental missclick doesn’t fuck thing up badly.

    1. I’m not sure what part of this you are talking about, but one thing I do if I’m not completely confident I have the expansion I mean to have is put echo first:

      echo rm -r [abx]*

      Then if I’m happy, I can pull the history line and delete the echo with a fairly high degree of confidence.

  2. Interesting … But the prompt is one thing I never thought about wasting my time ‘customizing’. I just use the command line a lot using ‘as is’.

    What’s with the swearing in above post??? Not necessary in a public family friendly area :) .

  3. I think I’ve outgrown shell ‘bling.’ I see an article like this, and I think ‘oh great, another set of customizations to maintain across multiple boxes.’ I’ve been there, I’ve done that, and I’ve learned that maintaining ‘bling’ takes more effort than it saves. Especially if you have multiple boxes that all need to be configured, synchronized, and updated… becomes a real PITA. So, although I’m not the biggest fan of good old bash, it’s what I use. Because I can count on it to be there all of the time, to work the same way all of the time, and to support my trained muscle memory all of the time. It may not be the shiny new, it may not have all the bells and whistles, but it’s always there and it always works the same way. It’s one less thing I need to worry about configuring each time I stand up a new box. And in the real world, I may not always get to decide what packages are installed on a box that I use or administer. So becoming proficient with the baseline software options that are available to me is much more valuable than a fully customized environment.

    1. Though, I am appreciative of this series. I love Linux tips and tricks, even if they’re not always ‘for me’! Keep ’em coming!

      Suggestions for the future: .bashrc / profile tips, tmux and/or screen configs, PGP settings (like a U2F/PGP SSH keyfob) – I’d love to see a custom keyboard – like QMK with a u2f support holding all my keys, for example!)

    2. I hear you on being able to work with the defaults, but I also like a few little helpers. Auto complete for certain tools, setting environment variables, things like that.

      So I have a very simple script that copies my bash and vim configuration from my laptop to all the remote boxes I work on. I haven’t looked at starship yet, not sure if it would just be another couple of files to copy or of the installation is a bit more invasive.

    3. Exactly. If you can work with the defaults, you can work everywhere — except for a bling box. But their owner won’t let you near it anyway….

      That said: give Ansible a try to keep your boxes in sync.

        1. It’s still a box, but someone put a lot of effort into making it bling. Maybe you remember you or your kids watching “Pimp my Ride” on MTV.

          The computer equivalent is where every part of the system is modded and themed and customized.

          Basically a hammer with glitter on the handle.

          =)

    4. Same here – for similar reasons I also DGAF about desktop customisation these days either, I have been on Linux Mint for over a decade now and it’s a very peaceful life with no surprises.

  4. Once upon a time, I tweaked the default Debian(/Ubuntu/Mint) $PS1 to take the hostname, and reduce it down to a number between 1 and 8. I then used that as a color in place of the default green for the ‘user@hostname’. It gave me a way to quickly distinguish between most of my boxen without having to customize anything on an individual level.

    Of course, as others have mentioned, it was just one more thing to keep up with. And it eventually was lost as too many systems were upgraded without remembering to backup .bashrc first.

  5. I hate it when things are distributed that way!

    Gentoo has an ebuild for it in portage. No scary script execution necessary as long as you trust the distro’s package maintainers.

  6. I put the following into my .bashrc file:

    PS1=’pwd: ‘

    Once done, the command prompt always tells you where you are. For example, when I’m rooting around in my current procect is shows:

    /Work/Projects/Ultrasonic: cd src
    /Work/Projects/Ultrasonic/src:

    I always know where I am when entering a command, and don’t have to keep track. Also, for particularly deep structures you can cut/paste part of the prompt to help form commands, and you can easily tell how many “../” tokens to get to a parallel part of the structure.

    /Work/Projects/AILib/c/src/include/HtmlBuilder/build:

    I’ve grown so used to this over the years, I don’t see how anyone can do significant command line work without using it. :-)

    1. The same result can be achieved with

      PS1=”\w”

      Other things that can be added to the prompt are time, date, username, hostname, text color and flashing, and history number. See manpages for bash and termiinal-color.d . I use

      PS1=”[\u]!:[\e[32m]\h[\e[0m]:[\e[36m]\w[\e[31m]>[\e[0m]”

  7. An install script that automatically downloads a bunch of arbitrary code, no thanks.

    Not to mention a few other things that make this a non starter.

    It only supports Intel and ARM platforms.

    Requires Windows, Linux, MacOS, or FreeBSD.

    Requires a terminal that fully supports unicode.

Leave a Reply to JohnuCancel reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.