I shortened my bash prompt by appending the following code block in ~/.bashrc
file.
get_PS1(){
local pwdmaxlen=30
local trunc_symbol="\[$(tput setaf 1)$(tput bold)\]..."
if [[ "${#PWD}" -gt "$pwdmaxlen" ]]; then
local right_chunk="\[$(tput setaf 4)$(tput bold)\]${PWD:$((${#PWD}-$pwdmaxlen)):${#PWD}}\[$(tput sgr0)\]"
PS1="${trunc_symbol}${right_chunk} \$ "
else
PS1="\[$(tput setaf 4)$(tput bold)\]\$(pwd) \$\[$(tput sgr0)\] "
fi
}
PROMPT_COMMAND=get_PS1
When it is active, I mean after exec bash
, if I create a virtual environment with virtualenv venv
and subsequently activate it with source venv/bin/activate
, (venv)
should have shown up at the beginning of bash prompt. There's no way to understand that I'm running a virtual environment, although which python
does in fact, show that I'm inside a virtual environment.
From what I understand, this happens only when I use PROMPT_COMMAND
variable. It doesn't matter, what type of modification I did inside get_PS1
function, It'll always behave this way. I know the PROMPT_DIRTRIM
way, but that's not what I'm looking for.
Bash version is 5.0.17(1)-release
. I'm running Kubuntu 20.04.3 LTS.
By the way, I learned this code block from here.
Thanks. Let me know if any other info is required.