4

I find myself constantly typing (using tab, of course) absurd paths like

cd path/to/the/thing\ that\ I\ need/python/proj/eraseme

Aside from doing an ln -s (or some other type of ln?), is there any other way to get around faster? Also, if the solution is to use ln, is there some standard way/place to put the links so a not to clutter my ~ directory?

I'm not asking for shortcuts only: any solution that helps with the problem of "how to get around" would help.

1

8 Answers 8

11

In bash, the $CDPATH environment variable contains a list of paths to be searched for directories when using cd.

CDPATH=".:~/long path to my projects"
cd eraseme
7
  • 1
    wow. that could be the coolest thing I've seen yet. I'll have to give it a spin. thanks! May 16, 2010 at 19:50
  • I think that's probably way more likely to cause confusion than an alias.
    – Stephen
    May 16, 2010 at 20:21
  • 2
    Depends on whether you can get used to it. I mean, since it's something you do for yourself and not something you impose on other people, there's no harm in trying.
    – David Z
    May 16, 2010 at 23:42
  • @Stephen: the reason I marked this as best answer is because if you combine this with a small script -- addcd, let's say -- it could be very powerful and you could avoid confusion entirely. Or you could add just one other path where you put your lns. This is really useful, in fact. May 17, 2010 at 8:06
  • @Ignacio Vazques-Abrams, how can I get my autocomplete (tabbing) to recognize the newly added paths as well? Without that this doesn't get me very far. May 17, 2010 at 9:14
9

Some people use aliases for this purpose. Try this for example:

alias cderaseme='cd /home/user/whatever/path/to/the/thing\ that\ I\ need/python/proj/eraseme'

Now you can type cderaseme every time you want to go to that directory. The alias will only be valid for the current terminal session, so put it in your .bashrc to keep it.

1
6

Some simple ideas:

  • Use tab completion.
  • Use cd to go back to your home directory. Use cd ~/<dir> to go a directory relative to your home directory.
  • Use cd - to go back to the last directory. I.e. with this command you can switch back and forth between two directories.
  • Use pushd <dir> to go to a directory and remember to previous location on the directory stack. Then use popd to go back (in the directory stack) to the previous location.
  • Use relative pathes, i.e. cd ../<dir>, instead of absolute paths.
  • Use cd !$ to go to the directory mentioned in the last argument of the previous command (depends on your shell). Example: mkdir /tmp/dir followed by cd !$ and you're current directory is /tmp/dir.
2
  • 2
    You can also use globbing while typing out the paths. Asterisk (*) is particularly useful -- like using a tab complete without having to pick alternatives (as long as something further down the path makes it unambiguous).
    – mpez0
    May 17, 2010 at 1:13
  • @mpez0 before reading your comment, I tried that out today. Didn't know that it works with multiple paths, it's actually very cool. May 17, 2010 at 9:17
4

zsh has slightly better completion--you can just type few characters of each directory and press tab to get all of them expanded:

$ cd p/t/t/t/p/p/er<tab>

Also there are some utilities that can remember where do you often go and try guessing, or just behave like a smarter version of cd, f.e. cdargs, wcd, apparix, kcd... never used them, I always just used zsh completion.

2
  • interesting. I'll have to study up on why I don't use zsh (or else switch). May 16, 2010 at 23:35
  • Even on bash, though, I can do cd /d*/j*/g* and if it's unique it'll get there. Not bad for a start. May 17, 2010 at 9:15
3

I usually create a shell script called something like 'mycd', which I can pass parameters to. Something like this:

# Shell script to CD into various locations.
if   [ "$1" == "myhome"  ]  ; then cd ~;
elif [ "$1" == "mypref"  ]  ; then cd ~/Library/Preferences;
elif [ "$1" == "mylib"  ]   ; then cd ~/Library;
elif [ "$1" == "syslib" ]   ; then cd /System/Library;
elif [ "$1" == "--help" ]   ; then
  echo "Usage: $0 location, which can be one of"
  echo "myhome = My home dir."
  echo "mypref = My Preferences dir."
  echo "mylib  = My Libraries dir."
  echo "syslib = System library."
  echo "--help = Show this message."
else echo "$0: $1 not known.";
fi

And then in the alias file, put an entry like:

alias mycd='. /path/to/mycd'

Then I can just call it with something like mycd mylib and it will take me straight there.

Similar to the list of aliases mentioned above, but this collects them all in one place and it gives help text if I need to be reminded what places I've stored.

1
  • +1 I like the code, but the end result is a tiny bit clumsy. May 17, 2010 at 9:16
1

Try this:
a() { alias $1=cd\ $PWD; }
This creates a little function that makes it easy to create new aliases for switching directories.

1
  • What would be the best way to get these to persist, though? I can have a add commands to the end of a shell script, but I can't manage to affect the current shell environment ever. This problem, if you will: stackoverflow.com/questions/496702 May 17, 2010 at 9:35
1

Been goofing around with some of these answers today. What's interesting is some combination of globbing with using tab on the Bash shell. For instance:

cd /p*/t*/*need*/py*

and hit tab and if it's unique it expands, otherwise it gives you choices.

1

On Mac OS X you (using /bin/bash) can use all the typical terminal cd commands and cdpath information.

There is a nice trick though. You can drag a folder from the Finder into the terminal and the full path will be displayed. That makes it easier for one-off complicated file lengths to quickly and visually be entered in the terminal.

1
  • true. I use PathFinder (love/hate relationship) which does that, and also I added a thing to the Finder a while back to tell me the path. May 18, 2010 at 18:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .