Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Using Aliases

Introduction to Aliases

Aliases are shortcuts that allow you to create abbreviations for long or frequently used commands in the command line. They help in improving productivity by reducing the amount of typing required.

Creating Temporary Aliases

A temporary alias is valid only for the current terminal session. Once you close the terminal, the alias will be gone. You can create a temporary alias using the following syntax:

alias ll='ls -lah'

In this example, ll is the alias for the command ls -lah. To use this alias, simply type ll in the terminal.

ll

total 16
drwxr-xr-x  4 user group  128 Sep 12 16:00 .
drwxr-xr-x  5 user group  160 Sep 12 15:00 ..
-rw-r--r--  1 user group  123 Sep 12 16:00 file1.txt
-rw-r--r--  1 user group  456 Sep 12 16:00 file2.txt
                

Creating Permanent Aliases

To make an alias permanent, you need to add it to your shell's configuration file. For bash, this file is ~/.bashrc or ~/.bash_profile. For zsh, it's ~/.zshrc. Open the file in a text editor and add your alias at the end.

nano ~/.bashrc

Add the alias to the file:

alias ll='ls -lah'

Save the file and then source it to apply the changes:

source ~/.bashrc

Commonly Used Aliases

Here are some commonly used aliases that you might find useful:

alias gs='git status'

alias ga='git add'

alias gp='git push'

alias gd='git diff'

These aliases shorten common git commands, making your workflow more efficient.

Listing and Removing Aliases

To see all the aliases currently defined in your shell, use the following command:

alias

If you want to remove an alias, you can use the unalias command:

unalias ll

This will remove the ll alias from the current session.

Conclusion

Using aliases can significantly speed up your command line workflow by reducing the amount of typing required for frequently used commands. You can create both temporary and permanent aliases to suit your needs. Start by defining a few aliases for your most commonly used commands and see how much more productive you can be.