Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Specifically I'm wondering if, while working on a project, if I do cd it can take me to the root of the project and if I'm anywhere else then I get the default behaviour.

share|improve this question
    
What do you mean by "project"? I the following alias for mercurial projects: alias cdroot='cd $(hg root)' –  lazy1 May 29 '12 at 23:47
    
You can use pushd instead of cd when you first leave the project root, then use popd to return when you're ready. –  Marty Fried May 30 '12 at 0:30
add comment

2 Answers

up vote 3 down vote accepted
function projectcd() {
  arg="$1"
  if [[ ! -z $arg ]] ; then
    cd $arg
  elif [[ $(pwd) == /home/user/project/root/* ]] ; then
    cd /home/user/project/root/
  else
    cd
  fi
}
alias cd=projectcd
share|improve this answer
    
You could simplify this a bit and avoid the alias by just calling the function cd. If you do so, you can refer to the built in cd as builtin cd. And you'll have to in the function definition. –  Scott Severance May 30 '12 at 10:31
    
@ScottSeverance Where I was headed was to make projectcd take two parameters. $1 would be the project root directory and $2 the optional argument from cd. Then the alias would set the project root directory (and could be changed easily) without needing to modify the function. Nevertheless, as written, I agree with you. –  walrii May 30 '12 at 16:21
add comment

Use CDPATH to define the base directory for cd command

[ramesh@dev-db ~]# pwd
/home/ramesh

[ramesh@dev-db ~]# cd mail
-bash: cd: mail: No such file or directory
[Note: This is looking for mail directory under current directory]

[ramesh@dev-db ~]# export CDPATH=/etc
[ramesh@dev-db ~]# cd mail
[Note: This is looking for mail under /etc and not under current directory]

[ramesh@dev-db /etc/mail]# pwd
/etc/mail
share|improve this answer
    
That's cool, but if i do just cd it still sends me to the home directory –  geermc4 May 29 '12 at 23:34
1  
@geermc4 You can change your HOME env var. –  kev May 29 '12 at 23:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.