9

I'm looking for a way to change the default directory of cd, and I'm wondering if this is possible. I tried adding

alias "cd=cd ~/Documents/Github" 

to .bashrc, but this obviously doesn't work because it breaks the cd command so that you cannot use cd for anything but going to that directory. Is there some export that I could put in .bashrc to do this? I do not want to change my home directory, just the default directory cd goes to. I ask because I regularly use cd to change to my default directory that I am programming in and would like to not have to type cd ~/workspace or cd and then cd workspace every time I want to change to the directory ~/workspace.

Jay
  • 998
  • 1
  • 10
  • 22

3 Answers3

17

Here's a simpler alternative using an alias:

alias cd='HOME=~/Documents/Github cd'
  • While this redefines $HOME, it does so ONLY for the cd command, so should be safe(*).
  • This also obviates the need for any custom parameter pre-parsing.

If you place this in ~/.bashrc, you should get the desired behavior.

Note that, by default, this alias will NOT be in effect in scripts (non-interactive shells), as alias expansion is by default disabled there (use shopt -s expand_aliases to explicitly enable).


(*) @chepner points out one restriction: with the alias in place you won't be able to do HOME=/somewhere/else cd, i.e., you won't be able to redefine (override) $HOME again, ad-hoc. As he further states, though, it's hard to imagine why anyone would want to do that.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • +1 Other than not being able to use the alias with `HOME=/somewhere/else cd` (and seriously, why would you?), this is much simpler than my shell function. – chepner Jun 04 '14 at 14:08
  • +1 A couple of the other answers worked as well, but I liked this one the best. Thanks! – Jay Jun 04 '14 at 14:08
  • 1
    My pleasure. @chepner: Thanks for that: for the sake of completeness I've added it to the answer. – mklement0 Jun 04 '14 at 14:31
  • 1
    As to why someone might want to... I have projects and I usually work on a particular project. I thought it might be nice if I had a simply way to cd back to the top directory of the project. So, when I start working on a project I could do PROJECT=`pwd` and then edit the cd function below to default to $PROJECT if it is set. – pedz Apr 22 '17 at 13:53
4

You can shadow the builtin cd command with a shell function. Add the following to your .bashrc file.

cd () {
    if [ $# = 0 ]; then
        builtin cd ~/Documents/Github
    else
        builtin cd "$@"
    fi
}

This isn't perfect, as it assumes you will never call the function as

* cd -L
* cd -P
* cd -LP
* etc

(that is, using one or more of the supported options without an explicit directory.)


UPDATE

This might be a little more comprehensive, but I haven't tested it.

cd () {
    local -a args
    while getopts LP opt "$@"; do
        case $opt in
          -*) args+=( "$opt" ) ;;
        esac
    done
    shift $((OPTIND-1))
    # Assume at most one directory argument, since
    # that is all cd uses
    args+=( "${1:-~/Documents/Github}" )

    builtin cd "${args[@]}"
}
chepner
  • 497,756
  • 71
  • 530
  • 681
  • 2
    Yes, `~/.bashrc` would be the appropriate place for this; that file is sourced by each *interactive* shell immediately after starting. (Note that any scripts you write will continue to use the real `cd`, not this function.) – chepner Jun 04 '14 at 14:01
1

this is default home location

$ pwd
/home/######

now you can reset it like this

$ export HOME=/tmp
$ cd
$ pwd
/tmp

now it's up to you where to put the new $HOME definition - .bashrc, or whatever

Pavel
  • 7,436
  • 2
  • 29
  • 42
  • 3
    Isn't that a little dangerous - what if other scripts use `$HOME`? – Flash Jun 04 '14 at 13:53
  • @Andrew That was my concern as well. I don't really want to change the home directory, just the functionality of cd. – Jay Jun 04 '14 at 13:54
  • 2
    Changing `HOME` is a bad idea. Most programs will look in `$HOME` for their configuration files; for example, `vim` looks for `$HOME/.vimrc` on startup. – chepner Jun 04 '14 at 14:04
  • I did it a couple of times when the homes were mounted in some unusual location, so I guess it's fine in some cases. – Pavel Jun 04 '14 at 14:05