How To Define Command Line Aliases On Any Operating System

Ads by Google

mac terminal   How To Define Command Line Aliases On Any Operating SystemWe’ve talked about, and recommended getting to grips with your computer’s command line terminal numerous times in the past. Tina wrote a good primer for Windows users with A Beginner’s Guide To The Windows Command Line.¬†Abraham provided a similar service for Linux users, with¬†An Introduction to the Linux Command Line. In a vast array of articles, MakeUseOf has explored other uses of the command line, showing that the above primers only begin to scratch the surface.

In any case, the general consensus appears to be that, although seemingly complicated, the command line allows you to perform some simple, everyday tasks much faster than you would be able to using graphical user interfaces (once you get the appropriate syntax down). Better yet, some tasks are not as easily performed using regular graphical user interface applications – powerful operating system features hiding in plain sight, as evidenced by Ryan’s¬†show of surprising uses of the Windows command line.

Alas, a lot (if not most) of the command line’s superior speed goes to waste if the proper command flags happen to slip your mind, or if the commands themselves get so thoroughly complicated you fail to see head from tail. Suddenly, your advanced workflow devolves into a combination of Googling and trial and error.

Command Line Aliases

Some people keep a list handy of their most visited commands, but that’s still far off from a lasting solution. Instead, we turn to an inherent feature of command line shells – aliases.¬†Command line aliases are an essential part of working with the command line interface, and once you start using them, you’ll wonder how you ever coped without it.

Ads by Google

As the name implies, the command line allows you to define aliases, or alternative syntax in the scope of your operating system’s command line shell. Once defined, any occurrence of the alias will be swapped out for the appropriate commands. This allows you to bind a command line call, perhaps with some of the flags already set, to an easily remembered alias. Henceforth, instead of jumbling flags or trying to remember a discombobulated series of commands, you’ll simply use the appropriate alias.

knot alias   How To Define Command Line Aliases On Any Operating System

This begs the following questions: should you use command line aliases? And if so, when is it called for? If you’re not a complete stranger to the command line, the answer to the first question is a definite yes. As for the second question, any commands you find yourself using repeatedly are good candidates. After all, it takes but a little time to set up and will save you a lot of time in the long run.

The exact process of defining a command line alias depends on your operating system, although there are definite similarities. We’ll review them case by case.

Mac OS X

Defining a temporary alias is very simple. Just use the following command:

alias name=’command

Where¬†name is the alias keyword your want to specify and¬†command is the command you’re trying to bind to it. For example, say I wanted a simpler way to browse to the Downloads folder on my Mac, I could define the following alias:

alias down=’cd ~/Downloads’

Now, running ‘down’ makes my Downloads folder the current directory. The main problem with this approach is that it’s only temporary; the aliases are voided when you close the Terminal. However, there is an easy way to define aliases in a more permanent way. We just have to add the alias commands as described above to one of the initialisation files of the Terminal. This way, the aliases are acquired every time you fire up the Terminal.

Create and/or edit a .bash_profile file in your home directory with the following command:

nano ~/.bash_profile

Now you’ll be able to enter or revise the aliases that you want on your system permanently. When you’re done, hit CTRL+X to exit. You’ll have to restart the Terminal or create a new window before you’ll be able to use those aliases.

Ubuntu

Ubuntu’s command line shell is very similar to that of Mac OS X. It shouldn’t be surprising that the alias command is handled similarly. As described above, you can define a temporary alias by running the following command:

alias name=’command

Where¬†name is the alias keyword and¬†command is the associated command. If you want to make the alias persistent across sessions, you’ll have to add the alias commands as specified above to one of the various initialisation files. I recommend adding it to .bash_aliases. You can create (if it doesn’t exist yet) or edit the file in nano using the following command:

nano ~/.bash_aliases

After which you’ll enter your desired command line aliases. When you’re done, hit CTRL+X to close the file. You’ll have to restart the Terminal or create a new window for your changes to take effect.

Windows

The Windows shell is a very different animal than the Mac OS X or Ubuntu shell, and creating aliases is even more dissimilar. Actually, Windows doesn’t even support the alias command, but we can emulate it by using the DOSKEY command.

DOSKEY name=’command

The above command creates a temporary alias, meaning it will last you until you close the session by closing the command line window. Defining a permanent alias is slightly more complicated than it is using a UNIX shell.

Create a new batch file (.cmd) with your DOSKEY commands in the Application Data folder, which is a hidden folder in your home folder. Run the ‘regedit’ command to fire up the registry editor and add the following data:

  • Key: HKCU\SOFTWARE\Microsoft\Command Processor
  • Value: AutoRun
  • Type: REG_EXPAND_SZ
  • Data: “%AppData%\aliases.cmd”

Credit goes to grawity at SuperUser for providing these details. Please note that the Windows registry is a highly sensitive area, which you shouldn’t muck around in. Only perform edits on the registry if you know what you’re doing.

Now that you know how to define a command alias, how will you be using it? Let us know in the comments section below the article!

Image credit: Shutterstock

Ads by Google
Check out more about:

8 Comments - Write a Comment

Reply

Muo TechGuy

This is pretty cool, but can it only be used for a command, or could I make an alias for say /var/www/vhosts/whatever -> website and then just use website in other commands? like cd website?

Simon Slangen

No, not as far as I know. Aliases are used to abbreviate commands, so you’d have to use something like

alias website=’cd /var/www/vhosts/whatever’

You could force something here with a script of your own (pseudocode), but it’s probably not worth the hassle.

cd -> myOwnCd{
switch(argument){
case website: argument = /var/www/vhosts/whatever; break;
case etc: argument = /etc/whatever; break;
}
builtin cd argument
}

Reply

dragonmouth

I know Ubuntu is the most popular distro. However, it is not the be-all and end-all of Linux. Your heading for the “alias” command paragraph should be LINUX, not Ubuntu.

ALIAS command works with all Linux shells (bash, csh, zsh, and others) and all Linux distros, not just Ubuntu.

Reply

Guy McDowell

This is great but I must add that if anyone is going to do this…
FOR THE LOVE OF ALL THAT’S GOOD – DOCUMENT IT
That should go without saying, but most things go without documenting.

Reply

Noman Fayez

except server without GUI.. commands line is must… otherwise GUI is far better than command line …. because commands are not user friendly……

Garris Rago

You’re right that gui is overall more user friendly, but I think this article tries to explain how you don’t always need to rely on your gui as some tasks can be easily completed using the command line much faster than using features of the gui

Guy McDowell

Exactly. And if you can create an alias for a complicated set of command line commands, it makes those tasks even faster. Pop open CMD, type a line, done.
Plus, sometimes you can do things in command line that you can’t do via gui. Sometimes you can kill a process via command line when the gui won’t let you.

Juan Pedro Secondo

Try to make a normal copy of a bunch of files in windows and compare it to the XCOPY in the command line and you’ll notice the difference in speed and options. The number of options are overwhelming but after a couple of runs you can easily remember them.

Your comment