CLI Magic: CDargs

505

Author: JT Smith

Typing long path names at the command line can get to be a chore very quickly. Even with tab-completion, it can take a lot of typing to move from your home directory to /var/www/www.mysite.com/cgi-bin or something similar. Wouldn’t it be much better if you could “bookmark” long path names and type something simple, like cdb site, to get to a directory? That’s where CDargs comes in. CDargs is a program that provides bookmarks and browsing at the command line. It takes a little work to set up, but it’s well worth it. I’ve been using this program for a few years now, and it really does help speed up work at the shell.

I don’t know of any distributions that install CDargs by default, but some Linux distributions, such as Debian and Ubuntu, provide packages for it, and Gentoo provides an ebuild. If packages aren’t available for your distro, the CDargs download page has RPMs for x86, source RPMs, and source code. CDargs should compile on most Unix-type systems. In addition to Linux, I’ve compiled and used it on Mac OS X with no problems.

After you’ve installed CDargs, you’ll need to set up the shell functions that are included with it. You can use cdargs by itself for some functions, but it’s much easier to use in conjunction with the shell functions. To set up the CDargs functions with bash, you’ll need to edit your .bashrc to add the following line:

source /$DIR/cdargs-bash.sh

Replace $DIR with the path to the file cdargs-bash.sh. If you’ve installed the Debian package, for example, cdargs-bash.sh can be found under /usr/share/doc/cdargs/examples. If you installed the RPM or compiled from source, the location will be different. To make the shell functions take effect immediately, run source cdargs-bash.sh — with the appropriate path to cdargs-bash.sh, of course.

Now for the fun part. To test the cd browser, type cv. You will see a list of subdirectories in your present working directory. For example, if you happen to be in the root directory on a Debian server, you would see something similar to Figure 1:

Figure 1. Using cv to browse the directory structure – click to enlarge

There are two modes for cv: browse mode and list mode. In the browse mode you can navigate the directory structure by using the arrow keys, vi movement keys, Emacs movement keys, or one of the number keys. In this example, if you wanted to navigate into the /home directory (which is highlighted), you could use the left arrow key (or the vi or Emacs movement keys) to browse the /home directory structure. Using the number keys, you will move the cursor to the numbered entry. If there are more than 10 directories in your working directory, only the first 10 will be numbered. Press the Enter key to move into the highlighted directory. If you’re looking for a “hidden” directory (any directory that starts with a “.”) you’ll need to press the period key to tell CDargs to display hidden directories.

To toggle between list and browse mode, use the Tab key. At the bottom of the display, you’ll see a “B” or an “L,” indicating whether you’re in browse mode or list mode, and the present working directory. Here you can manage your bookmarks or use them to move around in the directory tree.

The first time you start CDargs, you won’t have any directories bookmarked. Press a to automatically generate a bookmark — this will add the present working directory to the list of bookmarks, using the final directory’s name for the bookmark name. In /usr/local/include/linux/netfilter_bridge, for example, the bookmark’s name would be “netfilter_bridge.” If you’d like to give the bookmark a shorter name, press A and CDargs will prompt you for a description.

Once you have a few directories bookmarked, you may want to edit the descriptions or the actual directory. Typing e while in list mode will drop you into your default text editor, and you can edit the entries just as you would any text file. In fact, the list of bookmarks is a regular text file called .cdargs in your home directory.

To get the full list of available commands, type H or ?. This will take you into the CDargs help screen. Case is important, by the way. Using h instead of H moves you up one level in the directory browser.

Browsing the directory structure using cv is usually faster than typing out the directory you want to move to — though it takes a while to get used to using cv instead of cd. What’s even faster, though, is to bookmark your most frequently visited directories and use cdb to change directories.

For example, if you have created a bookmark called “web” for /var/www/mysite, the command cdb web will move you to the /var/www/mysite directory. I try to keep all of the bookmark names very short, to make them easy to remember and type. The longest bookmark description I have is five characters, and most are two to four characters.

It’s also possible to add bookmarks without having to enter the CDargs browser at all. If you’re in a directory that you want to bookmark, just use the ca command to add it to your list of bookmarks. To add a description for the bookmark, use ca description, so if you want to create a bookmark called “linux,” just use ca linux and cdargs will confirm that it’s been added.

What if you want to copy or move files to one of the directories you have bookmarked? No problem. The cpb shell function handles copying files to a bookmarked directory, and the mvb function moves files to a bookmarked directory.

To copy files, use cpb filenamebookmark. For moving files, use mvb filename bookmark .

Make sure to check the CDargs man page and its help page for all of its options. There’s a little setup required with CDargs, but it’s well worth it. It’s simple to use, and makes life at the command line just a little bit easier.