8

This question already has an answer here:

I have git repos stored in different directories which have long paths /opt/too/long/path/to/type/every/time/git-repo and it's kind of difficult to remember plus to cd to the directory every time is a pain and tedious. (I know TAB helps but that gets tedious too.) So, my life would be much easier if I could store the pwd somewhere and later do something like cd thatgitrepo.

marked as duplicate by muru, Stephen Kitt, Anthon, mdpc, MelBurslan May 13 '16 at 22:43

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

15

Make yourself some symbolic links in your home directory:

ln -s /opt/really/long/obnoxiously/long/path/to/some/projectname

This command will make a link called projectname in your home directory into which you can cd, and everything will work perfectly.

15

z is a very nice "plugin" for bash or zsh (it's included in Oh My Zsh) which keeps track of directories you cd to, and allows you to quickly switch to directories using parts of their names, based on how frequently you use them.

So after a little while, it will know that

z git-repo

should cd to the git-repo directory you use most often. You can qualify that with part of the path, e.g.

z d git-repo

or even

z d git

I've found this to be more useful than aliases or even CDPATH because it learns on its own and adapts to my changing habits.

9

Once inside the directory, you do:

repo=`pwd`

and later you do

cd $repo

If you want to keep it for next time, you could do:

echo "export repo=`pwd`" >> ~/.profile
9

bash's CDPATH shell variable might be a convenient solution for you. A command such as cd foo searches for the subdirectory called foo inside the directories listed in CDPATH.

7

Just to mention another useful tool, there is the pushd builtin. With

$ pushd dirname

the current directory will be pushed on the directory stack (you can look at that stack with dirs) and the current directory will be changed to dirname. You can later change back to the latest (top-most in the stack) directory using the popd builtin command.

This should work at least in bash, zsh, csh and tcsh.

If you just want to change to the previous directory you have been in, cd - comes to help.

5

This is how I would do it; create an alias in ~/.bashrc (if using Bash):

alias thatgitrepo='cd /opt/a/b/c/d/e/f/g/git-repo'

And use source .bashrc to have the alias for the current shell.

0

Quoting this answer by ramesh:

The command you are looking for is pushd and popd.

You could view a practical working example of pushd and popd from here.

mkdir /tmp/dir1
mkdir /tmp/dir2
mkdir /tmp/dir3
mkdir /tmp/dir4

cd /tmp/dir1
pushd .

cd /tmp/dir2
pushd .

cd /tmp/dir3
pushd .

cd /tmp/dir4
pushd .

dirs
/tmp/dir4 /tmp/dir4 /tmp/dir3 /tmp/dir2 /tmp/dir1
  • You can also just do pushd /some/new/dir. – TMN May 13 '16 at 15:10
  • If using zsh, setopt autopushd makes every cd a pushd, which is fantastic in that you no longer have to remember to use a different command. – Xiong Chiamiov May 13 '16 at 20:09
0

For coding stuff I have always just used environment variables to store commonly used code directories.

In a bash file that gets executed do:

export PROJECTNAMEDIR=/opt/too/long/path/to/type/every/time/git-repo

Then you can do stuff like cd $PROJECTNAMEDIR or git checkout $PROJECTNAMEDIR and also variables will autocomplete with bash.

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