发布时间:2022-08-09 文章分类:编程知识 投稿人:王小丽 字号: 默认 | | 超大 打印

Teleport - the Unix/Linux cd command, improved

Summary:The Teleport command is an improvement on the Unix/Linux cd command, basically a cd command that keeps a history of directories you've visited. By having a memory, the Teleport commands lets you transport from one directory to a previously visited directory very easily.

New in Version 0.5:Agreat new '-n' option has been contributed to the Teleport command. See the 'Teleport by number' section below.

(If you're just looking for the Download link, it's at the bottom of this article.)

(Also, if you'd like to see the Teleport command in action, I've created this Teleport command demo video on YouTube.)

The Teleport command

I've named the command "teleport", and it's intended to be a replacement for the Unix/Linux cd command. Really, it's a wrapper around the cd command, first doing all of its own work related to keeping a history, and then issuing the appropriate Linux cd command.

The teleport command (tp, for short) does everything the Linux cd command does, with these improvements:

  • It keeps a history, remembering the directories you've visited before.
  • It can display your history.
  • You can easily jump to any directory in your history.
  • And you can create aliases for your directories to make it even easier to jump to them.

Teleport command help

Once installed, you can get help on the teleport command by issuing the -h flag, like this:

tp -h

This displays a usage statement similar to the following:

Usage:      tp [DIR]
Purpose:    Provide the same capabilities as the 'cd' command, but with a memory
            of directories that you have previously visited.
  -h        Show HELP (this output)
  -j PARTIAL-DIR
            JUMP to a directory found in your history (by partial directory name)
  -l        LIST the history of visited directories
  -n [NUM]
            Go to a directory by NUMBER. NUM corresponds to the number printed
            by the -l option. If NUM is not specified then a list of visited
            directories is shown and user can pick a number.
  -s DIR    SEARCH for and display a directory named DIR found in your history

Given that starter information, let's see what the teleport command can do.

For basic use, tp is just like cd

For basic cases, you'll use the tp command just like the cd command. For instance, to move to your home directory, just type tp by itself, just like you would with the cd command:

tp

Or, if you prefer more typing, you can add the ~ character:

tp ~

Either one of those commands will take you to your home directory, just like cd.

To move back to your last directory, just add a -, again, just like cd:

tp -

To move to a local subdirectory named Pictures, again just replace the cd command with tp:

tp Pictures

To move to an absolute directory, like /tmp, again tp works just like cd:

tp /tmp

And to move up two directory levels, again, tp is the same:

tp ../..

Nothing too exciting yet ... so far it it works just like the Unix/Linux cd command. Now let's have some fun.

Basic teleporting

Where the tp command really shines is with its memory. For instance, let's say your current working directory is something like this:

/tmp/foo/bar/baz

Next, let's assume you want to move to another directory somewhere else in the filesystem, like this one:

/home/al/Pictures/2008/AlaskaTrip/Talkeetna

With the cd command, you'd normally have to type something like this:

cd /home/al/Pictures/2008/AlaskaTrip/Talkeetna

But with the tp command, if you've already been to the Talkeetna subdirectory, all you have to type is this:

tp Talkeetna

The teleport command instantly takes you to that directory. In this case, that saves you over 30 characters of typing.

Here's an image showing a few more examples of how easily the Teleport command lets you jump around:

This is one example of what remembering your working directory history can do for you, and is the primary reason for the name "teleport".

Listing your teleport history

The next nice thing teleport does is provide a listing of your teleport history. When you issue the teleport "list" command, like this:

tp -l

teleport will show you all the directories you've visited recently, like this:

43  /Users/al/Projects/DD/Sketch
44  /Users/al/Desktop
45  /Users/al/Projects/DD/Imagen/release
46  /Users/al/Projects/DD/Hyde/build
47  /Applications/MAMP/htdocs/minime/sites/all/modules/minime
48  /Applications/MAMP/logs
49  /Users/al/Projects/DD/Imagen/build
50  /Users/al/Working/Cato-CRUD-Generator
51  /Applications/MAMP/htdocs/cato
52  /Users/al/Projects/DD/Imagen
53  /Applications/MAMP/htdocs/cato/templates
54  /Applications/MAMP/htdocs/sleetmute

I like this feature because (a) I work on a lot of different projects every week, and (b) I have a bad memory. With the teleport command, it's much easier to remember where all my project files are located.

Also, once you've seen this history, you can teleport to any of those directories very easily. For instance, no matter where I am currently in the filesystem, if I want to go to this directory:

/Applications/MAMP/htdocs/minime/sites/all/modules/minime

all I have to type is this command:

tp minime

and I'm instantly teleported to that directory. I think you'll agree that's a really nice time-saver.

Teleport by number

The teleport command also includes an option to let you teleport to a directory by the number that is shown when you list the teleport history. For instance, using the listing shown above, I can teleport to the same 'minime' directory by typing this -n command:

tp -n 47

This is a nice feature for many purposes, especially when you have multiple directories that end with the same name.

New in Version 0.5:Thanks to a nice contribution by John Santos (somewhere in Portugal), the '-n' command has been significantly improved. Now, instead of typing '-l' and then using those results to feed the '-n' command, if you now type the -n command without a number, like this:

tp -n

the Teleport command will automatically give you the -l history list, and prompt you for a selection, like this:

50  /Users/al/Working/Cato-CRUD-Generator
51  /Applications/MAMP/htdocs/cato
52  /Users/al/Projects/DD/Imagen
53  /Applications/MAMP/htdocs/cato/templates
54  /Applications/MAMP/htdocs/sleetmute
Pick a number or press [Enter] to cancel: _ 

If you've used the Teleport command before, you know this is a sweet addition. I suspect most people will now just use the -n option, and -l will mostly hang around for legacy purposes.

You can also use a teleport alias.

Teleport aliases

Teleport aliases provide a simple way for you to create aliases to directories. If you want to resolve conflicts between multiple directories with the same name (as I just showed in the previous section), or you simply want to make sure teleport always knows about a certain directory, you can create an alias for that directory.

Currently the teleport alias works as a separate command named tpa. (I've thought about merging the tp and tpa commands into one command, but have not done so yet. After using the commands for a while, I'll be glad to hear your opinion on whether they should be merged together, or not.)

Here's the usage statement for the teleport alias (tpa) command:

Usage:           tpa ALIAS
Purpose:         Transport to a directory that has an alias ALIAS associated with it.
  -a ALIAS DIR   Add ALIAS as an alias/shortcut for the directory DIR
  -h             Show HELP (this output)
  -l             LIST the known directory aliases
  -r ALIAS       Remove ALIAS from your list of known aliases

Adding/creating a teleport alias

To add a new teleport alias, use the -a option, and specify the alias first, followed by the actual directory name, like this:

tpa -a baz /tmp/bar/baz

After issuing that command you'll see output similar to this:

Alias "baz" was added for the directory "/tmp/bar/baz".

Using a teleport alias

Once you've created an alias like that, no matter where you are on the filesystem, you can move to that directory using your alias with the tpa command, like this:

tpa baz

Listing your teleport aliases

To list your teleport aliases, just use the -l option of the tpa command, like this:

tpa -l

This will list all of the aliases you've defined, like this:

perl:/Users/al/AlsLife/DD/Perl
arrays:/Users/al/AlsLife/DD/Perl/ArrayTests
pub:/Users/al/AlsLife/DD/Perl/published

(I need to improve this format, but that's the way it looks currently.)

Removing an alias

To remove a tpa alias, just use the -r option and the name of the alias, like this:

tpa -r pub

The tpa command responds like this:

About to remove alias "pub" -- are you sure (y/[n])?: _
Remove operation cancelled.

At this point 'n' will cancel the remove operation, and 'y' will proceed with the remove.

Teleport command - summary

I hope you like the teleport command.Ithink it makes some nice improvements to the standard Linux cd command.

The hardest part for me when I first started using it was remembering to type tp instead of cd. Once I made it through that mental hurdle, I now hate to work on systems where I haven't installed the teleport command yet.

Teleport command - download

You can download the teleport command as a tar'd and gzip'd file from the following link:

  • Click here to download the teleport command

    (md5sum: 2159de9e82e9a4ccee6a450fa8cb75c2)

This file will extract to a directory named teleport, and that directory will contain the following files:

teleport/tp_command
teleport/INSTALL
teleport/LICENSE

Once you've extracted the contents of this file, you can proceed with the installation. See the INSTALL file for more information.