I’ve been using Linux for a long time; I always used the default prompt because I figured it was the default for a reason, so it was best to leave it like that.
It was until not long ago that I decided to customize my prompt to this:
To my surprise changing the prompt is not complicated at all, only a matter of changing the value of a variable: The PS1 variable.
Before we continue, this is not precisely a tutorial on how to change your prompt it is an example of how I customized mine.
I will show you the process that I followed; if you want to learn how to add more elements to the one you make, I recommend reading two posts, the first to check how the colours work and the second to learn about different components you can add.
Another thing to note is that I will be using Alacritty as my terminal, Nord with a different colour for the background as the theme and Fira Code as the font.
With that aside, let’s begin.
The first thing I did is think about what I wanted to include, which were three main sections:
- If I was in a git repo, I wanted to be able to see the current branch.
- The directory I was currently working on.
- That the commands I type appear in a new line. Let’s start with the first point. I needed a way to check if the current directory is a git repository; the command I found is:
|
|
2>/dev/null is there so that the command output doesn’t print to the terminal.
By default, the exit status of the previous command execution is in the variable $?, which is 0 if there are no errors; this means that if the value of the variable $? after the above command equals 0, then the current directory is a git repository.
That gave me a solution to the first problem; now I needed a way to get thecurrent git branch name; this is also a single command:
|
|
The enclosure in $() is to run it and print the output inside the echo double-quotes.
With those two commands, I wrote the following function:
|
|
It should be pretty self-explanatory about how it works, but if you have problems,here are the steps it follows:
- It checks if we are in a git repo with the command we learned.
- It compares the value of $? to 0 to check for errors in the previous\n execution.
- If there were no errors, it prints the current branch name.
NOTE: The characters “” and “” don’t look like that in the final prompt; they will look similar to the image if you use the Fira Code font.
I stored the call to the function inside a variable:
|
|
It is similar to calling it inside echo; the backslash calls the function every time we use the variable.
The previous will work, but it will have the default colour; I changed that next.
The basic format for adding colour to the terminal is:
|
|
I want the git section to be bold and purple; to make it easier to understand and edit in the future, I used a variable for the foreground colour, so in the end, it will look like this:
|
|
That solves the first and most complicated part of the custom prompt; the next two, showing the current working directory and having the commands in a new line, are as simple as the following:
|
|
It has three parts:
- The colour this time is yellow.
- \w: this shows the current working directory.
- \n: this is just a newline.
One final thing is to make clearer that the commands will appear in a new line; I did this by adding an arrow character.
|
|
It also sets the prompt character to purple and everything after that to the default colour.
The final step was to put everything together and export it.
|
|
The final script looks something like this:
|
|
I saved the script and added the following to my .bashrc and .bash_profile.
|
|
Finally, I sourced .bashrc and .bash_profile to apply the new prompt.
BONUS: you can add this line to your script to show a status indicator in your prompt; it will turn green if the previous command exited without errors and red if the opposite.
|
|
Remember to add the CHECK variable at the start of your PS1 variable.
|
|
As you can see, customizing the prompt is very simple; you can do many things to make it look just the way you like; it is just a matter of doing some research and trial and error.
Thank you for reading this post.