Keep cmd.exe command history between sessions?

79

20

Is there a way to keep the cmd command history between sessions?

firefusion

Posted 2011-03-15T13:34:19.993

Reputation: 1 703

Answers

23

Switch to using PowerShell, and follow the instructions at the following site to enable history:

http://blogs.msdn.com/b/powershell/archive/2006/07/01/perserving-command-history-across-sessions.aspx

Alternatively, in cmd.exe, you can use "doskey /history" at the end of your session to show what you typed in that session, but theres no way to really load it into the next session.

Hyppy

Posted 2011-03-15T13:34:19.993

Reputation: 3 636

Should be noted that some commands that work in cmd don't work the same in PowerShell e.g. mvn install `-Dmaven.test.skip=true would requires a backtick as shown before the dash – Drenai – 2017-07-13T09:39:15.317

6I hestitated to +1 this because "Switch to using PowerShell" is not necessarily as easy as it sounds depending on what sort of stuff you're using the console for, but the info is accurate soooooooooo ;) – Shinrai – 2011-03-15T14:23:03.393

2Agreed. I hesitated to put it down but it's the only real answer I can find. I tried to get something similar going on a workstation I had before, but there just isn't a persistent history mechanism available for cmd.exe :-( – Hyppy – 2011-03-15T14:27:15.887

Note that this will keep a list of commands you typed, but it doe NOT alter the list of commands that will come up when you hit the up key on the keyboard. It doesn't affect the "doskey" functionality, in other words. – Mark – 2011-04-13T19:06:30.810

70

I've found 2 ways, neither of which require switching to PowerShell.

  1. Install Clink, which enhances cmd.exe with persistent history and much more. Just install it and then open cmd as normal.

  2. Install TCC/LE free version, which is a separate program, again providing an enhanced version of cmd.exe.

sparrowt

Posted 2011-03-15T13:34:19.993

Reputation: 2 013

@LoSauer, Use the portable version. clink inject. It only affects the current session. – Pacerier – 2015-08-23T22:03:19.230

@sparrowt, Is there a free version of TCC/LE that last more then 30 days? – Pacerier – 2015-08-23T22:04:10.863

3This should be the accepted answer. Because this is what asked by the question..!! This is best rather than using any other alternative application. – Samitha Chathuranga – 2015-11-15T09:02:47.333

It is available in Chocolatey!!!! – Brethlosze – 2017-06-21T03:23:00.490

1Thank you for this. Clink is fantastic and works seamlessly with ConEmu. – miCRoSCoPiCeaRthLinG – 2017-11-13T13:56:32.240

Clink is excellent. It took me a short while to setup a couple of useful shortcuts. – sancho.s Reinstate Monica – 2018-06-18T13:23:10.223

Holy goodness Clink is awesome! Nice find. Also, works with JetBrains IDE's embedded command window – RedactedProfile – 2018-11-15T18:17:29.137

6I am a bit apprehensive against system-wide replacing of cmd.exe but its a good answer, and clink supports an injection mode as well. – Lorenz Lo Sauer – 2012-09-09T18:10:52.980

1If you don't want to install it system wide, you can launch it via a shortcut to clink.bat. – William Bettridge-Radford – 2012-11-13T12:53:06.303

On my German Windows, some keyboard shortcuts in the cmd window did not work anymore after installing CLink. – Uwe Keim – 2013-04-22T08:27:50.947

4Clink is brilliant. Replaced cmd with it, been no problems, very happy with it. – Colonel Panic – 2013-07-24T14:38:14.690

12

Saving history is a small workflow - here's a less "heavy" way to do this (no external libs).

Create a bat/cmd file to set up your history, in this case I called it MyEnvironment.cmd:

doskey save=doskey /history $g$g C:\CmdHistory.log
doskey quit=doskey /history $g$g C:\CmdHistory.log $T exit
doskey history=find /I "$*" C:\CmdHistory.log
cls

Then run this from "Start->Run" (you can also setup an alias for this too):

cmd.exe /K C:\MyEnvironment.cmd

Every time I'm closing a session I hit "quit" - or if I'm afraid of losing history mid-session I hit "save". If I want to grep for something in history, I just hit "history KEYWORD".


Per @dave_thompson_085 's comment, the AutoRun feature works well if you don't want to use the /K switch. If you set up the Registry key correctly, the .cmd or .bat does not need to be in %AppData%, it can be in the same location it already is.

If you do use the %AppData% location, be aware that cmd will probably look for your batch file in the "Roaming" folder (instead of the AppData root).

More info on the AutoRun CMD feature: https://superuser.com/a/302553/333316

Coruscate5

Posted 2011-03-15T13:34:19.993

Reputation: 844

What $g and $t are for? – None – 2017-07-03T17:29:07.340

@Chinggis6 $g inserts a >, and $t is a command separator. – Superole – 2017-08-25T08:19:45.237

@Superole ah ok, greater than and terminate, but why not to use > and && characters directly in the doskey alias? – None – 2017-08-25T08:58:24.090

@Chinggis6 because that would break the command creating the alias. Then the command-interpreter would assume that you meant: first execute doskey quit=doskey /history and save the output of the macro creation in a logfile (either nothing or an errormessage); next close the window (discarding everything). -You could escape those characters with a ^ instead though, I think. Like ^>^> and ^& (...and yes, you only need one ampersand) – Superole – 2017-09-04T12:55:49.323

1

Instead of explicit /k can use AutoRun https://superuser.com/a/302553/333316

– dave_thompson_085 – 2018-06-03T20:45:32.640

2

I use cygwin. It also provides some others functionalities that Linux has but Windows not.

Harun

Posted 2011-03-15T13:34:19.993

Reputation: 129