My Linux Prompt
It’s time to create the personal prompt you always wanted.
After this tutorial you will have something that will look like this, but with this tutorial you can configure the prompt anyway you like.
mainuser -> your user name
21:12 -> display the time
lab01 -> hostname of machine
anytext -> whatever you want to add
~/.mlp -> current directory
arrow -> prompt
Let’s start by creating a directory to store and hid this file so you don’t see it every time you list your home directory.
sudo mkdir ~/.mlp sudo nano ~/.mlp/.mlp
To customize the prompt we must edit the PS1 environment variable. PS1 stands for Prompt String 1 and to set this variable we export the variable with its data.
First we will begin with defining our colours and a reset to set the colours back to normal. These are ANSI escape sequences and a quick search on the net will provide all the possible colours and their escape sequences.
# _F_ = forground colour BLUE_F_="\[\e[34m\]" RED_F_="\[\e[31m\]" WHITE_F_="\[\e[97m\]" GREEN_F_="\[\e[32m\]" # _B_ = background colour BLUE_B_="\[\e[44m\]" RED_B_="\[\e[41m\]" GREEN_B_="\[\e[42m\]" BLACK_B_="\[\e[49m\]" RESET="\[\e[0m\]"
Next, we want to create some characters for our prompt. Define the characters like this: LESS_THAN_=”\356\202\262″. To create these characters, you can calculate these string of numbers by hand, but this method has a little bit of a learning curve. Instead, I use a simple command line script that takes the character and generates the string for you. Here is the script:
echo -n X | od -t o1 | head -1 | cut -d' ' -f2- | sed -e 's#\([0-9]\+\) *#\\\1#g'
Replace the capital X with any character you want. For this example, I will use this character “╬”. It is better to copy and replace the character in Notepad, as doing it in the terminal pushes more then one character.
echo -n ╬ | od -t o1 | head -1 | cut -d' ' -f2- | sed -e 's#\([0-9]\+\) *#\\\1#g'
Executing this script produces this output: \342\225\254.
For the test character, I created this variable TEST_CHARACTER and assigned it the output from the script above. Each number must be preceded by the escape character ‘\’. The example character is defined as such: TEST_CHARACTER=”\342\225\254″
Moving back to our terminal, add this variable definition after the colour statements.
Setting the prompt to display the character is as simple as assigning our character variable to the prompt string and exporting it.
export PS1=TEST_CHARACTER;
This is what you should now have for your .mlp file in nano.
# _F_ = forground colour BLUE_F_="\[\e[34m\]" RED_F_="\[\e[31m\]" WHITE_F_="\[\e[97m\]" GREEN_F_="\[\e[32m\]" # _B_ = background colour BLUE_B_="\[\e[44m\]" RED_B_="\[\e[41m\]" GREEN_B_="\[\e[42m\]" BLACK_B_="\[\e[49m\]" RESET="\[\e[0m\]" TEST_CHARACTER="\342\225\254" export PS1="$TEST_CHARACTER"
Save the file and execute it using . .mlp. The test character will now be the prompt, as seen below.
$. .mlp ╬
Using the method above we can create all the characters we need for the prompt. Here are the eight characters you will need defined below. This image is what they look like.
LESS_THAN_="\356\202\262" GREATER_THAN_="\356\202\260" RIGHT_ARROW_="\342\226\267" LINE_="\342\224\200" ARC_DOWN_RIGHT_="\342\225\255" ARC_UP_RIGHT_="\342\225\260" BOX_VERTICAL_RIGHT_="\342\224\234" VERTICAL_LINE_="\342\224\202"
Let’s start creating a prompt, having the username appear. To do this, use the \u escape sequence to display the username. Set PS1 as below.
export PS1="\u"
Remember to execute the script and you should see your username as the prompt.
$. .mlp mainuser
Below is a list of the sequences you will be using:
\u username
\w working directory
\A current time
\n new line
Below is a complete example of a prompt string and what it looks like. I have spread it over multiple lines to make it easier to read and debug. Each line ends with a ‘\’ for line concatenation.
export PS1="\n\ $BLUE_F_$BLACK_B_$ARC_DOWN_RIGHT_$LINE_\ $RED_F_$LESS_THAN_\ $WHITE_F_$RED_B_ \u \ $RED_F_$BLUE_B_$GREATER_THAN_\ $WHITE_F_ \A \ $BLUE_F_$GREEN_B_\ $GREEN_B_$WHITE_F_ $(hostname) \ $RED_F_$GREEN_B_$LESS_THAN_\ $RED_B_$WHITE_F_ anytext \ $RED_F_$BLACK_B_$GREATER_THAN_\n\ $BLUE_F_$BLACK_B_$VERTICAL_LINE_\ $WHITE_F_$BLACK_B_\w\n\ $BLUE_F_$BLACK_B_$VERTICAL_LINE_\n\ $BLUE_F_$ARC_UP_RIGHT_\ $BLUE_F_$RIGHT_ARROW_$RESET"
Final code for you should have for this example.
# _F_ = forground colour BLUE_F_="\[\e[34m\]" RED_F_="\[\e[31m\]" WHITE_F_="\[\e[97m\]" GREEN_F_="\[\e[32m\]" # _B_ = background colour BLUE_B_="\[\e[44m\]" RED_B_="\[\e[41m\]" GREEN_B_="\[\e[42m\]" BLACK_B_="\[\e[49m\]" RESET="\[\e[0m\]" LESS_THAN_="\356\202\262" GREATER_THAN_="\356\202\260" RIGHT_ARROW_="\342\226\267" LINE_="\342\224\200" ARC_DOWN_RIGHT_="\342\225\255" ARC_UP_RIGHT_="\342\225\260" BOX_VERTICAL_RIGHT_="\342\224\234" VERTICAL_LINE_="\342\224\202" export PS1="\n\ $BLUE_F_$BLACK_B_$ARC_DOWN_RIGHT_$LINE_\ $RED_F_$LESS_THAN_\ $WHITE_F_$RED_B_ \u \ $RED_F_$BLUE_B_$GREATER_THAN_\ $WHITE_F_ \A \ $BLUE_F_$GREEN_B_\ $GREEN_B_$WHITE_F_ $(hostname) \ $RED_F_$GREEN_B_$LESS_THAN_\ $RED_B_$WHITE_F_ anytext \ $RED_F_$BLACK_B_$GREATER_THAN_\n\ $BLUE_F_$BLACK_B_$VERTICAL_LINE_\ $WHITE_F_$BLACK_B_\w\n\ $BLUE_F_$BLACK_B_$VERTICAL_LINE_\n\ $BLUE_F_$ARC_UP_RIGHT_\ $BLUE_F_$RIGHT_ARROW_$RESET"
Now it is up to you to create you own prompt. Add new characters, colours, and make your design unique for your needs.
Share this content:
Leave a Reply