Linux.com

Feature

CLI Magic: CDargs

By Joe "Zonker" Brockmeier on September 19, 2005 (8:00:00 AM)

Share    Print    Comments   

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:

Using cv to browse the directory structure
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 filename bookmark . 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.

Share    Print    Comments   

Comments

on CLI Magic: CDargs

Note: Comments are owned by the poster. We are not responsible for their content.

So what is wrong with shell variables

Posted by: Anonymous Coward on September 19, 2005 04:13 PM
Surely it would be simpler to define a set of shell variables in your<nobr> <wbr></nobr>.profile and simply use them instead?

alan.
<a href="http://blogs.sun.com/tpenta" title="sun.com">http://blogs.sun.com/tpenta</a sun.com>

#

So what is wrong with subdirectories

Posted by: Anonymous Coward on September 19, 2005 04:41 PM
I use a subdirectory in my home and root folder. if I want to go to gnome themes directory, i just do cd ~/gno/the . That way, I can even maintain heirarchies!

#

Absolutely nothing is wrong with shell variables

Posted by: Anonymous Coward on September 26, 2005 02:29 AM
Nothing is wrong with shell-variables. They don't need special versions of cp, mv and cd, either.

They can also be used in other commands that are not aware of CDargs.

I have written something about how to do it here:
<a href="http://linuxbasics.org/tutorials/advanced/realworld/directory-bookmarks" title="linuxbasics.org">http://linuxbasics.org/tutorials/advanced/realwor<nobr>l<wbr></nobr> d/directory-bookmarks</a linuxbasics.org>

Even with a nice way to automatically add the current directory to the list.

Stefan

#

bash arg completion

Posted by: Anonymous Coward on September 19, 2005 05:48 PM
bash can complete args from your history
using M-TAB (dynamic-complete-history)
or alt-TAB

for example, type
$ echo fjdslkfjdslk
then try
$ printf fj M-TAB

if this doesn't work try putting
M-TAB: dynamic-complete-history
in your<nobr> <wbr></nobr>.inputrc

bash can also complete variable names
echo $P TAB TAB

#

Re:bash arg completion

Posted by: Administrator on November 04, 2005 12:36 AM
I tend to set up the following in ~/.inputrc to allow for 4DOS-style history searching:

"\M-[A":history-search-backward
"\M-[B":history-search-forward

#

How about symlinks?

Posted by: Anonymous Coward on September 19, 2005 08:04 PM
If I am going to be working from the command line in deep directories, I set up symlinks to those directories. Easy.

#

aliases and pushd

Posted by: Anonymous Coward on September 20, 2005 12:32 AM
Why not use some simple BASH aliases to invoke "pushd PATH"? Then, create a simple script to concatenate new paths to and re-source your aliases file.
<tt>#!/bin/bash
#
# addnickpath -- Add a path nick to your aliases file
#
ALIASFILE=~/.bash_aliases

usage() {
    cat << EOUSAGE >&2
Usage: $0 NICK PATH

Add a nick alias for a pathname.  The nick alias will not be added if one
already exists.  The alias will only be ACTIVE if the directory exists, but the
rule will still be added to your aliases file.

Current Alias File: ${ALIASFILE}
EOUSAGE
}

if [ $# -lt 2 ] ; then
    usage
fi

# Check for the alias
if { alias|grep $1 >/dev/null; } ; then
    echo "Alias exists.  Aborted." >&2
    exit 1
fi

if { grep "alias $1=" $ALIASFILE >/dev/null; } ; then
    echo "Alias already added to $ALIASFILE.  Aborted." >&2
    exit 1
fi

# Add nick alias to ~/.bash_aliases
cat << EOADD >> ${ALIASFILE}
# Added with $0
if [ -d "$2" ] ; then
    alias $1="pushd $2"
fi
EOADD

echo "# Re-source your ${ALIASFILE} with:"
echo ". ${ALIASFILE}"

exit 0</tt>
Just type "NICK" at the commandline. Use a naming convention such as "goto-PLACE", so that your bash-completion will give you all the options...
<tt>shell$ goto-[tab] [tab]
goto-web  goto-logs  goto-wherever</tt>
Because you used "pushd", you have pushed the directory on to a stack. Use "popd" to remove the top directory and go to the previous directory on the stack, or use "pushd" to traverse to another directory on the stack. Type "dirs" to print the stack.

To delete path nicks, edit your $ALIASFILE. To see where an alias points, just type "alias ALIASNAME". Enjoy.

#

lower back pain

Posted by: Anonymous Coward on May 28, 2006 01:58 PM
[URL=http://painrelief.fanspace.com/index.htm] Pain relief [/URL]

  [URL=http://lowerbackpain.0pi.com/backpain.htm] Back Pain [/URL]

  [URL=http://painreliefproduct.guildspace.com] Pain relief [/URL]
[URL=http://painreliefmedic.friendpages.com] Pain relief [/URL]
[URL=http://nervepainrelief.jeeran.com/painrelief<nobr>.<wbr></nobr> htm] Nerve pain relief [/URL]

#

relief joint

Posted by: Anonymous Coward on May 30, 2006 01:06 AM
[URL=http://nervepainrelief.jeeran.com/painrelief<nobr>.<wbr></nobr> htm] Nerve pain relief [/URL]

  [URL=http://www.back.painreliefnetwork.net/lowbac<nobr>k<wbr></nobr> pain.htm] Low back pain [/URL]

  [URL=http://blog.gala.net/uploads/painreliefback/<nobr>b<wbr></nobr> ackpainrelief.htm] Back pain relief [/URL]

  [URL=http://www.weblog.ro/usercontent/13155/profi<nobr>l<wbr></nobr> es/kneepainrelief.htm] Knee pain relief [/URL]

  [URL=http://www.info.painreliefnetwork.net/Pain-R<nobr>e<wbr></nobr> lief.html] Pain relief [/URL]

  [URL=http://www.sitefights.com/community/scifi/pa<nobr>i<wbr></nobr> nrelief/painreliefpreved.htm] Pain relief [/URL]

  [URL=http://www.info.painreliefnetwork.net/Medica<nobr>t<wbr></nobr> ion-Pain-Relief.html] Medication pain relief [/URL]

  [URL=http://www.info.painreliefnetwork.net/Natura<nobr>l<wbr></nobr> -Pain-Relief.html] Natural pain relief [/URL]


  [URL=http://painrelief.fanspace.com/index.htm] Pain relief [/URL]

  [URL=http://lowerbackpain.0pi.com/backpain.htm] Back Pain [/URL]

  [URL=http://painreliefproduct.guildspace.com] Pain relief [/URL]
[URL=http://painreliefmedic.friendpages.com] Pain relief [/URL]

#

pushd, popd

Posted by: Anonymous Coward on September 20, 2005 01:33 PM
Nice idea, but as already mentioned, shell variables, aliases and symbolic links can already work around the problem.
Also, the pushd and popd builtins can help.

#

Thanks for the info on cdargs

Posted by: Anonymous Coward on September 20, 2005 03:27 PM
I must inhabit the same parallel universe as the author of the article - the other commenters suggestions sound like far more work than using this tool<nobr> <wbr></nobr>:-)

#

Wow...

Posted by: Anonymous Coward on September 20, 2005 10:00 PM
All that faffing around... the Amiga did it so much better with its ASSIGN command...

#

cdpath

Posted by: Administrator on November 08, 2005 08:23 AM
I just use bash's $CDPATH var for built in support for something similar. It's used like $PATH is for executables, only 'cd' checks this before you change directories:


<tt>

sith@sithtop:~$ export CDPATH=/home/sith/cvs/:/usr/src/

sith@sithtop:~$ cd myproject
<nobr> <wbr></nobr>/home/sith/cvs/myproject

sith@sithtop:~/cvs/myproject$ cd linux-`uname -r`
<nobr> <wbr></nobr>/usr/src/linux-2.6.11.12-grsec

sith@sithtop:/usr/src/linux-2.6.11.12-grsec$ pwd
<nobr> <wbr></nobr>/usr/src/linux-2.6.11.12-grsec

</tt>

#

This story has been archived. Comments can no longer be posted.


 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya