A Simple, Useful Fish Shell Prompt for Java/Python Programmers
When I wrote my last article discussing how I set up my new Mac, I realized that I’ve been using the same shell prompt for a long, long time. It’s pretty boring, looks pretty much the same in both my Bash and Fish shells, but has worked just fine for me.
But it made me curious, I spend a large portion of my work day in a terminal, maybe it was time for me to revisit my simple prompt and modernize it.
And so I began my descent down the rabbit hole of shell customization …
Trying Out the Work of Others …
My tour began with looking at popular prompt themes. For fish, there were two that I kept seeing over and over: spacefish and bobthefish. I tried them both. They had many interesting ideas, were immensely flexible, and could provide a tremendous variety of state information. If you want to know your git branch, docker version, rust version, ruby version, etc., you could easily display the information … usually in very bright colors. However, as I tweaked around with them I realized three things:
- The colors, symbols, and ornamentation were very distracting. My eyes didn’t know where to look.
- Much of the information either was not particular useful to me or displayed in a way that limited its usefulness.
- It took a huge amount of code to support those prompts. On the order of thousands of lines of fish shell code. I didn’t want that level of complexity. I wanted something that was much more straightforward to customize to my liking.
Deciding on What I Want in a Prompt …
Tweaking and trying out many, many different variations of other prompt themes helped me decide what I wanted in my updated prompt:
- Minimal distraction. No bright colors or powerline segments. It needs to provide the information I need without drawing my attention. Because I work simultaneously on many different machines and as different users, I want user and host information displayed along with the path. This helps provide context of what I’m doing and where I’m working. I also like scp-style paths such as
brian@dharma:/Users/brian
; I can copy and paste those when I need to move files around with scp.
- If I’m in a directory that is not writable, the prompt should indicate that.
- When in a git repo, it should display my current git branch as well as the usual stats of number of commits, adds, pushes and pulls awaiting. Fish has a handy built-in function for this called __fish_git_prompt.
- When I ‘m developing, my Java and Python versions may change depending on what I’m working on. If I’m working on a Java or Scala project, I’d like to know which version of the JVM I’m using. If I’m working in a Python project, I want to see which version of Python is in use.
Keeping it Simple … the Implementation
I wanted to avoid having to install anything special to make the prompt work. I almost succeeded with that ambition. In my fish_prompt.fish, I use Java and Python unicode characters that are available in the patched Nerd Fonts. But they are entirely optional. If needed, you can add a nerd font on a Mac using homebrew and then set them as the font used by your Terminal/iTerm2. Here’s how I installed the nerd-font patched version of Hasklig:
brew tap homebrew/cask-fonts
brew cask install font-hasklig-nerd-font
fish_prompt.fish
To use this as your prompt, put the following code in ~/.config/fish/functions/fish_prompt.fish
.
bash
p.s. For consistency, I’d like my bash prompt to look similar. Here’s a similar looking (although much, much less functional) bash prompt that you can add to your ~/.bash_profile
or ~/.bashrc
file.
PS1=$'\n\[\033[90m\]\u@\h:$PWD`git branch 2> /dev/null | grep -e ^* | sed s/"* "// | sed s/^/"\[\033[31\]m ⏣ \[\033[0;32m\]"/`\n\[\033[90m\]>> \[\033[m\]'