When working in the Terminal, I often have to change back and forth between several directories. The tcsh shell has built-in capabilities to help with this: 'push' and 'pop' can be used instead of 'cd' when changing directories. With 'push' and 'pop', you get a stack of directories that makes it a bit easier for going back and forth. But this is not so useful if you have a more random pattern of directory use: you go here, then you go somewhere else, ... and of course, you have to remember to use 'push' and 'pop' instead of 'cd'. Often you find yourself having to type in a long directory path (but filename completion via Tab helps a lot here). So what I find convenient is the following facility which allows you to give short mnemonic names to directories and then you can use these names of your own choosing with 'cd' .
Read the rest of the article for the how-to...
First I'll show you how to use it (via examples) and then I'll show you how to implement it via a few aliases.
Suppose you find yourself frequently going to the directory /Users/fred/Documents/Projects/Acme. When you are in that directory, you issue the command:
The above commands (save and show) are implemented via aliases and you can install them for your use as follows:
How this works is the 'save' command creates a shell variable with the name you supply and sets its value to the full path of the current directory. The variables are stored in the file ".dirs" in your home directory. It is a built-in feature of the tcsh shell that if you do 'cd foo', the 'cd' command checks if foo is a shell variable and if so, it evaluates the variable to find the directory it is supposed to go to (no $ is needed). Of course, if there is a sub-directory named "foo", it goes there directly, not bothering about any variables.
You can edit the ".dirs" file manually if you want - this is useful for updating paths and deleting directory short names which you no longer need. The 'sdirs' command is useful if you have set up a short name in one Terminal window and you want it to be available in another already-existing window. New windows automatically get all the short names by virtue of the line that sources the ".dirs" file.
I find the directory-path variables useful for other operations as well. For example, if I need to copy a bunch of files from the current directory to the directory which I had previously given the short name "acme", I can do it with the command:
Read the rest of the article for the how-to...
First I'll show you how to use it (via examples) and then I'll show you how to implement it via a few aliases.
Suppose you find yourself frequently going to the directory /Users/fred/Documents/Projects/Acme. When you are in that directory, you issue the command:
% save acmeThis assigns "acme" as your own private short name for the directory you are in. Then, later on, when you are in some other (far-away) directory, you can go back to the Acme directory by issuing the command:
% cd acmeAfter a while, you may have accumulated a number of these short names for directories and they will serve as a memory aid. Suppose you forget where the preferences files are kept and you know that you had saved a short name for this directory, but you forget what it was. You issue the command...
% show... and you will get a list of all the short names and their full-path equivalents. Note: the short names you supply via the 'save' command must be one word (no spaces or punctuation).
The above commands (save and show) are implemented via aliases and you can install them for your use as follows:
- Create an empty file called ".dirs" in your home directory via the following command:
% touch ~/.dirs
- Put the following lines in your aliases file (see previous hints on how to use aliases):
# The following aliases (save & show) are for saving frequently used directories
[NOTE: Enter the "save" alias as one line; it's been broken here for readability]
# You can save a directory using an abbreviation of your choosing. Eg. save ms
# You can subsequently move to one of the saved directories by using cd with
# the abbreviation you choose. Eg. cd ms (Note that no '$' is necessary.)
alias save 'sed "/set \!$/d" ~/.dirs > ~/.dirs1; \mv ~/.dirs1 ~/.dirs; echo
set \!* = \"`pwd`\" >> ~/.dirs; source ~/.dirs'
alias sdirs 'source ~/.dirs'
alias show 'cat ~/.dirs'
# source the ~/.dirs file so that we get the previously set up variables
if ( -f ~/.dirs ) source ~/.dirs
How this works is the 'save' command creates a shell variable with the name you supply and sets its value to the full path of the current directory. The variables are stored in the file ".dirs" in your home directory. It is a built-in feature of the tcsh shell that if you do 'cd foo', the 'cd' command checks if foo is a shell variable and if so, it evaluates the variable to find the directory it is supposed to go to (no $ is needed). Of course, if there is a sub-directory named "foo", it goes there directly, not bothering about any variables.
You can edit the ".dirs" file manually if you want - this is useful for updating paths and deleting directory short names which you no longer need. The 'sdirs' command is useful if you have set up a short name in one Terminal window and you want it to be available in another already-existing window. New windows automatically get all the short names by virtue of the line that sources the ".dirs" file.
I find the directory-path variables useful for other operations as well. For example, if I need to copy a bunch of files from the current directory to the directory which I had previously given the short name "acme", I can do it with the command:
% cp file1 file2 file3 $acmeHere the $ is necessary since (unlike 'cd') the 'cp' command does not automatically look for shell variables.
•
[10,429 views]

