Loading .bash_profile Aliases on Every Terminal
NB: Windows OS
~ = C:\Users\Username
Do you have your aliases saved in ~/.bash_profile
but can’t get them to work across all your terminals automatically and seamlessly?
The Scenario
I use cmd, git bash and other integrated terminals since I alternate between VSCode and Sublime Text often. With VSCode my aliases saved within the ~/.bash_profile
never works until I use source ~/.bash_profile
to load them in each time I launch the code editor. Those milliseconds are precious to us lazy fellas. It works as expected with the standalone git bash — my accomplice anytime I code with Sublime Text.
This inconsistency bugs me a lot and this made me forage for a silver bullet to put this issue to a deserved rest. There’s need to get this to work across all terminals — without any repetitive effort.
It appears ~/.bashrc
is the default on Windows from which all terminals are expected to load manually created commands. The annoying part is that checking within your C:\Users\Username directory the .bashrc file may not even exist. Implication is that git bash will load ~/.bash_profile
anyway and the integrated bash or terminals on VSCode will not.
The Fix
Create a ~/.bashrc
file and source ~/.bash_profile
in it:
if [ -f ~/.bash_profile ]; then
. ~/.bash_profile
fi
The script above means, if ~/.bash_profile
is not loaded by default then loading ~/.bashrc
the content of ~/.bash_profile
will get loaded. The assumption is that you have added as many aliases as is necessary for your daily activities added into the ~/.bash_profile
file. Below are some aliases I have within my ~/.bash_profile
:
# GENERAL
alias ..='cd ..'
alias c='clear'# GIT
alias gpor='git push origin main'
alias ga='git add .'
alias gs='git status'
alias gtcm='git commit -m'# PHP/LARAVEL
alias cda='composer dumpautoload'
alias memcomposer='COMPOSER_MEMORY_LIMIT=-1 composer'# PHP ARTISAN
alias pa='php artisan'
alias pad='php artisan dusk'
alias padf='php artisan dusk --filter'
alias padg='php artisan dusk --group'
alias paf='php artisan make:factory'
alias pam='php artisan migrate'
alias pamf='php artisan migrate:fresh'
alias pamm='php artisan make:model'
alias pamr='php artisan make:resource'
alias pams='php artisan migrate:fresh --seed'
alias parl='php artisan route:list'
alias pat='php artisan tinker'## PHPUNIT
alias pu='vendor/bin/phpunit'
alias puf='vendor/bin/phpunit --filter'
alias putf='vendor/bin/phpunit --testsuite Feature'
alias putu='vendor/bin/phpunit --testsuite Unit'# OTHERS
alias create-react-app='create-react-app.cmd'
alias express='express.cmd'
alias sqlite='winpty c:/sqlite/sqlite3.exe'
alias yarn='yarn.cmd'