41

Sometimes when I want to change to the parent directory, I mistakenly type cd ,, instead of cd ... Is there a way I can make bash consider these statements to be the same? I never use commas in my commands. Ideally, I would like to alias ,, to ... When I tried this, it didn't work. Why didn't this work? How else can I approach this problem?

$ alias ,,=..
$ cd ,,
-bash: cd: ,,: No such file or directory
7
  • 6
    An alias is a program to run, not an argument to the program; so $ ,, would complain, as such: bash: ..: Is a directory. (In other words, it's not an executable.)
    – jpaugh
    Commented Apr 27, 2017 at 21:58
  • 5
    @jpaugh: A clearer way to say that is: alias expansion only happens on the first "word" of a command. So as you say, it only works for command names, not args. (And you couldn't even alias 'cd ,,'='cd ..'. Well, you could, but you'd have to run it as cd\ ,, to make it all one word.) Commented Apr 28, 2017 at 1:20
  • 5
    Consider zsh. It has global aliases, so alias -g ,,=.. and cd ,, works as you expect it to.
    – muru
    Commented Apr 28, 2017 at 5:13
  • 1
    Lateral thinking: find / -type d -exec ln -s .. {}/,, \;... ;-) Commented Apr 28, 2017 at 12:58
  • 2
    Imho: don't do this. Using aliases to cover up typographic errors is a bad practice. Those errors can end up in copy-pasted code or screencasts. It won't work in other hosts or accounts. And such modification (depending on how it's implemented) could cause other, unexpected, difficult to debug errors. My suggestion: if you mistype a command, simply type it again ;-).
    – jjmontes
    Commented May 1, 2017 at 14:11

5 Answers 5

44

ZSH:

If you'r using zsh alias -g ,,=".." is what you need, but this will cause ",," to be expanded everywhere, not only when used with cd.

From man zshbuiltins:

If the -g flag is present, define a global alias; global aliases are expanded even if they do not occur in command position.

BASH:

If restricted to bash (as the question is tagged with ), read the following:

This is a pure-alias solution, as requested, however this will do more than required possibly frustrating the user (see the warning at the end of my post).

Quoting man bash:

If the last character of the alias value is a blank, then the next command word following the alias is also checked for alias expansion.

Therefore it's enough to alias cd with an extra space (to allow expanding of its next argument) and then alias ,, to ... See that

alias cd='cd '
alias ,,='..'

gives exactly what you need.

Note that this is correct not only for bash (and its alias implementation), but all POSIX-compilant shells. Quoting an example from man 1p alias (the manual does not describe this feature explicitly, only through an example):

  1. Set up nohup so that it can deal with an argument that is itself an alias name:

           alias nohup="nohup "
    

Warning: As @PeterCordes writes in his comment, it will automatically cause other aliases to expand when written after cd. It may require you to write cd \grep if you want to change directory to one named grep but your grep is an alias for grep --color=auto. Without the backslash, cd will report "too many arguments" error (you can't cd to two directories at once)!.

2
  • 22
    That's a clever but nasty hack. I wouldn't want to do that; too easy for that to accidentally bite you. e.g. if you have an alias for a command you commonly run, and you want to cd into a directory of its source code. Or other collisions between your aliases and common directory names. Like cd dr would expand to cd disown -r for me, with this alias. Anyway, much better to override builtin cd with a function for stuff like this, IMO. Commented Apr 28, 2017 at 1:31
  • 8
    +1 for pure-alias solution (I learnt something). At the same time I agree with Peter's comment. Some kind of explicit warning should be added to your answer, I think. Commented Apr 28, 2017 at 4:16
59

Aliases aren't meant to do this, but you can create a function called cd that is a wrapper for the real cd. This one works for me! Just put it into your .bash_profile or your profile file of choice.

cd () { if [ "$1" = ",," ]; then builtin cd ..; else builtin cd "$@"; fi; }

Or, with comments and pretty formatting:

cd ()
{
  if [ "$1" = ",," ]; then  # if first argument is ",,"...
    builtin cd ..           # call the builtin cd with ".." instead...
  else
    builtin cd "$@"         # else call builtin cd with original arguments 
  fi
}

EDIT

Thanks @derobert for pointing out that if then else is better here than && ||. Also, I just realized (*facepalm*) that this implementation will only work if any non-path arguments (i.e. -L -P) are not the first argument. So be warned, if you have a bad habit of typing cd -L ,, this isn't going to help you. And that is non-trivial to handle correctly, I think.

9
  • 16
    @BenjiWiebe I think the "comments and pretty formatting" version would be better with an if-then-else.
    – zwol
    Commented Apr 27, 2017 at 19:42
  • Wow folks, lots of good ideas. I see you are using the edit feature to take care of the fixes needed. I didn't spend much time testing it. :)
    – BenjiWiebe
    Commented Apr 27, 2017 at 20:49
  • 1
    Alternatively, just overwrite $1 before executing the built-in: cd () { [ "$1" = ",," ] && set .. ; cd "$@"; } Commented Apr 28, 2017 at 7:42
  • 1
    @TobySpeight set .. "${@:2}", or you lose anything after the first argument.
    – chepner
    Commented Apr 28, 2017 at 11:26
  • 8
    Also, technically .. doesn't have to be the first argument; cd in bash can take a variety of options (-P, -L, -@, -e).
    – chepner
    Commented Apr 28, 2017 at 11:29
29

Aliases must be the first word of a command. Also, the alias must be substituted for a word therefore no spaces).

Bash Reference Manual: Aliases

Aliases allow a string to be substituted for a word when it is used as the first word of a simple command.

You could alias both .. and ,, to be cd ...

$ alias ..="cd .."
$ alias ,,="cd .."
$ cd /tmp && pwd
/tmp
$ ,, && pwd
/
3
  • Thats exactly what I do! :D these two dots/commas are much easier to type than cd ..
    – rav_kr
    Commented Apr 27, 2017 at 19:42
  • 10
    +1, that's not a bad idea at all. Another way to do it for .. in Bash is with shopt -s autocd which lets you change to any directory by just entering its name. Of course you still the alias for ,,
    – ilkkachu
    Commented Apr 27, 2017 at 19:52
  • Me too :-) Commented Apr 29, 2017 at 9:51
3

Another way to make .. change the directory is to enable Bash's autocd feature:

shopt -s autocd 

That'll automatically change the directory when you enter the name of one:

~$ /tmp
cd /tmp
/tmp$ 

You still have to make an alias for ,, but that can be done with just alias ,,=..

2

There's already an app a script for that: https://github.com/shyiko/commacd (no affiliation with Stanley Shuyiko).

commacd exports three commands: , (for jumping forward), ,, (backward) and ,,, (backward+forward):

All three of them try to match by prefix first. Only if no results are found, will they fallback to substring (fuzzy) matching

To answer your question the script aliases commas to three functions it defines:

alias ,=_commacd_forward
alias ,,=_commacd_backward
alias ,,,=_commacd_backward_forward

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .