I was a long time tcsh user and I have used a custom prompt, which includes the date/time, host, and a largish path. In the past, when I've used venv, I would switch to bash first, just because everything's geared toward bash, and I liked that the current environment would be added to the prompt, and I had left the bash prompt uncustomized. However, since I've been using bash more and more, I decided to migrate a version of my custom tcsh prompt. To do that, I added this to my bash profile:
changeprompt() { DIR=`dirs | perl -e '$p=scalar(<STDIN>);$p=~s/.*(?=\S{40}\Z)/.../;print($p)'`;export PS1="\h[\D{%F %T}]:$DIR\$ "; }
PROMPT_COMMAND="changeprompt"
It leaves me with a prompt like this:
gen-rl-macbookair[2021-02-12 14:01:24]:~/PROJECT-local/TRACEBASE$
I wanted a longer path in the prompt that is manually truncated with an ellipsis at a given length based on characters instead of directory depth, which is why I set PROMPT_COMMAND
.
However, I realized yesterday when I started working on a venv for the first time in a long time, that activating a venv would not change the prompt to prepend (environment_name)
to the prompt, since I had set PROMPT_COMMAND
.
I fixed the issue by editing the activate script to unset COMMAND_PROMPT
upon activate and reset COMMAND_PROMPT
upon deactivate...
But I'm wondering if there exists a better way to maintain a custom command prompt like this that allows for a variably prepended string that a venv activate script adds - without needing custom edits to any activate script.
PS1
instead ofCOMMAND_PROMPT
PS1
features don't support the feature I want of limiting the path length, which is why I usePROMPT_COMMAND
. I want to see as many dirs of my path within 40 characters. That's what myPROMPT_COMMAND
does. I think all I should need to do is store the old prompt command when a new env gets activated and restore it when deactivated. Or maybe I can change my prompt command to use the existing PS1 value, or modify it instead of set it.