Aliases are handy to make the workflow more efficient. It shortens the process of retyping repetitive commands in your terminal.
One of the scenarios we use here on our WordPress website is when running promotions.
When running a sale on our website, we need to edit four files: style.css, functions.php, our Plugin Manager server file, and a specific JS file.
Up until the point I created the alias, I needed to navigate into each of the file folders and open them individually.
The command that I used to open them all was:
open store/wp-content/plugins/pluginsforwp-server/pluginsforwp-server.php store/wp-content/themes/oceanwp-child-theme-master/functions.php store/wp-content/themes/oceanwp-child-theme-master/scripts/general-scripts.js store/wp-content/themes/oceanwp-child-theme-master/style.css
A process like so will waste valuable time. Good developers should aim to be more productive and simplify operations. Therefore, creating an alias is precisely what I needed to do.
Thus, I created the alias promofiles to open all four files.
In the next section, I will show you how to set an alias in Unix environments, such as macOS, Bash, Zsh, and Linux.
Temporary Alias
The first type is a temporary alias, valid only for the current session (until closing the shell) or turning off your computer.
First, you’ll need to open the terminal. Then, use the syntax below:
alias <newcommand>='<old command>’
For example, if we would like to use the command pull to pull our WordPress website from git, we can create an alias like so:
alias pull=‘git pull origin master’
Once we set it up, we can execute the alias using the pull syntax.
If you would like to persist with the newly created alias and use it again in the future, we will need to save it permanently.
Permanent Alias
The advantage of a permanent alias is the option to reuse the syntax in the future, even after restarting our system.
To save the alias on our system, we will need to add it to the .bash_profile or .zshrc files.
The following steps will show you how to create an alias for macOS:
-
Launch the terminal from the Utility folder.
-
Type
~/
to navigate to your home directory. -
Now, you’ll need to open the .bash_profile or the .zshrc file by entering
open .zshrc
(if you don’t have the file, create one first with thenano .zshrc
command and open it afterward). -
Scroll to the bottom of the .zshrc file and enter
alias <newcommand>='<old command>’
. For example:alias l='ls -lah’
. -
Save the file and exit it.
-
Run the alias in the terminal and verify it’s working.
That’s it. You just created an alias on your macOS that will make your developing job much quicker.
Frequently Asked Questions
Any UNIX environment: Bash, Zsh, Fish on macOS and Linux.
Since the release of macOS 10.6, Zsh has been the default shell (replaced the bash shell).
Yes, there are many great tutorials that you can find on Google. This is the one we followed in the past and found beneficial.
Related Articles
- Using WP-CLI to manage WordPress on the command line
- How to create a custom WP-CLI command
- Using git to manage your WordPress websites
- Running WordPress locally on your mac with Mamp
Conclusion
Alias is a great way to prevent retyping repetitive commands and is very useful in saving valuable time.
Leave us a comment and let us know if you have any questions about the process or need any other command.
Also, make sure to subscribe to our YouTube channel and like our page on Facebook.