Some Bash tricks: CDPATH and ~/.inputrc

August 14, 2010

With the help of the obnoxious desire to be as efficient as possible when using the command line, I’ve browsed the Internet looking for new ways of achieving a new level in geekdom.

As such, I’ve come across some interesting features of Bash that I’m currently using (quite proficiently).

The first one is the CDPATH Bash variable. If you do a man bash and search for it, it’s quite clear what it does. It allows you to specify additional folders (search paths) where the cd command should look into. The same manual page states, when talking about cd “The variable CDPATH defines the search path for the directory containing dir. Alternative directory names in CDPATH are separated by a colon (:)”. How’s this helpful? For example I have a folder $HOME/school/current/ where all school related folder reside. I simply add that folder to the CDPATH and, for example, wherever I call cd so from, it changes to $HOME/school/current/so.

Here’s a sample run scenario on my laptop

razvan@valhalla:~$ echo $CDPATH
razvan@valhalla:~$ cd so
razvan@valhalla:~/school/current/so$ cd p2p-next

It’s pretty neat! Whatever the current directory, if I do a cd so or cd p2p-next it gets me to the precise folder without the need to write the whole (sometimes cumbersome) path. A minor disadvantage is that there’s no cd Bash completion for files other than those in the current directory; you have to type the whole path of a folder reachable through CDPATH. Bash completion for the cd command works for paths reachable through CDPATH, provided the bash-completion package is installed.

Of course, in order to make it persistent, you’ll have to add a line such as the one below to your ~/.bashrc file:

export CDPATH=.:$HOME/school/current:$HOME/projects

You can find more info by issuing a simple Google search.

The second one is the INPUTRC file. There is a global /etc/inputrc file and a per-user ~/.inputrc file. The INPUTRC file defines Bash behavior with respect to keyboard input; it’s being used by the GNU Readline Library. There are a lot of features that may be tuned using the INPUTRC file.

The ones I’m using at this point is using TAB to show all matches in case of ambiguity for completion (similar to a TAB-TAB) and ignore case for completion. So, here are my two lines of configuration:

razvan@valhalla:~$ cat ~/.inputrc
set show-all-if-ambiguous on
set completion-ignore-case on

The ability to control the Readline library through INPUTRC is cool and useful. Check the documentation for the features you’re interested in.

tags: , , ,
posted in Administration, Free/Open Source Software by Răzvan Deaconescu

Follow comments via the RSS Feed | Leave a comment | Trackback URL

5 Comments to "Some Bash tricks: CDPATH and ~/.inputrc"

  1. Vlad Dogaru wrote:

    Bash completion for folders in $CDPATH works for me out of the box on Ubuntu. Maybe you need to install a package or configure somethinng? It’s actually quite helpful.


    Răzvan Deaconescu Reply:

    You’re right. I hadn’t installed the bash-completion package. I don’t know how I missed that :-) .

    Thanks for your remark! I’ve updated the post.


  2. Dorin wrote:

    I think that the use of CDPATH should be forbidden – if you start relying on that, you’ll probably create scripts that don’t run well on different machines, and that isn’t as cool as the feature sounds.
    I wouldn’t use it. The other two settings… quite cool


    Răzvan Deaconescu Reply:

    Thanks for your reply!

    I agree that CDPATH may be dangerous. But that may be the same with other tools and features. I find CDPATH really helpful for my needs, allowing a boost in my efficiency. I don’t plan to use it mindlessly :-) . When using scripts, even the PATH variabile may be problematic (that’s why, when using scripts, full path to script/executable files should be used) – should that also be forbidden?

    If I may quote: “UNIX was not designed to stop its users from doing stupid things, as that would also stop them from doing clever things.”


  3. Optional wrote:

    It’s cool, indeed, thanks!


Leave Your Comment



Powered by Wordpress and MySQL. Theme by Shlomi Noach,