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 it possible to use cd command to navigate back and forward (like browser)?. Something similar to cd - but it only swaps current and last location. I know I can push dir on stack, it would be great to use cd -> and cd <-, though.

share|improve this question
    
you might be able to invent this on your own with a batch script or two. –  uSlackr Sep 6 '11 at 13:58
add comment

4 Answers 4

up vote 3 down vote accepted

zsh has this feature.

Enable by setting these options

setopt autopushd
setopt pushdminus

then use with the following commands:

[tim@host] ~% cd
[tim@host] ~% cd /
[tim@host] /% cd /tmp
[tim@host] /tmp% d
0   /tmp
1   /
2   ~
3   ~
[tim@host] /tmp% cd -3
~

Some other zsh options you might want to look into:

autopushd
pushdminus
pushdsilent
pushdtohome
pushd_ignore_dups
share|improve this answer
add comment

You can use pushd and popd

A small tutorial on the subject.

share|improve this answer
    
Ye I know about pushd but I would like to have a queue of 20 last dirs. Ideally, each time I cd it will add a dir to the queue that I can use for navigation. –  lukas Sep 6 '11 at 14:21
add comment
 # try this function
 # function cd_func
 # This function defines a 'cd' replacement function capable of keeping, 
 # displaying and accessing history of visited directories, up to 10 entries.
 # To use it, uncomment it, source this file and try 'cd --'.
 # acd_func 1.0.5, 10-nov-2004
 # Petar Marinov, http:/geocities.com/h2428, this is public domain
    cd_func ()
    {
    local x2 the_new_dir adir index
    local -i cnt

    if [[ $1 ==  "--" ]]; then
      dirs -v
      return 0
    fi
    the_new_dir=$1
    [[ -z $1 ]] && the_new_dir=$HOME

    if [[ ${the_new_dir:0:1} == '-' ]]; then
      #
      # Extract dir N from dirs
      index=${the_new_dir:1}
      [[ -z $index ]] && index=1
      adir=$(dirs +$index)
      [[ -z $adir ]] && return 1
      the_new_dir=$adir
    fi

    #
    # '~' has to be substituted by ${HOME}
    [[ ${the_new_dir:0:1} == '~' ]] && the_new_dir="${HOME}${the_new_dir:1}"

    #
    # Now change to the new dir and add to the top of the stack
    pushd "${the_new_dir}" > /dev/null
    [[ $? -ne 0 ]] && return 1
    the_new_dir=$(pwd)

    #
    # Trim down everything beyond 11th entry
    popd -n +11 2>/dev/null 1>/dev/null

    #
    # Remove any other occurence of this dir, skipping the top of the stack
    for ((cnt=1; cnt <= 10; cnt++)); do
      x2=$(dirs +${cnt} 2>/dev/null)
      [[ $? -ne 0 ]] && return 0
      [[ ${x2:0:1} == '~' ]] && x2="${HOME}${x2:1}"
      if [[ "${x2}" == "${the_new_dir}" ]]; then
        popd -n +$cnt 2>/dev/null 1>/dev/null
        cnt=cnt-1
      fi
    done

    return 0
    }

    alias cd=cd_func
share|improve this answer
add comment

I use these functions:

export BACK_HISTORY=""
export FORWARD_HISTORY=""

function cd {
    BACK_HISTORY=$PWD:$BACK_HISTORY
    FORWARD_HISTORY=""
    builtin cd "$@"
}

function back {
    DIR=${BACK_HISTORY%%:*}
    if [[ -d "$DIR" ]]
    then
        BACK_HISTORY=${BACK_HISTORY#*:}
        FORWARD_HISTORY=$PWD:$FORWARD_HISTORY
        builtin cd "$DIR"
    fi
}

function forward {
    DIR=${FORWARD_HISTORY%%:*}
    if [[ -d "$DIR" ]]
    then
        FORWARD_HISTORY=${FORWARD_HISTORY#*:}
        BACK_HISTORY=$PWD:$BACK_HISTORY
        builtin cd "$DIR"
    fi
}
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.