8

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.

8 Answers8

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.

DopeGhoti
  • 76,081
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.

Stephen Kitt
  • 434,908
10

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.

egmont
  • 5,866
8

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.

Dubu
  • 3,723
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.

DopeGhoti
  • 76,081
Vombat
  • 12,884
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
gerrit
  • 3,487
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.

pllee
  • 101