How to Define Command Line Aliases on Any Operating System
We’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 . 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.
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.
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:
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:
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’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:
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:
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.
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.
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