Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|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
add comment

4 Answers

up vote 5 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

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

Just to chime in with my own experience, I wrote a simple script to address this requirement some time ago, it overrides the builtin cd command with a simple function which appends the new directory location to a history file, a python script is then used to provide a bash interface which dynamically updates an ordered list of directories as you enter search terms, somewhat like the reverse command search of bash.

It's available on git-hub for those that are curious.

share|improve this answer
add comment

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>.

share|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
add comment

bash has pushd/popd/dirs. I have this in my .bashrc to auto-push directories onto bash's stack.

#let cd also pushd directories into stack. Use popd to reverse stack
function cd ()
{
  if [ -e $1 ]; then 
    pushd $1 &> /dev/null   #dont display current stack 
  fi
}

Pop these using popd and display the stack using dirs

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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