• Codespaces
  • bash

How to configure aliases in GitHub Codespaces

November 04, 2021 • 1 min read

I will show you two ways you can configure aliases in GitHub Codespaces

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.

Codespaces .bashrc

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 like
3
# ~/.bash_aliases, instead of adding them here directly.
4
# See /usr/share/doc/bash-doc/examples in the bash-doc package.
5
6
if [ -f ~/.bash_aliases ]; then
7
. ~/.bash_aliases
8
fi

Method 1: Configure with bash file

So in order to configure the aliases you need to:

  1. Create a .bash_aliases file in the codespaces home directory (~/.bash_aliases)
  2. Add your aliases there
  3. Start a new terminal session which will load new aliases

Method 2: Configure your aliases with dotfiles repo

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:

  1. Create a new repo online through GitHub

    (You can also do this locally and push the changes)

  2. Add your .bash_aliases file in the repo.
  3. Push your changes
  4. Configure your GitHub Codespace settings so they load the dotfiles in each Codespace you create.

Now when you start a new Codespace instance, your aliases will be available.

Conclusion

I hope this was helpful and let me know if you have any questions.