Create an alias to save typing long commands

Several tips in this book—such as Tip 208, on page 242, which explains how to view images in a folder as a slideshow—involve typing a chain of commands at the prompt. If your memory is as bad as mine you may find it hard to recall the precise order of commands (or even the command itself).

The solution is to create a bash alias. This lets you create a homemade single-word command that, when typed into a terminal window or virtual console, invokes another command, or a stream of commands, if need be.

Let's take as an example the aforementioned tip—Tip 208, on page 242. The command needed to view all the images in a folder as a slideshow is eog -f *.{jpg,tif,bmp,gif,png}. It would be nicer, and easier, to just switch to the folder and type slideshow. To make this possible, open your .bashrc file in Gedit (type gedit ~/.bashrc into a terminal window) and add the following new line at the bottom:

alias slideshow= "eog -f *.{jpg,tif,bmp,gif,png}"

In other words, the new command you want to create comes first, after which you list the command (in quotation marks because it includes spaces).

Open a new terminal window to see if your new command works by typing slideshow in a folder full of images.

You can have as many aliases as you want listed in the .bashrc file. Just type each on a new line. Before creating a new alias, ensure that the command you intend to use isn't already in use—a surprising amount of seemingly innocuous words are already in use as commands. This can be done by simply typing whereis followed by the command. To check to see if slideshow is in use, I'd type whereis slideshow. If I received back a listing of a folder then I'd know the command is in use. If I see just the command with nothing after it then I know it's not in use.

If all you want to do is create personalized shortcuts to already-installed applications, see Tip 239, on page 281.

Was this article helpful?

0 0

Post a comment