Is there something like bash's reverse-search-history (Ctrl-r), but for only directories?

I have some deep folder hierarchies that I want to jump to, so I'd like to use something like reverse-search-history, but it only looks for folder names and gives me absolute paths.

Essentially, it would give similar results to using !? but only matching commands with cd in the front, you can step through results, and full paths.

So far, the best solution I've found is bashmarks.

link|improve this question
1  
I recently started using z and that seems to work so far, but I used it for a few paths. – Rob Jun 20 '11 at 17:04
feedback

2 Answers

up vote 3 down vote accepted

Have a look at autojump:

One of the most used shell commands is “cd”. A quick survey among my friends revealed that between 10 and 20% of all commands they type are actually cd commands! Unfortunately, jumping from one part of your system to another with cd requires you to enter almost the full path, which isn’t very practical and requires a lot of keystrokes.

autojump is a faster way to navigate your filesystem. It works by maintaining a database of the directories you use the most from the command line. The jumpstat command shows you the current contents of the database. You need to work a little bit before the database becomes usable. Once your database is reasonably complete, you can “jump” to a commonly "cd"ed directory by typing:
j dirspec

link|improve this answer
It might lead to issues with Terminal's title. – Daniel Beck Jun 24 '11 at 11:01
feedback

Well, you could add this code snippet to your ~/.bashrc, which

  1. provides a custom cd command

    function cd () { exists=false for dir in "${CDHIST[@]}"; do [ "$dir" == "$1" ] && { exists == true break } done

    $exists || {
        len=${#CDHIST[@]}
        ${CDHIST[$len]} = "$1"
    }
    
    builtin cd "$1"
    }
    
  2. and provides a cd history search command.

    function cdhist () { #TODO: Make this magical.

    for dir in "${CDHIST[@]}"; do
        echo "$dir"
    done
    }
    

Of course, the cdhist command I provided is very basic, and not what you wanted; but it is conceivable to use case statements or parameter expansion to achieve something similar to what you want.

You could even add some "Programmable completion" function, which could be used to add the full cd /path/to/mydir command based on a unique sub-string of /path/to/unique/mydir, although that method would still require you to type cd unique/mydir<tab>.

link|improve this answer
1  
I misread your code and found bash's dirs command which does essentially the same thing as my_dirs, but on one line. – pydave Jun 20 '11 at 18:55
Cool. So not only does bash support it; it implements it. I wish I had more time to study bash's deep magic. Of course, this only supports remembering pushd, not cd. But I suppose alias cd=pushd would be the most elegant form. – jpaugh Jun 25 '11 at 0:46
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.