DEV Community

Hamid Haghdoost
Hamid Haghdoost

Posted on

How to define custom alias in zsh

If you're looking to enhance your command usage in a more streamlined manner, incorporating custom aliases can be a highly effective approach. These aliases provide a convenient way to simplify and expedite your workflow.

To begin, navigate to the ~/.zshrc file. This file serves as the configuration file for the Zsh shell, allowing you to customize its behavior and add personalizations. You can access it by using the following command in your terminal:

vim ~/.zshrc
Enter fullscreen mode Exit fullscreen mode

Once you have the ~/.zshrc file open, you can proceed to define your custom aliases. Aliases allow you to create shortcuts for longer or frequently used commands. By assigning a shorter, more memorable name to a command, you can save time and effort in your day-to-day tasks.

Here's an example of how you can add aliases to your ~/.zshrc file:

alias ka="kubectl get all -o wide"
alias ks="kubectl get services -o wide"
alias kap="kubectl apply -f "
Enter fullscreen mode Exit fullscreen mode

In the above example, we've created three aliases: ka, ks, and kap. The ka alias invokes the command kubectl get all -o wide, while ks executes kubectl get services -o wide. Lastly, kap allows you to apply Kubernetes configuration files using the kubectl apply -f command.

Once you've defined your desired aliases, it's essential to inform Zsh to re-initialize itself with the updated ~/.zshrc file. This ensures that your new aliases come into effect. To achieve this, execute the following command in your terminal:

source ~/.zshrc
Enter fullscreen mode Exit fullscreen mode

By running the source command, Zsh reloads the configuration file, making your custom aliases immediately available for use.

Incorporating custom aliases into your workflow can significantly enhance your productivity and make your command-line experience more efficient. With these simplified shortcuts, you'll be able to execute complex commands with ease and speed, ultimately streamlining your development or administrative tasks.

Top comments (0)