What is an alias?
An alias is a feature of command line shells. They are a way to store command and parameters that you use frequently against a memorable name. Basically, they’re like a function or method.
If that didn’t make sense, let me give you an example. Say you were working in a project that had a really awkward command for running all of the tests. You want them to run in a particular way.
For instance, let’s pretend you have to type in the below to run the tests in multithreaded mode to make them go faster.
tester --run-tests --in-parallel --nicely-formatted --with-coverage ./src/tests/actual-tests
Depending on the setup of your terminal, you might have to manually type that in every time!
Would it not be more productive and more accurate to just type this?:
tpc being an abbreviation for tests parallel coverage. It might be even better to use something more readable like this:
How do I set up an alias?
- Open your terminal
- Open / make a bash profile
- Open / make a .alias file
- Link to the .alias file from the bash profile
- Add aliases
1. Open your terminal
On windows, open git bash if you have it installed.
If you are using macOS, open the app called Terminal or use iTerm 2 if you have it installed.
If you are using Ubuntu open the app called Terminal.
Alternatively, if you prefer, you can use the terminal built into your IDE.
2. Open / make a bash profile
If you prefer to manage files using your mouse, you can do this from your file manager. However, you will need to enable hidden files.
First we need to check if we already have one. Type in the following command
ls -lah ~
This will print out all of the files in your home directory.
Look for a file with one of these names. The files all start with a dot/period. These are called dot files and they are hidden by default.
If you don’t have one of these files don’t worry. Create a file called
.alias and open it in a text editor.
By the way, when I say bash profile, I mean your file with one of the names from the above list.
3. Make an alias file
Same as before, if you have a
.alias file in your home directory open it in an editor, otherwise create it and then open it.
4. Link to the .alias file from the bash profile
If you have a
.aliasfile already, this may be done for you.
You’ll need to find the path to your home directory. Your home directory is where these dot files normally live. It is also where your Documents, Desktop, Downloads (etc) folders are.
In your terminal, type in
You will see output similar to
In your bash profile, add the following line to the bottom, replacing the filepath with the one you found using pwd.
5. Add aliases
Now we can finally add some aliases. Let’s check it all worked by adding a hello world one to the
alias hello="echo 'hello, world!'"
Then you will need to open a new terminal, or type in the following command to reload the alias profile.
You should see the following output:
Easy right? Now you can add all of the long or repetitive commands you frequently use.
You don’t need to keep the
.alias file in your home directory. You could keep it in a folder and link to it there.
This folder could contain shell scripts and be a git repo so you can publish it on GitHub/GitLab/BitBucket for your team to use or for the next time you get a new laptop.
For me, I have a folder called bin/ in my home directory where I keep my .alias and some handy custom commands. One of which you will see below.
My current bash profile
I’ve modified this to remove some of the work stuff but I’ve kept it mostly intact. Please note that I’m using macOS and the notification one only works on Macs.
# display a notification that the tests are done alias testsdone="osascript -e 'display notification \"Tests have finished running have they passed?\" with title \"End to End Tests: DONE\"'" # git commands alias gs="git status" alias gc="git commit" # React JS shortcut alias reduxify="touch Actions.ts Reducer.ts Types.ts AsyncActions.ts" # Spring boot alias runlocal='mvn something:run -Dspring.profiles.active=environment' # frequently accessed directories alias api='cd /Users/myname /api-project' alias ui='cd /Users/myname/UUB-project' # tests alias e2e="cd ~/ebond-ui/; npm run e2e-with-mock-server; testsdone" alias testall="ui; npm run testall; testsdone" alias parallel="ui; npm run test-parallel; testsdone";
Notice that you can separate multiple commands in aliases with a semicolon. However, if the alias grows over 3 steps, you should create a function instead. This is very easy and it works in the same way as an alias.
I will be releasing a post soon about how I’ve used a shell function to extract the story id from the current branch in git to use in commit messages.