DEV Community

Cover image for Set Your First Alias Command in Mac Terminal(+ z command)
Sho-ki
Sho-ki

Posted on

Set Your First Alias Command in Mac Terminal(+ z command)

Setting aliases can help you type more efficiently and work faster. In this article, I will explain what aliases are and how to set them from the beginning. And finally, I will introduce some aliases that will help you in your coding life.

Purpose

  1. Understand what an alias is
  2. Set your first alias and use it
  3. Custom some other useful aliases

In the following, I will use the term "terminal" regardless of the type of shell, assuming a UNIX-like OS environment such as Mac or Linux.

1. What an alias is

As it turns out, aliases are like shortcut keys in the terminal.
Just like command+x to cut text and command+v to paste on Macbook, the terminal also has shortcuts.
For example, git branch is one of the most common commands to see what branch you are on. However, typing git branch every time is a bit tedious, isn't it? So, if you create an alias, you can use the git branch command just by typing gb.

2. Set your first alias and use it

Step1. Open your .zshrc file

First, open the terminal, and type

vim ~/.zshrc

Then, you may see something and you may not find any in the file.

Step2. Learn a bit about commands in Vim.

If you are knowledgeable about vim commands, you can skip this step.
Vim is one of the editors, just like Visual Studio Code, and by typing commands, you can change modes and perform operations such as editing and saving.

Followings are common commands in Vim editor:

i 
// Insert mode: Start editing from where the cursor is
Enter fullscreen mode Exit fullscreen mode

This is one of the most commonly used commands for changing.

esc
// Exit insert mode:Pressing Esc key will exit insert mode.
Enter fullscreen mode Exit fullscreen mode
dd
// Delete and copy the line containing the cursor
Enter fullscreen mode Exit fullscreen mode
$
// Move the cursor to the end of the line.
Enter fullscreen mode Exit fullscreen mode
p
// Paste the copied content after the cursor position
Enter fullscreen mode Exit fullscreen mode
:wq
// Save your edits and then close Vim. 
// If you don't save your edits, :q works.
Enter fullscreen mode Exit fullscreen mode

There are much more commands to edit in Vim, and you can search others if you are interested in.

Step3. Write an alias

For now, I will show you how you can write an alias for your terminal command.
Basically, you can register an alias in the file you just created with alias name = "command to execute".
For example, if you want to make an alias of clear command, which will clean your terminal, you just write alias c="clear" in insert mode. Then press the esc key, and enter :wq to save and close Vim.

Step4. Apply the change and use the alias

To apply your changes, you need the command source ~/.zshrc.
After applying the changes with the command, you can now use c to clear in your terminal.
Of course, you can also customize other commands to be as short as you like.

3. Custom some other useful aliases

Now that you know how to create aliases, let's list some aliases that will speed up your work.

alias g="g"
alias cm="commit -m"
alias gb="git branch"
alias gbD="git branch -D"
alias st="git status"
alias gp="git pull"
alias gs="git switch"
alias gpo="git push origin"

alias ..="cd ../"
alias ....="cd ../../"
alias ......="cd ../../../"

alias ni="npm i"
Enter fullscreen mode Exit fullscreen mode

Bonus Tier z command

z command is a tool that saves the history of directory names that were once moved by cd, and moves to the directory by simply typing part of the directory name.

Moves by cd will be saved in ~/.zshrc, and you will be able to move to any directory there at once.

For example, once you move the directory myFile, the directory name is stored in ~/.zshrc. After that, you can access to myFile directory wherever you are by z myFile. (Only z myF also works.)

Install z command

brew install z
Enter fullscreen mode Exit fullscreen mode
vim ~/.zshrc
Enter fullscreen mode Exit fullscreen mode

type i to change to insert mode

# z
. `brew --prefix`/etc/profile.d/z.sh
Enter fullscreen mode Exit fullscreen mode

Press esc key, and type :wq to save and quit.

Then, source ~/.zshrc to apply the change.

Now, you can use z command.
Image description

Summary

Setting aliases are very helpful for your work.
Why don't you make more aliases for commands frequently using to improve your work efficiency?

Discussion (0)