03. Aliasing alias, unalias

Now for one of the most useful commands! With aliasing, we can store a series of commands into a single variable. This is especially useful for repetitive tasks.

Executing a series of commands with alias

Let's say we're feeling lazy and want a way to navigate to another directory, as well as open a file within that directory.

Rather than using cd to move to the folder, then opening with vim, we can shorten these two commands into one through aliasing.

Before we choose a variable name to alias, we must make sure that the command is not already taken. Let's first check if shortcut has any function.

$ type shortcut
-bash: type: shortcut: not found

Great! Now let's now attach our commands to this variable.

We'll want to use absolute pathnames so that we can navigate to this directory no matter where we are on the file system.

We first type out the first command, then use a semicolon (;) before starting the second command. Our result will look something like this:

$ alias shortcut='cd /Users/JohnDoe/CodeSnippetsAcademy/CommandLine/Basic-Commands; vim testFile.txt'

Great! Now all we have to do is type shortcut to execute those two commands. Note that we could shorten 'shortcut' to two letters if we wanted.

Edit files at lightning speed with Vim!

Vim is a light-weight text editor that enables you to edit files at blazing speed.

If you want to learn how to use Vim, check out our Vim tutorials!

This shortcut will exist in your current session, but will disappear after you close your terminal.

Making an alias command persist

To make an alias last every time you boot up your terminal, place it in your ~/.bashrc. Your .bashrc within your home directory (~) runs every time you start a session, if it's configured properly in your .bash_profile.

To open your .bashrc with vim, use:

$ vim ~/.bashrc

Now every time you start a new session, the shell runs .bashrc, so it'll be available every time.

Unaliasing

To unalias an alias, use the unalias command.

$ unalias shortcut

Checking your set aliases

To check the aliases you have set, simply use the alias command.

$ alias
# List of all set aliases

Aliasing is a very powerful and can save you loads of time. Make sure to use it extensively in your workflow!

Take your Linux skills to the next level!

Linux for Beginners

Take your Linux skills to the next level! Try Linux & UNIX

Linux for Beginners doesn't make any assumptions about your background or knowledge of Linux. You need no prior knowledge to benefit from this book. You will be guided step by step using a logical and systematic approach. As new concepts, commands, or jargon are encountered they are explained in plain language, making it easy for anyone to understand.

$ Check price
24.9924.99Amazon 4.5 logo(101+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Prism Glasses

Aching back from coding all day? Try Back Problems

Ever feel achy from sitting crunched up on your computer table? Try lying down with these optical glasses that allow you to work on your laptop while lying flat on your back. This is the perfect solution with those with limited mobility or those who wish to prevent neck cramps and back strains.

$ Check price
4.454.45Amazon 4 logo(128+ reviews)

More Back Problems resources

Ad