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 Answers
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.
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.
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
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.
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.
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.
Quoting this answer by ramesh:
The command you are looking for is
pushdandpopd.You could view a practical working example of
pushdandpopdfrom 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
-
1You can also just do
pushd /some/new/dir.TMN– TMN2016-05-13 15:10:23 +00:00Commented May 13, 2016 at 15:10 -
If using zsh,
setopt autopushdmakes everycdapushd, which is fantastic in that you no longer have to remember to use a different command.Xiong Chiamiov– Xiong Chiamiov2016-05-13 20:09:38 +00:00Commented May 13, 2016 at 20:09
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.
-
A great answer! Except... largely redundant with Julie Pelletier's already-existing answer.TOOGAM– TOOGAM2016-05-14 09:32:30 +00:00Commented May 14, 2016 at 9:32
-
Haha fair enough, didn't see that one, just upvoted now.pllee– pllee2016-05-14 16:19:30 +00:00Commented May 14, 2016 at 16:19
cdinto some well know directories.CDPATH. It has many advantages over symlinks.