Is there a way to keep the cmd command history between sessions?
4 Answers
I've found 2 ways, neither of which require switching to PowerShell.
-
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 Sep 09 '12 at 18:10
-
1If you don't want to install it system wide, you can launch it via a shortcut to `clink.bat`. – William Bettridge-Radford Nov 13 '12 at 12:53
-
On my German Windows, some keyboard shortcuts in the `cmd` window did not work anymore after installing CLink. – Uwe Keim Apr 22 '13 at 08:27
-
5Clink is brilliant. Replaced `cmd` with it, been no problems, very happy with it. – Colonel Panic Jul 24 '13 at 14:38
-
@LoSauer, Use the portable version. `clink inject`. It only affects the current session. – Pacerier Aug 23 '15 at 22:03
-
@sparrowt, Is there a free version of TCC/LE that last more then 30 days? – Pacerier Aug 23 '15 at 22:04
-
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 Nov 15 '15 at 09:02
-
It is available in Chocolatey!!!! – Brethlosze Jun 21 '17 at 03:23
-
1Thank you for this. Clink is fantastic and works seamlessly with ConEmu. – miCRoSCoPiCeaRthLinG Nov 13 '17 at 13:56
-
Clink is excellent. It took me a short while to setup a couple of useful shortcuts. – sancho.s ReinstateMonicaCellio Jun 18 '18 at 13:23
-
Holy goodness Clink is awesome! Nice find. Also, works with JetBrains IDE's embedded command window – RedactedProfile Nov 15 '18 at 18:17
-
@Pacerier : Sure. TCC/LE is not limited to 30 days. My guess is that you downloaded one of the other TC-related demos. Make sure you chose "TCC/LE". (Although, at the time of this writing, TCC/LE has fallen behind some of the other TC releases.) – TOOGAM Jun 10 '20 at 13:07
-
I used TCC/LE back in the day - I never encountered any incompatibilites between it and the Windows command prompt; – timbck2 Jun 29 '23 at 20:10
Switch to using PowerShell, and follow the instructions at the following site to enable history:
https://devblogs.microsoft.com/powershell/perserving-command-history-across-sessions/ (archived)
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.
- 4,089
- 8
- 24
- 37
- 3,712
- 20
- 18
-
12I 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 Mar 15 '11 at 14:23
-
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 Mar 15 '11 at 14:27
-
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 Apr 13 '11 at 19:06
-
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 Jul 13 '17 at 09:39
-
The question is cmd not PowerShell. You shuld start to answer the question and then follow up with alternatives. – Morten Apr 03 '20 at 19:00
-
Yes, powershell is not a great option if, like me, you are constantly on different servers, which may or may not have powershell correctly installed or updated and on which I may or may not have install rights. cmd is always there and always works right away. However, you may have just convinced me to embrace PS anyway :) – Daniel Williams Nov 23 '21 at 16:21
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
Update (Oct. 2020) on Autorun: I've used this feature now for years, but would like to point out many programs that rely on CMD to run background commands (Visual Studio, Win32 apps), get very confused if anything runs before the passed CMD command (e.g. VS developer prompt). If this causes pain, switch to the /K method.
- 994
- 10
- 14
-
1
-
1
-
@Superole ah ok, **g**reater than and **t**erminate, but why not to use `>` and `&&` characters directly in the doskey alias? – Aug 25 '17 at 08:58
-
@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 Sep 04 '17 at 12:55
-
1Instead of explicit `/k` can use `AutoRun` https://superuser.com/a/302553/333316 – dave_thompson_085 Jun 03 '18 at 20:45
-
As of [Windows 10](https://en.wikipedia.org/wiki/Windows_10), apparently [bash](https://en.wikipedia.org/wiki/Bash_(Unix_shell)) is an option, [InfoWorld: ...Bash on Windows](https://www.infoworld.com/article/3178631/discover-the-power-of-bash-on-windows.html). I haven't bothered to configure and use it though. – ScottWelker Dec 31 '21 at 01:25