12

One of the things that drive me crazy in command line under MS-Windows is the order in which the Windows shell (both the standard one and PowerShell) retrieve commands from history when you use the up and down arrow keys.

The standard way I've grown to like, as it is done in basically all other shells in the world, is that the last command is put into the history stack, and then pressing up will retrieve the last command I ran, "up" again will retrieve the one from before that, and so on. If you choose a command from history and submit it again, the command is copied to the top of the stack and if you press up on the next prompt you can see that last command and then the command previous to last. If after browsing the history you press "down" past the most recent command in the history stack, you will be returned to your empty prompt (or whatever you had there before you started browsing history).

In Windows shell, it works weirdly different: if you have 3 commands in the history - for example lets call them "A", "B" and "C" - and you use the up arrow key to select command "B" and execute it again, and then use the up arrow key again on the next prompt, the shell will display "B". But this is not the last command you executed (though "B" was indeed copied to the history stack again) as if you press "up" again you will be shown "A" and not "C". If you browse down (to later entries) you will see "B" again, followed by "C" then followed by "B" again which was the last command entered - but you also can't go any further: there's no going back to an empty prompt except deleting whatever you have on the line or pressing CTRL-C.

My question - is there some setting that one can set to change this behavior?

I'm aware of the option of just getting bash or something like that and using it on Windows, but due to external constraints (as in - that's what I need to write my scripts in), I have to work in PowerShell or another Windows shell that can invoke PowerShell scripts.

4
  • I'd say the answer is "probably not".
    – user3463
    Commented Aug 13, 2012 at 19:48
  • I can't solve your main problem, but a nice shortcut to remember is to press CRTL-Home to erase the current line.
    – zdan
    Commented Aug 13, 2012 at 20:50
  • BTW - this is another example of why making PowerShell be completely object based was not a good idea: by introducing new complex mechanisms you also introduce new frustrating weird behaviors and edge cases.
    – Guss
    Commented Sep 3, 2012 at 17:55
  • I agree it's maddening when you're used to a *nix prompt, but I think there's a rationale. I believe it "takes you back" to the command in the stack, so you are simply navigating up and down the list. It doesn't add a new item to the bottom and take you there, unless you change something or type something new. Once you remember you are staying back there in history, you can get used to it. But it's a very hard shift for muscle memory!
    – scipilot
    Commented Aug 24, 2017 at 8:23

2 Answers 2

7

There is a different way to control the history stack, it is not what you wanted but it is a different option you should check out.

In Properties (or Default) when you right click the top left corner there is an option "Discard Old Duplicates" tick it and the stack will change its behavior.

in your example performing "A", "B", "C". and then twice up to perform "B", the stack will look: "B","C","A" (first up will be "B")

2
  • 1
    Its not exactly what I wanted, but its so close it doesn't matter much - my main grievance was that hitting UP ARROW after executing a command from history doesn't revert the "current history entry" to the "after last entry" but to "after the entry you last selected" (but before the next one). This fixes it by still having the virtual history cursor "after the entry you last selected", but moving said entry to the end, causing the history cursor to also be "after last entry". Thanks!
    – Guss
    Commented Sep 3, 2012 at 17:53
  • Half way there, but it is SOOO much better. Thanks Ofir!
    – kmort
    Commented Dec 9, 2015 at 19:54
5

Also, don't forget F7. This pops up a DOS-style menu showing command history (ESC to make it go away). The 'last' command either entered, or selected with up-arrow will be shown, so you immediately will see where you are in the up/down arrow context.

Apparently this is from something called DOSKEY, a poorly documented utility. For example, besides up, down, and then enter to execute the command, I have found that a right or left arrow key will put it at the prompt without executing (so you can edit it). PgUp/PgDn/Home/End will navigate through the list in the obvious way, and I just discovered (haven't seen it documented anywhere) that if you press the first letter of a command it will cycle through commands starting with that letter (so type a command like ze fun start now! to jump to that point in history easily by pressing z in teh F7).

Perhaps though, if one foresees the reliving much of history, may tis better to

doskey /history > editme.bat

than suffer the slings and arrows, and copy/paste from said record (or have thine minions fashion it into a script).

There's a couple other command prompt tricks in Joel Spolsky's article probably aren't widely known. I didn't know about the wildcard expansion, so you can use tab-completion by typing the last characters of a name, or the middle.

I find the windows prompt quaint, but consider it dead end, and only learn such trivia when it seems that it will reduce my interactiion with it. Given the opportunity I believe it is better to install cygwin to get a more expression shell and less annoying environment, and skills and scripts that you'll be able to use elsewhere.

2
  • 1. Thanks - I wasn't aware of F7 and it looks useful - if only to reduce the horrible confusing jumble that is Windows console history navigation. 2. the doskey command doesn't work for me, likely because I'm using PowerShell and not cmd.exe (see OP). 3. The link you provided is not for Joel Spolsky, but his co-conspirator Jeff Atwood. Still an awesome dude, and the information is useful even if pertaining only to cmd.exe - so thanks again. 4. Lastly, regarding cygwin, see last paragraph of OP. \n\n It looks like I'm left with no alternative but to roll my own console.
    – Guss
    Commented Jul 12, 2013 at 9:19
  • That's so gorgeously retro! I love it. So glad they bothered to keep it in PS.
    – scipilot
    Commented Aug 24, 2017 at 8:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .