I’ve been using Git Bash on Windows, and recently, I had the chance to try Codespace. I loved how the Codespace terminal setup was very clean and simple.
When you open the terminal, there is no path prefix, i.e. when working in the main directory:
$ write_here
and as you move into the subdirectories, it writes the path prefix starting from the first subdirectory after the main one
new/ $ write_here
new/new2/ $ write_here
Basically, all it does is strips away the path prefix of main directory from the full path prefix of any directory
I wanted to replicate this on my main setup, but couldn't find a clean way of scripting this.
What i've tried so far
I tried to customize the prompt by modifying the .bashrc file. For example, I used these two scripts:
(PS: my main directory is /e/VScode)
PS1='${PWD#/e/VScode/} $ ' <br/>
PS1='${PWD//\/e\/VScode\//}/ $ '
Both of these did half the job. They show the reduced path of the subdirectories,
(for example new/new2/ $ write_from_here)
but when it comes to the main directory, it still showed /e/VScode/ $ write_here.
I’m still relatively new to the community, so I’ll admit that I’m not very familiar with all the scripting conventions and techniques yet. But I tried a workaround by introducing separate case for the main directory:
PS1='$(if [ "$PWD" = "/e/VScode" ]
then
echo "$ ";
else
echo "${PWD#/e/VScode/}/ $ ";
fi)'
And of course, the results are exactly how I wanted them to be.
But:
Even though I can proceed with the workaround, I'm still curious about the ideal, clean, one-cased script line to achieve this result. And why the initial script lines did not work for that one case?