Image for

My New Bash Prompt (PS1)

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?

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.

Share!

Did you like this blog post? Why not share it with your network!