Change iTerm2 profile depending on current directory
Domanda
Using my .bash_profile, I used to change my iTerm2 profile per machine with the following command:
echo -e "\033]50;SetProfile=ssh\a"
Now, I want to change it depending on my current working directory.
Ideally must work something like this:
- Once I enter /mnt/production or any subfolder inside, iTerm should change to production profile.
- Once I leave that directory, should change to bash profile.
Is there any way of doing that?
Soluzione
Finally I found a solution using bash-preexec.
That utility have the following function:
precmd
Executed just before each prompt. Equivalent to PROMPT_COMMAND, but more flexible and resilient.
To install bash-preexec:
brew install bash-preexec
Then I have to add the following to my .bash_profile:
if [ -f $(brew --prefix)/etc/profile.d/bash-preexec.sh ]; then
. $(brew --prefix)/etc/profile.d/bash-preexec.sh
precmd() {
if [[ $PWD == /mi/prod/path/produccion* ]]; then
if [ -z $shouldChangeProfile ]; then
echo -e "\033]50;SetProfile=production\a"
fi
shouldChangeProfile=1
elif [ "$shouldChangeProfile" == "1" ]; then
echo -e "\033]50;SetProfile=bash\a"
unset shouldChangeProfile
fi
}
fi
Inside the precmd
function is where I change the profile according to my needs.
shouldChangeProfile
prevents unnecessary profile changes.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a apple.stackexchange