Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

On Windows with Cygwin in the .bashrc file there is a function that you can uncomment. I did so but I cannot figure out how to make it work or call it.

I tried doin the cd -- in the command line but it doesn't do anything

# b) 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=$adircd 
   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
 }
share|improve this question
    
Have you opened a new Cygwin shell/terminal, or . sourced the .bashrc file to get the function into your session? –  jscott Dec 5 '11 at 18:14
    
I'm not familiar with "sourced" I thought that file was included already. I just tried source .bashrc and it says bash: $'\r': command not found –  jasondavis Dec 5 '11 at 18:34
    
Problem with DOS-like line endings, sigh. –  kubanczyk Dec 5 '11 at 19:07

1 Answer 1

up vote 1 down vote accepted

Either

alias cd="cd_func"
cd --

or simply

cd_func --
share|improve this answer
    
That's what I was thinking but it give this error bash: cd_func: command not found –  jasondavis Dec 5 '11 at 18:11
    
Please note that .bashrc is NOT run (nor sourced) on login, this is when .bash_profile is applied instead. Usually .bash_profile should call 'source .bashrc' explicitly. –  kubanczyk Dec 5 '11 at 19:06
    
Ok thanks, now looking at my .bash_profile it looks like it comes with source "${HOME}/.bashrc" already in it, thanks for your help –  jasondavis Dec 5 '11 at 19:20

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.