Linux terminal. Declutter the terminal prompt.
Making life easier for the Linux beginner
With this post, I provide quick and easy solutions to declutter (or shorten) your terminal prompt, making it easier for you to work in the terminal. You can play around with these concepts and devise different implementations as you continue to learn.
Problem: Cluttered terminal line leaves little space
When working on a small/narrow computer screen, and/or you are working deep inside a nested directory structure, the remaining line space to type commands becomes limited and makes for a cluttered look and feel. I don't particularly appreciate working with multi-line commands.
Solution: Shorten the terminal prompt
Method 1: Set PROMPT_DIRTRIM
Limit the number of trailing directories to be retained. The removed characters are replaced with an ellipsis.
$ PROMPT_DIRTRIM=1
Example:
demo-user@host1:~/CodeProjects/Python/web/django_projects/mysite/cart$ PROMPT_DIRTRIM=1
demo-user@host1:~/.../cart$
Method 2: Set the PS1 variable
Show only the current working directory in square brackets, but exclude the username and hostname:
$ PS1="[\W]\\$ "
Example:
demo-user@host1:~/CodeProjects/Python/web/django_projects/mysite/cart$ PS1="[\W]\\$
[cart]$
Method 3: Get the MOST space possible
To get the most space on the command line, include a line break in PS1, so that the working directory and the actual prompt appear on two separate lines.
$ PS1="\w\n>"
Example:
demo-user@host1:~/CodeProjects/Python/web/django_projects/mysite/cart$ PS1="\w\n>"
~/Code-Projects/Python-Projects/vscode-projects/Code Challenges/PiBites/Regular bites
>
A Bonus of Method 3: If you do not change or shorten the prompt, this format has the added benefit of always showing you the current user, hostname and full path to your current working directory while having a clean command line just below.
Problem: How to construct a unique prompt
You can easily construct a unique format for the prompt.
For a list of all the parameters you can implement to control the prompt, see here Controlling the Prompt.
Example:
Show only the current username (use "\u") and hostname (use "\h"), separated with a '@' symbol and ending with '~$'.
PS1="\u@\h ~$"
Example:
demo-user@host1:~/CodeProjects/Python/web/django_projects/mysite/cart$ PS1="\u@\h ~$"
demo-user@host1 ~$
References:
Cover Photo by Gabriel Heinzer on Unsplash
Subscribe to my newsletter
Read articles from Rudi van Rooyen directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by