How to permanently alias set up in Mac?

Jai Mahto 2 Mins 07/04/2024
How to permanently alias set up in Mac?

Aliases are a convenient way to create shortcuts for commands you frequently use in the Terminal on macOS. Instead of typing out long commands or paths repeatedly, you can create an alias to represent them. However, these aliases are typically temporary and are lost once you close the Terminal session. In this blog post, we'll explore how to create permanent aliases in macOS.

What is an Alias?

An alias is a custom shorthand for a command or a file path. It allows you to create your own commands or shortcuts for longer commands. For example, you can create an alias ll for the ls -l command to list files in long format.

Creating Temporary Aliases

Temporary aliases can be created directly in the Terminal using the alias command. Here's the general syntax:

alias alias_name='command'

For example, to create an alias ll for ls -l, you would type:

alias ll='ls -l'

However, these aliases are temporary and will be lost once you close the Terminal window.

Making Aliases Permanent

To make aliases permanent in macOS, you need to modify the shell configuration file. macOS uses the Bash shell by default, and the configuration file for Bash is ~/.bash_profile.

Here's how to make aliases permanent:

  1. Open Terminal on your Mac. You can find it in the Applications folder under Utilities, or you can use Spotlight Search (Cmd + Space and type "Terminal").

  2. Open your .bash_profile file using a text editor like nano or vim. You can do this by typing:

nano ~/.bash_profile
  1. Scroll to the bottom of the file and add your aliases in the following format:
alias alias_name='command'

For example:

alias ll='ls -l'
  1. After adding your aliases, save the file by pressing Ctrl + X, then Y to confirm changes, and finally Enter to save.

  2. To apply the changes, either restart Terminal or run the following command:

source ~/.bash_profile

Now, your aliases will be permanent and available every time you open a new Terminal session.

Conclusion

Creating permanent aliases in macOS can save you time and make your command-line experience more efficient. By modifying the .bash_profile file, you can ensure that your aliases are available whenever you open a new Terminal window. So go ahead, customize your shell environment with aliases and streamline your workflow!