Mike Kasberg

Husband. Father. Software engineer. Ubuntu Linux user.

Image for My New Bash Prompt (PS1)

My New Bash Prompt (PS1)

28 Jun 2021

I recently spent some time improving my bash prompt. I already had a lightly customized prompt, but wanted to see if I could find something I liked that provided even more information.

I’ve known about powerline for years, but never actually sat down to experiment with it on my own machine, so that’s where I started. I installed the Ubuntu Mono Nerd Font and installed and configured powerline-status. The prompt was nice enough, but right off the bat there were a few things I didn’t like about it. There was no easy way to add a newline to the prompt, it was noticeably slow, and customizing the config seemed more complicated than it needed to be.

Powerline Bash prompt

Because I was a little disappointed with powerline, I looked for some alternatives. powerline-shell seemed like a cool project – based on the original powerline, but with a focus on the shell prompt. I liked powerline-shell better. The config was easier to understand, and there was even easy newline support (Hooray 🎉)! But powerline-shell was still slow enough that there was a noticeable lag to generate the prompt. It was jarring enough that I kept searching for alternatives.

powerline-go looks like a good alternative to powerline-shell that claims to be much faster. I can’t recommend it from personal experience though, because I never actually tried it. After already installing two different powerline prompts I didn’t like that much, it was getting a bit tedious to install all these different packages just to realize I didn’t like them that much. And I’d also begun to realize, after trying a couple different config variataions of both powerline-status and powerline-shell, that they looked kinda cool but were generally painful to set up and didn’t provide much info I couldn’t easily add to my own lightly customized bash prompt, without installing anything.

So, that’s what I did. I’d noticed that pretty much every powerline package worked by breaking the prompt line into “segments” – individual pieces of information that could be glued together to build the complete prompt. So I refactored my own PS1 customizations to do something similar.

Before the change, my PS1 looked like this:

PS1='\n\[${CYAN}\]${debian_chroot:+($debian_chroot)}\u\[${RESET}${BOLD}\] \[${RED}\]>>\[${GREEN}\]> \[${CYAN}\]\w\[${RESET}\]$(__git_ps1 " (\[${MAGENTA}\]%s\[${RESET}\])")\n\[${BOLD}\]\$\[${RESET}\] '

After the change, I’d refactored my PS1 into an entirely separate file, with each “segment” generated in its own function. Because of the functions, the PS1 is much easier to read.

local ps1="\n$(__mkps1_box_top)$(__mkps1_debian_chroot)$(__mkps1_exitcode)$(__mkps1_time)$(__mkps1_username)$(__mkps1_arrows) $(__mkps1_workdir)$(__mkps1_git)\n$(__mkps1_box_bottom)$(__mkps1_user_prompt)";

So, where am I at now?

  • ✅ I’m still using powerline-status as a statusline in Vim. I like the info it provides, and I haven’t noticed any lag on the Vim statusline that’s significant enough to bother me.
  • ❌ I’m not using any powerline fonts. Not in my Vim statusline, and not in my shell prompt. Although they look kind of cool, I realized I value portability and ease of setup over the look. (I don’t want to configure every application that might render a terminal to use a powerline font.) I’ve configured the powerline Vim plugin to use ascii mode, and I didn’t include any powerline characters in my own prompt.
  • ➡ I am using a few Unicode characters in my own prompt though. They seem to be supported by default pretty much everywhere these days, and it seemed like a good compromise to get a cool look without the powerline font.
  • 😍 I love the newlines in my prompt, and I’d never switch back to a prompt without newlines. I use a newline above the prompt to create separation from the previous output, and I put the $ on a newline because I like having lots of room to type and I like having it consistently on the left side of the screen. Overall, both these newlines make it easier to visually scan through terminal output.
  • ⏱ I added a timestamp to my prompt. I’ve found myself wishing I had this several times in the past when I wanted to know how long a command’s been running.
  • ❓ I also added an exit code indicator to my prompt – it only shows if the previous command failed.
  • 📦 Did you know __git_ps1() (the prompt function that ships with git) includes several environment variables you can use to show more info? I enabled several of these, and they’re great!

Sharing My Work

I share my dotfiles on GitHub, so you can look at my .bashrc or .mkps1.sh script if you’re curious to see what I’m doing, or if you’d like to try it yourself. You don’t need to install anything special to make it work - it’s all just shell script. (I found it somewhat odd that many of the various bash prompt generators require other languages like NodeJS, Python, etc.) If you like it, let me know – I’m considering writing a more detailed tutorial about how you can do something similar to what I’ve done to customize your own Bash or Zsh prompt.

And if you came here looking for a recommendation about how to set up a powerline prompt in Bash, I’d probably start by trying powerline-go. It should be easy to install with precompiled binaries. (You don’t need to install Go yourself - just put the binaries in ~/bin or any similar location, run chmod +x powerline-go, and when editing your .bashrc as in the readme make sure the path to the powerline-go binary is where you saved it.) Powerline-go comes with reasonable defaults, it will look nice, and it should be faster than some of the other options since it’s written in Go.

Finally, if you’re using zsh instead of bash (as you probably are if you’re on a modern version of macOS), you should check out Oh My Zsh as a starting point if you want to customize your prompt. It comes with a good selection of themes that are easy to install by setting the ZSH_THEME variable in your ~/.zshrc, and you can add additional customizations if you want to. If you choose a theme that uses a powerline-like prompt, you’ll still need to install a Nerd Font to render the special characters correctly. Either way, I’m sure you can find something you like!

About the Author

Mike Kasberg

👋 Hi, I'm Mike! I'm a husband, I'm a father, and I'm a senior software engineer at Strava. I use Ubuntu Linux daily at work and at home. And I enjoy writing about Linux, open source, programming, 3D printing, tech, and other random topics. I'd love to have you follow me on X or LinkedIn to show your support and see when I write new content!

Share!

If you enjoyed this blog post, I'd love it if you could share it with your network!

I run this blog in my spare time. There's no need to pay to access any of the content on this site, but if you find my content useful and would like to show your support, buying me a coffee is a small gesture to let me know what you like and encourage me to write more great content!

You can also support me by visiting LinuxLaptopPrices.com, a website I run as a side project.