powershell-profile icon indicating copy to clipboard operation
powershell-profile copied to clipboard

Any idea how to create the equivalent to "cd --" in powershell?

Open E3V3A opened this issue 5 years ago • 7 comments

I found you great powershell aliases to get nix style back into PS.

I've been looking for a while, to find the fantastic equivalent to using cd -- in linux/bash, to get the last visited directories.

$ cd --
 0  /cygdrive/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.2/lib/x64
 1  /cygdrive/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.2/lib
 2  /cygdrive/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.2
 3  /cygdrive/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.2/tools
 4  /cygdrive/d/avatarify/avatarify
 5  /cygdrive/d/avatarify
 6  /cygdrive/d
 7  ~

Then you can just do cd -4 to go there.

I use this 100 times a day, even under Cygwin, but I can't find anything like it in Windows powershell. The closest info is here.

Any ideas how to implement this?

E3V3A avatar May 01 '20 22:05 E3V3A

Hrm that's fun. I'll try this weekend.

On Fri, 1 May 2020, 23:03 E:V:A, [email protected] wrote:

I found you great powershell aliases to get nix style back into PS.

I've been looking for a while, to find the fantastic equivalent to using cd -- in linux/bash, to get the last visited directories.

$ cd -- 0 /cygdrive/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.2/lib/x64 1 /cygdrive/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.2/lib 2 /cygdrive/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.2 3 /cygdrive/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.2/tools 4 /cygdrive/d/avatarify/avatarify 5 /cygdrive/d/avatarify 6 /cygdrive/d 7 ~

Then you can just do cd -4 to go there.

I use this 100 times a day, even under Cygwin, but I can't find anything like it in Windows powershell. The closest info is here https://docs.microsoft.com/en-us/powershell/scripting/samples/managing-current-location?view=powershell-7#saving-and-recalling-recent-locations-push-location-and-pop-location .

Any ideas how to implement this?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/mikemaccana/powershell-profile/issues/6, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABKEMUUA2FJ6NCFYNKRULLRPNBLBANCNFSM4MXMSRMA .

mikemaccana avatar May 01 '20 22:05 mikemaccana

Also if you want to try, you'd made a cd function that stored the new directories in an array. When someone does a --, you pop the last item off the end of the array.

On Fri, 1 May 2020, 23:03 E:V:A, [email protected] wrote:

I found you great powershell aliases to get nix style back into PS.

I've been looking for a while, to find the fantastic equivalent to using cd -- in linux/bash, to get the last visited directories.

$ cd -- 0 /cygdrive/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.2/lib/x64 1 /cygdrive/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.2/lib 2 /cygdrive/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.2 3 /cygdrive/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.2/tools 4 /cygdrive/d/avatarify/avatarify 5 /cygdrive/d/avatarify 6 /cygdrive/d 7 ~

Then you can just do cd -4 to go there.

I use this 100 times a day, even under Cygwin, but I can't find anything like it in Windows powershell. The closest info is here https://docs.microsoft.com/en-us/powershell/scripting/samples/managing-current-location?view=powershell-7#saving-and-recalling-recent-locations-push-location-and-pop-location .

Any ideas how to implement this?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/mikemaccana/powershell-profile/issues/6, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABKEMUUA2FJ6NCFYNKRULLRPNBLBANCNFSM4MXMSRMA .

mikemaccana avatar May 01 '20 22:05 mikemaccana

I just found this:

  • https://github.com/tkellogg/Jump-Location
  • https://github.com/vors/ZLocation

But it doesn't quite get there, although smart. (I like numbered lists)

E3V3A avatar May 01 '20 22:05 E3V3A

Hmm, here's the bashrc function:

#----------------------------------------------------------
# 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=$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

E3V3A avatar May 01 '20 22:05 E3V3A

Also if you want to try, you'd made

Unfortunately I suck at anything powershell, and whatever I get done, is only through pure, sheer, iron, will-power, so it take me ages! (Improving though.)

E3V3A avatar May 01 '20 22:05 E3V3A

any updates or ideas?

E3V3A avatar May 15 '20 19:05 E3V3A

Following the links above, it seem that this commandlet is getting very close to that idea.

  • https://github.com/Pscx/Pscx/blob/master/Src/Pscx/Modules/CD/Pscx.CD.psm1#L78

however, it required two files where messages.psd1 is one of them. I'm not able to get this to run properly as stand-alone (as a function) though.

eabase avatar Oct 06 '20 19:10 eabase