An Intro to Custom Terminal Aliases and Functions

Putting aliases in your bash profile allow them to be used in any terminal instance you open.

We can also type our alias definitions in our terminal.

This only defines the alias for the current terminal session, but it’s a good trick for checking if your alias works.

I tend to do this to test if my syntax is correct first before putting my alias in the bash profile.

Try it out for yourself: in your current terminal window, type the following:alias desktop='cd ~/Desktop'Now try typing desktop and you should be navigated to your desktop.

Quick and easy test!Let’s get to implementing our aliases into the bash profile.

To access your bash profile, type the following into terminal:open ~/.

bash_profileYou should see the file open up in a text editor.

Be very careful not to delete anything here.

We’ll be adding our aliases all the way to the bottom of this file.

Let’s use some alias magic to make opening this bash profile quicker and easier in the future because open ~/.

bash_profile isn’t pretty.

Add the following line to the end of the bash profile:# My custom commandsalias bash_profile='open ~/.

bash_profile'alias desktop='cd ~/Desktop'It’s a good convention to comment your bash profile.

I added a comment to keep things clean and to list your future aliases under it.

You can name the alias whatever you choose, I just like keeping it bash_profile as it helps me remember it.

Save the bash profile file, close it, and quit and restart your terminal.

We need to essentially reboot terminal in order for the updated bash profile to be loaded.

Type alias and you should see your new alias to open the bash profile and to navigate to the Desktop!.Try running them marvel at your amazingness!Now you can add additional aliases for your frequently used commands.

FunctionsAliases aren’t powerful enough, you say?.That is true.

Enter functions.

Functions are used when you realize your alias isn’t dynamic.

For example, if you wanted to write an alias to mkdir and immediately cd into that new directory, it may look like alias mk_cd='mkdir new_dir && cd new_dir'.

Looks alright, but there’s a limitation — how do you dynamically name the directory you want to create?.Every time mk_cd runs, it’ll create a directory called new_dir.

Enter functions!Custom functions live in your bash profile, as well.

Luckily for us, we have an easy way to open our bash profile with the help of an alias.

Open your bash profile and type in the following custom function by your aliases under your # My custom commands comment all the way on the bottom:function new_dir() { mkdir $1 cd $1}This function called new_dir runs two commands: mkdir and cd, and a $1, which denotes the 1st variable, which in this case is the name of the directory we want to make.

In shell syntax, you’re not required to pass that variable in the function declaration, the function statements just know what to do.

Save your bash profile, reboot your terminal, and try running new_dir 'test_folder’.

You should see that you’re now in that test_folder.

Ta-da!Now for something even more powerful!.My git workflow tends to follow the following three commands:git add .

git commit -m 'commit message'git pushLet’s abstract this into a function!.I know that the only dynamic statement here is the commit message, so I’ll make that my variable:function gops() { git add .

git commit -m "$1" git push}I named this function gops for git operations.

The second statement includes "$1" which interpolates the argument passed in a string as that is what the commit message expects.

Now when I need to run my git workflow, I just type gops 'committing this awesome code', and it runs all three commands!.This function has been a huge time saver!.Try it out for yourself and tweak it as needed.

I’m secretly super proud of this function.

In my concluding statements of my first ever Medium post, I mentioned that I wanted to implement a way to run all three of these commands in succession pending a commit message.

It’s come full circle and I’ve finally learned how to implement that functionality.

It’s a great feeling to follow up on something you wanted to learn and see it fulfilled.

Bonus function!The following function is one that I’ve been thinking a lot about over the last few months:function gclcd() { git clone $1 && cd $(basename $1 .

git)}It allows me to clone a repository and immediately cd into it.

basename is an amazing UNIX program that’s been around for 40 years.

It takes the long repo SSH link and cuts it down to the name of the directory it creates.

Definitely add this one to your bash profile!ConclusionCustom alias and functions are super powerful topics to explore, and they’re limited by your imagination.

Try to think through some repetitive commands, static or dynamic, that you frequently run and create alias and functions for them.

Abstraction is a key concept in software development, so why not use it in your daily life, as well?There’s a lot more to learn about shell scripting.

My setup isn’t very ideal, and I’d like to properly set up a custom commands .

sh file that loads upon terminal launch.

This would keep the bash_profile clean and doesn’t risk messing anything up in it.

Hopefully this has served as some inspiration of how to apply programming concepts to your workflow.

It’s been an exciting adventure to get these aliases and functions working, and I hope it is the same for you.










. More details

Leave a Reply