Have you ever seen someone with a custom terminal prompt with cool colors and wanted to do it but didn't know how? I hope this guide will be another outlet to your creativity!
I have tried ohmyZsh with and had terrible problems. While I've seen incredible guides for it, they just didn't work for me. I've tried other editors like iterm2 but I'm going to be honest, I prefer my default terminal and it works better with Alfred App in my experience. So, I am going to show you how I updated my zsh
terminal with my bash_profile
in OS Catalina (this will also work on regular bash terminals).
Currently, your terminal probably looks very similar to mine. Pretty boring right?
How do I do this magic?
First, we want to remove all the stuff terminal will call when you create a new window like the "Last login: date, time, on ttys001" . This is great for privacy and for having a clean work space. To do this, in terminal run:
touch ~/.hushlogin
To view that change, you'll need to restart your current shell. It should look like this:
Wow, now it looks super boring but it's pretty clean, right? π
Don't stress we are about to bedazzle it up!
Open up preferences for your terminal and go to your "profiles".
Let's create a new profile and name it "Default" for simplicity:
Have fun with these options but keep in mind contrast is important and we are going to set some more specific customizations momentarily.
I personally google'd background images and looked for a simple dark background and added a transparency to it and selected a font that I liked.
Next, click on the "Default" profile you just created so it's highlighted, then click the "Default button" at the bottom of the window (it's oposite of the +
button). This will set the profile you just created as the default when opening a new terminal.
NOTE: The next step is only required if you are running Catlina OS - versions before Catalina were bash so it's not necessary.
Head over to the shell tab as seen below and check the radio box for "Run command" and "Run inside Shell".
In the run command block, insert the following:
clear && . ~/.bash_profile
It should look like this:
It's still boring, now what?
Now that you have customized your terminal, it will likely do nothing at all because you probably don't have a bash_profile
. Let's create one!
In your actual terminal, run: touch ~/.bash_profile && open ~/.bash_profile
, now your text editor should open up your bash profile. We are going to create some bash code to customize our terminal output.
PS1 ="Your Wish is my command π§ "οΈ
Copy and paste the code above into your open text-editor and press βS
to save, then open a new command shell.
How do I make further customizations?
You can further customize this but you need to be using bash for it to compile more complex functions. You'd need to remove the "Run command" with clear && . ~/.bash_profile
in it out of the terminal preferences.
Now change the default shell to bash using the following command:
chsh -s /bin/bash
By the way you can swap the default shell back to zsh easily if you run chsh -s /bin/zsh
.
PROMPT_COMMAND=__prompt_command # Func to gen PS1 after CMDs parse_git_branch() { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/' | xargs } __prompt_command() { local EXIT="$?" # This needs to be first PS1="" local RCol='\[\e[0m\]' # local Red='\[\e[0;31m\]' local Gre='\[\e[0;32m\]' local BYel='\[\e[1;33m\]' local BBlu='\[\e[1;34m\]' local Pur='\[\e[0;35m\]' local Cyan='\[\e[1;96m\]' if [ $EXIT != 0 ]; then PS1+="${Red}[\t]${RCol}" # Add red if exit code non 0 else PS1+="${Gre}[\t]${RCol}" fi PS1+="${RCol} ${Cyan}\w" if [[ -z $(git status -s 2> /dev/null) ]]; then PS1+="${Gre}$(parse_git_branch)${Gre} π¦ ${RCol}" # Add Green on no modified/untracked git changes else PS1+="${BYel}$(parse_git_branch)${Gre} π¦ ${RCol}" fi }
Special shout out to Sam Fleeman for the bulk of the code above.
Copy and paste the code above into your open text-editor and press βS
to save, then open a new command shell.
You can edit the colors by exchanging the numbers inside the brackets.
31
β red32
β green33
β yellow34
β blue35
β purple36
β cyan37
β white
More details on color codes for bash here: https://mybyways.com/blog/os-x-terminal-color-prompt
This is what it should look like. If you have untracked changes in a git repo it will show yellow an you can update any of the color codes above for a more custom approach or even change the π¦ emoji (I'm learning deno so it seemed appropriate) into anything you want including your username with ${whoami}
. I do recommend putting a space after it though.
Nextβ¦
I hope you enjoyed this brief article on how to customize your terminal and I look forward to writing more.
If you found this article helpful, give me a shout on twitter β Iβd love to hear from you. @codingwithdrewk or connect with me on linkedin. As always, if you found any errors, just highlight it and mash that βRβ button on the right side of the screen and Iβll get those fixed right up!
Drew is a seasoned DevOps Engineer with a rich background that spans multiple industries and technologies. With foundational training as a Nuclear Engineer in the US Navy, Drew brings a meticulous approach to operational efficiency and reliability. His expertise lies in cloud migration strategies, CI/CD automation, and Kubernetes orchestration. Known for a keen focus on facts and correctness, Drew is proficient in a range of programming languages including Bash and JavaScript. His diverse experiences, from serving in the military to working in the corporate world, have equipped him with a comprehensive worldview and a knack for creative problem-solving. Drew advocates for streamlined, fact-based approaches in both code and business, making him a reliable authority in the tech industry.