2

So I'm looking for a way to shorten the path in the prompt if it reaches a defined length, such as:

user1@localhost:~/Pictures/awesome_trip/

Would be fine, but if we cd into more sub-directories such as:

user1@localhost:~/Pictures/awesome_trip/first_day/mikes_camera/funny_pics

I would like the prompt to change to

user1@localhost:/.../funny_pics

I know how to change the prompt in the .bashrc

I was wondering if there is a way to make it dynamically such that I can get the current length of the working dir and check the length, and change the prompt. something along the lines of:

CUR_PROMPT=$(pwd); # get the working dir

CUR_PROMPT_LENGTH={#CUR_PROMPT}; # get the length

if[ $CUR_PROMPT_LENGTH -gt 20 ]; then

if prompt is long, render shorter version

PS1='\u@\h:...\w:$'; else

else render long version

PS1='\u@\h:\W:$'

Hopefully there is way to do this, Thank you for your answer.

1 Answers1

0

You can use something like the code below. Just add it to your .bashrc file in your home folder. It will basically make your prompt resemble "user@computername [dir] $" so it might look like telegonicaxx@laptop [shared] $

export PS1="\u@\h [\W] \\$\[$(tput sgr0)\]"

You can generate your own prompt style and colors using the website linked below

http://bashrcgenerator.com/

Roxana
  • 76
  • Thank you @Roxana, this is really neat website, thank you for sharing it. Not exactly what I was looking for though – Telegonicaxx Aug 28 '21 at 21:09