DEV Community

Trevor
Trevor

Posted on

DRY: Reducing Redundancy in Zsh with Aliases

We're Not Lazy, We're Efficient

One common through-line that I have noticed when reading other blogs about coding is how often it is mentioned that people who code are lazy. When thinking about software engineers, I do not think that I ever thought to myself, "wow, this person really likes to slack-off." From my perspective, I have always thought that people who write code are incredibly efficient -- or, at the very least, their goal is to be efficient. One of the most important principals of writing code is DRY. Don't repeat yourself. It is a fairly easy concept to grasp, but it is important enough that is requires some sort of explanation when talking about it. Let's say that I am writing some code and I find that I am manually writing a block of codes multiple times.

const exampleOne = (a, b) => {
  addMe = a + b
  subtractMe = a - b
  divideMe = a / b
  multiplyMe = a * b
}

const exampleTwo = (a, b) => {
  addMe = a + b
  subtractMe = a - b
  divideMe = a / b
  multiplyMe = a * b
}
Enter fullscreen mode Exit fullscreen mode

As shown in the block above, the code is redundant and I am making my life harder by repeating myself. Albeit not extremely harder with this specific example, but harder nonetheless. So, how would I go about correcting this redundancy? The answer comes in the form of a helper function.

const exampleOne = (callback, valOne, valTwo) => {
 callback(valOne, valTwo)
}

const exampleTwo = (callback, valOne, valTwo) => {
  callback(valOne, valTwo)
}
const math = (a, b) => {
  addMe = console.log(a + b)
  subtractMe = console.log(a - b)
  divideMe = console.log(a / b)
  multiplyMe = console.log(a * b)
}

exampleOne(math, 1, 2)
Enter fullscreen mode Exit fullscreen mode

Now, anytime I need to make some basic computations I can just use my helper function as opposed to writing the same lines of code over and over again (is this laziness, or efficiency?). You might be thinking, well, you only had to rewrite that code block one time -- and you'd be right in this particular instance. But, imagine that I had one hundred functions that all required these four lines of code...you get the point. Now, let's apply that to zsh.

Zsh Aliasing

First off, I should start by defining what an alias is in this context. An alias is a shortened form of a longer command. Simple, right?

As previously mentioned, when writing code we should always be thinking about how we can reduce redundancy. However, one thing that I do not see mentioned enough is how we can reduce that same redundancy when we are working within our terminals. For example, as a student at Flatiron I am writing the same three lines of text over and over again.

git add .
git commit -m "Completed Assignment"
git push
Enter fullscreen mode Exit fullscreen mode

I recognized this as a pattern that I was writing on a daily basis, multiple times per day. It was not until recently that it begun to grate on me because there had to be a way that I could condense this in the same way I condensed my code above using a helper function.

Luckily, through aliasing, Zsh provides a way for us to create these helper functions to reduce this redundancy.

How to Create an Alias

In order to create an alias, the first thing you are going to need to do is open your terminal and run open ~/.zshrc.
Within this file, there should already be an area sectioned off for aliasing. If there is not an area already sectioned off for some reason, it is best practice to write all of your aliases at the bottom of the file as a way of organizing and separating information. Now, let's look at how a an alias is constructed:

alias <custom-alias>=<command>

#OR

<alias-name>() {
    //content here
}
Enter fullscreen mode Exit fullscreen mode

To break this down, let's say that you wanted to make your own alias function that prints out an encouraging message anytime you are feeling a little anxious. In your zshrc file, you could write the following function:

helpMe() {
echo "Everything is okay. You are a bright and glittering star and you are loved more than you know. You might feel overwhelmed now, but no feeling is final. Remember that." 
}
Enter fullscreen mode Exit fullscreen mode

And now, any time you are working in your terminal you can run helpMe to print the text in your alias function to your terminal.

Practical Examples

As mentioned previously, I found myself typing the same three lines of code over and over and over again. However, with aliasing, this is a simple fix. Adding the following alias function has made me more efficient -- not lazy.

gacp() {
git add .
git commit -m "Completed Assigment"
git push
}
Enter fullscreen mode Exit fullscreen mode

With this function, I will only have to type four characters and hit enter once. I don't know about you, but that is amazing.

Although this alias function is incredibly helpful for my coding experience, it may not be general enough to help you. However, the following may be:

// Clones down a repo and cds directly into the repo when you run gclonecd <the_cloned_repo>
gclonecd() {
  git_dir="$(basename "$1" .git)"
  git_dir_resolved=${2:-$git_dir}
  git clone "$@" && cd "$git_dir_resolved";
}

//Create a branch with your name
alisn gb="git branch {yourNameHere}"

//clear your terminal
alias c="clear"

//Fairly straight forward useful command for apps containing nodes
alias nis="npm install && npm start"

//
Enter fullscreen mode Exit fullscreen mode

As you write more code in the terminal, the more you will pay attention to what lines of code you are writing most often. At that point, it is your job as a coder to be efficient and create aliases for those lines of code.

Top comments (0)