November 04, 2021 • 1 min read
I was recently using Codespaces for a project and found myself having to repeat a command in the terminal over and over again. It was a pain, but luckily the tutorial I was reading showed that their was an alias for that command😀. I went ahead and copied the alias and pasted it in the terminal like the tutorial showed.
1$ alias myCommand='cd ./to/some/long/path/bin/command'
Turns out that "worked", but the catch was that if I opened a new terminal session and ran myCommand
, it will error out and say myCommand
could not be found. It seems that the alias is not being persited properly.
I later found out that when a Codespace is booted up, the current user is codespace
. You can verify this by running whoami
in your Codespace terminal. The important information here is that there would be some bash file for the user that can be updated to add our aliases so they are persited.
You can find these files by going to the current user's directory.
1$ cd ~
Then if you run:
1$ ls -al
You will see all kinds of dotfiles that can be configured. For this specific case, the .bashrc
file seemed the most promising. When I opened it and looked through it, I found the following code, which basically told me what needed to be done.
1# Alias definitions.2# You may want to put all your additions into a separate file like3# ~/.bash_aliases, instead of adding them here directly.4# See /usr/share/doc/bash-doc/examples in the bash-doc package.56if [ -f ~/.bash_aliases ]; then7. ~/.bash_aliases8fi
So in order to configure the aliases you need to:
.bash_aliases
file in the codespaces home directory (~/.bash_aliases)One issue with Method 1 is that if you deleted your Codespace instance, your aliases will no longer be available. You can solve this by creating a dotfile repo and placing your .bash_aliases
file in there.
Here is what you need to do:
(You can also do this locally and push the changes)
.bash_aliases
file in the repo.Now when you start a new Codespace instance, your aliases will be available.
I hope this was helpful and let me know if you have any questions.