2024 Guide: Create Permanent ZSH Aliases on Linux & Mac Terminals
How To Make Alias With Terminal
How To Create An Alias In Zsh
Zsh aliases are stored in the users zshrc file within the home directory, which is prefixed with a period to signify that it is hidden. Thus we must edit that file to configure aliases.
nano ~/.zshrc
alias (aliasname)=”command”
For example, to make an alias called “fullinstallers” that lists all available complete installers for MacOS using the softwareupdate command with the -list-full-installers flag, the syntax would be:
alias fullinstallers=”softwareupdate –list-full-installers”
You could also use this alias if, for example, you have installed Homebrew and gcc on your Mac, but you prefer to run clang instead of gcc.
alias gcc=”gcc-13″
Another example, is to use an alias if you find yourself frequently typing in a lengthy command to connect to a particular ssh server, like so:
alias remoteshell=’ssh -p 123123 [email protected]’
Source /.zshrc
You can now use your new alias at the command-line. Just type in the command that you have linked with the alias and the alias will be run. The examples would be remoteshell, gcc, and fullinstallers.
You can use aliases for many things, such as using color in ls.
When We Open A New Terminal Window, Aliases Will Not Be Available
We can make aliases permanent by adding them to a file that is read at the start of a shell session. As we mentioned before, the most popular choices are /.bashrc or /.bash_profile. It is necessary to add aliases in any of the files.
It is a good practice to assign such names for the aliases, which are easy to remember. For future use, it is recommended to include a statement declaring a function that is related to bash-aliases.
We can make the.bashrc more modular by putting the aliases into a separate file. This is /.bash_aliases. We need to make sure that the code appears in the ~/.bashrc file:
Using The Alias Command To Create An Alias
Entering the following command into Terminal will create an alias for you.
alias
=
What is the best way to get in touch with you?
You can use any combination of characters to replace the command. is the command you would like to execute when you type the string.
If I use my previous example, then the command is:
alias l=’ls -ltra’
If you want to ensure that the alias is available every time you open Terminal without needing to type it in, you can enter it into your shell profile. In the next section, I’ll show you how.
Imagine you want to view a list all the aliases that you have created in your current session. If this is the case, type “alias” by itself.
=, and it will show you the list of them.
Creating Bash Profile Aliases For Quick Folder Access
Matthew Sedlacek
Follow us on Twitter
codeburst
—
Listen
Share
My computer is filled with many folders and files containing my various programming projects. Therefore, having to navigate through the many different folders to get to the project I’m looking for in the terminal can take more than a few lines of code. Luckily, there is a better way to navigate to folders and projects. Enter Bash Profile Aliases.
Before discussing Bash Profile Aliases, let’s take a step back and review some terminology. To use the Command-Line Interface, software engineers need to launch the terminal. “… will immediately launch a shell-program when the terminal is open. The shell program is what actually prompts you for input and returns the output. The shell most computers default to using is known as bash”(Flatiron School).
Bash has a Bash Profile that allows users to customize the program. Type la -a into your terminal to see your bash profiles and hidden folders on your computer. Type the following to open your Bash Profile and see it in your code-editor.
If you liked this, you might also be interested in
Is 256Gb Storage Enough For Macbook Air
It will look pretty empty if you’ve never used it. However, that will quickly change once you start adding your aliases. Let’s look at an example. Let’s say I want to add an alias for my projects folder. The first folder in the projects file path is Development, the next folder after development is code, and then the projects folder follows after that. To create an alias, I’d add this to my Bash Profile.
In the terminal I only need to enter projects to go directly to the project folder
How To Create Alias In Linux
Continue with the example from above.
You must note a few things:
You can check if a certain command is actually an alias with the type command. For example, in Ubuntu, ls is actually an alias to show you colorful output.
If you want to use the original command, without its aliased version, use single quotes around it.
The alias that you created is only temporary. You’ll lose your alias if you leave the shell. The alias must be made permanent.
How To Create Your Own Base Aliases
The process of creating bash-aliases is fairly simple for those who are new. First, open up the ~/.bashrc file in a text editor that is found in your home directory. Uncomment or add the following lines:
This tells it to load a .bash_aliases file, if it exists, so you can put all your aliases in it and make them easier to share and keep up with. Finally, create the ~/.bash_aliases file and add the following as your first alias:
If you were a fan of this, you might also be into
Mds Stores Process High Cpu
Save the file and type the following in the terminal:
How To Define A Linux Alias
It’s very simple to create a Linux Alias. You can either enter them at the command line as you’re working, or more likely, you’ll put them in one of your startup files, like your .bash_profile or .bashrc files, so they will be available every time you log in.
The l alias was created by adding the following commands to my.bash_profile:
The Linux alias is easy to understand:
How To Save Keystroke Aliases
This is very simple and straight forward, Let’s say you want to list all the contents of a folder instead of typing ls -latr, you want to use just ls by using the alias command:
The shell will save the keystrokes until you close it. Also, please pay attention to the spacing in the above command.
Tips:
-
What if you want to remove the alias in the present shell?
-
Just type unalias ls
Create Permanent Aliases
You will need to store the aliases in your shell profile. You could use:
The syntax to use in this case is the same as when we create a temporary one. This time, we’ll save the file. You can, for instance, open the bashrc with any editor you like.
Find a location in the document to store the aliases. A good place to add them is usually at the end of the file. You can add a note before the following:
Save the file when finished. This file will be uploaded automatically in your next session. You can use the file you have just written in your current session by running the command below:
Our aliases will be available in another document. You can define an alias permanently by following the directions in the bashrc. We will be able to have a separate file called bash_aliases to store them.
Each of the files we have created will be available to us when we next open a terminal. Use the command below to apply changes instantly.
Change Directories And View The Contents At The Same Time
It’s not necessary to “walk” through your computer’s tree of directories in a stop-and-start process.
It’s cheating because this isn’t an alias, but it gives you a good excuse to learn Bash functions. While aliases are great for quick substitutions, Bash allows you to add local functions in your .bashrc file (or a separate functions file that you load into .bashrc, just as you do your aliases file).
Create a file named /.bash_functions, and have your.bashrc read it.
In the functions file, add this code:
Load the function into your Bash session and then try it out:
You can be more creative with functions than you are with aliases. However, this flexibility also means that you must ensure your code is logical and performs as you want. Keep aliases simple but still useful. Use functions and custom shell scripts to modify Bash’s behavior.
For the record, there are some clever hacks to implement the cd and ls sequence as an alias, so if you’re patient enough, then the sky is the limit even using humble aliases.
Shell Aliases With Oh My Zsh Git Plugin
The list above is quite small, you may think. It is. This is because I also use the aliases created by the Oh My Zsh Git plugin.
Oh My Zsh is a framework for managing your zsh configuration. This tool allows you to customize your shell prompt. You can use a variety of themes and plugins, which offer aliases, in order to improve productivity.
This is an example of a plugin created:
If you don’t use Oh My Zsh, you can still copy the line above and paste it in the end of the ~/.bashrc or ~/.zshrc files – the shell alias will work fine.
I prefer shell aliases over Git aliases for 2 reasons:
Protip: If you are using the default shell without any customization, I do recommend checking out the Oh My Zsh project and Wes Bos’ Command Line Power User.
Global Alias
A global alias is an alias that can be applied to more than one command. You can use it more than once in a single command or use it anywhere within the command as it fits, except at the very beginning.
You can name your global alias however you want, but I prefer writing it capitalized as it distinguishes them from the normal aliases. Also, it will come handy when we talk about the global alias expansion later on.
Syntax
We use the flag -g to indicate a global name.
alias -g [custom-alias]=”[Command]”
You can also see our Example of a Good Way to Start
alias -g G=”| grep”
alias -g L=”| less”
If you appreciated this, you might also enjoy
Is 128Gb Enough For Iphone
alias -g GG=”google.com”
Terminal:
-
First alias used:
The apt-cache G search will translate into
apt-cache search vlc
2. Second alias (Use of):
cat readme.md L will translate to
cat readme.md | less
3. Use of third alias
$ ping GG will translate to
$ ping google.com