Three ways to alias your way to upvotes!

April 18, 2011 by . 1 comments

By Mark Szymanski

At Super User we were recently asked the question

Any shortcut for “cd ..” ?

Is there any shortcut for “cd ..”?

Well, I answered it, thinking it was just another run-of-the-mill quick questions and answers that we get many of every day.  Little did I know, but things were going to take a turn in a slightly surreal direction… My answer, for some unfathomable reason, got an insane amount of upvotes. 50 to be precise, and still counting, and I am not exactly sure why.

The original asker of the question was looking for a shorter way to run the command cd .., which is used to change to the parent directory.  The answer I gave provided this simple shell alias:

$ alias ..="cd .."
which allows the user to use the .. command in place of cd ..

I didn’t really think anything of this answer and just though it was a simple answer that would help the person who asked the question. Little did I know that it would receive more than 50 upvotes. I’m also slightly surprised that the original poster selected my answer as the one to accept, when other answers (such as the one about a shopt trick) were, in my opinion, better than mine. Maybe the massive amount of upvotes is from how simple my answer was, and from the fact that it works and is probably the shortest command given.

What is Alias?

The alias command is used to substitute (or alias) one command for another. It works like this:

$ alias newcommand=shortcommand
After running this command in a shell, or putting it in a shell resource file such as ~/.bash_profile, you can type newcommand, and oldcommand will be run. This is often used to make commonly used commands easier to type. alias can be used on any OS (even Windows, if you use Cygwin) with the bash shell (it can probably be used with other shells as well, such as zsh). I use quite a few aliases myself, like piano aliased to pianobar. Or q aliased to exit. Using these aliases makes the commands shorter, therefore faster to type and making you more productive.

Here’s an example of how it can make you more productive:

MOAR PRODUCTIVE!

This screenshot shows an example of the command

$ git log --graph --pretty=oneline --abbrev-commit
That would take a long time to type, and you might not remember all the command line flags in order to make it work. So you’d have to figure out all the flags, which would take away from the time you could be using to actually get stuff done. To make this easier, you can use an alias. I have this command aliased to gll. There’s no funky command line flags to trip up on. Just a simple, three letter command. You just simply type these three letters, look at the git log shown, and get on with being productive!

You can also use aliases for parts of commands. For instance, say I want to use that gll alias, but I want to append the -5 switch to the command so it’ll only show me five commits. So I could just do:

$ gll -5
And it will run the command:
$ git log --graph --pretty=oneline --abbrev-commit -5
This can be used in any situation where you want to put arguments at the end of a long command using an alias.

Is there anything better than Alias?

There were a few other alternatives offered for that question, a few of which using aliases as well, but a few not. The one I found most interesting used shopt.

shopt is short for “Shell Options” and is (logically) used to set various options within the shell.  One of these options is “autocd”:

$ shopt -s autcd
The autocd option converts all directories into commands which, when run, change into that directory, for example:
$ /tmp/
Will expand to:
$ cd /tmp
Another interesting alternative I saw given was the use of the bind command to allow you to link a single keypress to a command:
$ bind '"eu":"cd ..C-m"'
Running this command (or of course putting it in a shell resource file, such as ~/.bash_profile) will let you press Control+U and it will automatically do the cd .. command.

So what does using aliases etc all equate to?

  • Fewer keystrokes means you type your commands faster
  • You don’t have to remember complex commands
  • Your keyboard will last longer.

So next time you find yourself typing a long command for the second or third time, why not consider aliasing it?

Filed under Question of the Week

One Comment

Subscribe to comments with RSS.

  • Ben Finney says:

    Along with not really understanding why the answer is so popular (it’s correct and helpful, sure, but hardly life-changing), I’m a little baffled that the question was so popular.

    Do people find themselves using ‘cd’ so very often that a shortcut is needed? We have the ability to talk about files in a different directory, folks. If you can specify the path to ‘cd’ there, and you’re not going to want to be there very long, why not specify the filesystem path of the file instead and stay where you are?

  • Comments have been closed for this post