Powershell: Add support for 'cd -'

Created on 5 Sep 2016  路  14Comments  路  Source: PowerShell/PowerShell

Sorely missing the ability of being able to go to previous directory without any previous action.

Area-Cmdlets Hacktoberfest Issue-Enhancement Resolution-Fixed Up-for-Grabs

Most helpful comment

Good point! We probably should add cd - syntax at the same time.

All 14 comments

Good point! We probably should add cd - syntax at the same time.

@SRGOM For now you can use Push-Location (alias pushd) and Pop-Location (alias popd).

@vors if you're going to do that then go all the way and implement a directory history stack e.g.:

13> cd

     # Directory Stack:
   --- ----------------
     0 C:\Users\Keith
     1 C:\Users\Keith\GitHub\PowerShell\Plaster
     2 C:\Users\Keith\GitHub\rkeithhill\PowerShellEditorServices
     3 C:\Users\Keith\GitHub\rkeithhill\vscode-powershell
     4 C:\Users\Keith\GitHub\rkeithhill\vscode-powershell\scripts
     5 C:\Users\Keith\GitHub\rkeithhill\vscode-powershell
->   6 C:\Users\Keith\GitHub\rkeithhill

~\GitHub\RKEITHHILL
14> cd -
~\GitHub\rkeithhill\vscode-powershell
15> cd -
~\GitHub\rkeithhill\vscode-powershell\scripts
16> cd +
~\GitHub\rkeithhill\vscode-powershell
17> cd -1
~\GitHub\PowerShell\Plaster
18> cd $profile
~\Documents\WindowsPowerShell
19> cd c:\program files
C:\program files

See https://github.com/Pscx/Pscx/blob/master/Src/Pscx/Modules/CD/Pscx.CD.psm1

@rkeithhill cd - actually was part of https://github.com/PowerShell/PowerShell/issues/1083#issuecomment-228840665

Is this history stack used in other shells?

@vors Take Command supports this: https://jpsoft.com/help/index.htm?cd.htm

In fact, you will probably find several PowerShell modules that implement CD history in different ways. The one above is from PSCX. I've seen other modules that retain history and use fuzzy matching to find the directory with auto-completion as you type IIRC. There's likely enough variation here that this functionality is best implemented by 3rd party modules.

That's what I think. There are a lot of approaches for advanced usage and they are implemented or could be implemented as additional modules. Core functionality should be conservative and universal. PowerShell team agreed that cd - should be included in the core cd, but that's probably where we stop.

I'm a bit confused. If cd - is in the core cd, doesn't that make cd into pushd and popd?

Or perhaps I should say: maybe you should implement cd as a function which calls pushd (or popd).

If cd - is in the core cd, doesn't that make cd into pushd and popd?

No, because it will only keep knowledge about 1 previous location. And this location could be $env:OLDPWD which would address the original issue described by @SRGOM

I should note that I wouldn't want this to be specific to OS-Linux, although yes such a functionality isn't present in Windows already.

Look at http://linux.die.net/man/1/zshoptions if you want to know what all options zsh has. A lot of us use a lot of options over there.

I would love to use an actual programming language with a clean syntax as my shell but I will not give up the ease of access that comes with bash/zsh. I will list a few power tools/options that come to my mind that are absolutely indispensable to me and the power users I know.

1) History, big deal for most. I have this setopt histfindnodups histignoredups histignorespace. I want to be able to search through it, and very quickly

2) Moving around- I use github:rupa/z which is a fuzzy matcher. You think you can show a navigable dropdown in fuzzy cd mode? Maybe call it zd? z doesn't have a dropdown but its still very cool

3) Completion/editing - one area where I feel PSReadLine is lacking compared to bash/zsh. (Or maybe I don't know enough? Please correct me). Can't do Ctrl-X Ctrl-E (edit command in EDITOR), Alt-s, cycle through completions in bash or just repeated tabs in zsh.

TBC- I don't want PoSh to grow haphazardly like bash/zsh which are really pathetic for anything but interactive use. I also love the long-names, it makes my history so much better to read. But it's just very lacking for interactive use.

I hope you guys incorporate features from zsh while keeping Powershell consistent and solid.

Thank you for the thoughtful feedback!

Yes, definitely not only on unix. We would like to keep the experience as close as it possible on all platforms.

Absolutely agree about history. In fact, the very first PS code that I wrote was https://github.com/vors/PersistentHistory (yes, PowerShell didn't have persistent history built-in, kind of crazy). Meanwhile, PSReadLine was included in the out-of-the-box package and it addressed a lot of interactive usage issues, including history.
I.e. histfindnodups histignoredups == Set-PSReadlineOption -HistoryNoDuplicates.
Ctrl-X Ctrl-E is probably not in the standard keybindings, but can be added with Set-PSReadlineKeyHandler

For moving around, there are projects inspired by autojump and z.sh

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

They haven't been ported to Unix yet, but ZLocation probably would be at some point.
Also, as @rkeithhill mentioned, PSCX has a replacement for cd. The point here is: there are a lot of different approaches about moving around and I don't think that we should pick THE ONE to be included in the core. They are already implemented (or could be implemented) as 3rd party modules.
Meanwhile, cerating things (like cd -) could and probably should be included in the out-of-the-box expirience, because it's well established and expected in unix shells.

Thanks for your reply, @vors. I agree about a careful boundary about what should be in powershell and what should be outside. I do think that certain useful functionality should be added-on by default using external packages, PS-Readline itself being a great example.

I think I agree about the history part btw- it doesn't belong in Powershell itself (just like a lot of things don't). I'm right now on Linux and not liking how zsh/bash handle history order and timestamps(love everything else history-related). Maybe there's a need to stop and think about the right way of doing things.

If we are talking about a location history (like command history, except locations only), then yes please.

@iSazonov Can you please rename the issue title to reflect its current state better. Something like Add support for 'cd -'?

Was this page helpful?
0 / 5 - 0 ratings