Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
E3V3A opened this issue May 1, 2020 · 7 comments
Open

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

E3V3A opened this issue May 1, 2020 · 7 comments

Comments

@E3V3A
Copy link

E3V3A commented May 1, 2020

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 E3V3A changed the title Any idea how to create th equivalent to "cd --" in powershell? Any idea how to create the equivalent to "cd --" in powershell? May 1, 2020
@mikemaccana
Copy link
Owner

mikemaccana commented May 1, 2020 via email

@mikemaccana
Copy link
Owner

mikemaccana commented May 1, 2020 via email

@E3V3A
Copy link
Author

E3V3A commented May 1, 2020

I just found this:

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

@E3V3A
Copy link
Author

E3V3A commented May 1, 2020

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
Copy link
Author

E3V3A commented May 1, 2020

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
Copy link
Author

E3V3A commented May 15, 2020

any updates or ideas?

@eabase
Copy link

eabase commented Oct 6, 2020

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

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants