简体   繁体   中英

Why do Windows consoles lose command-line history (up arrow) after a time?

It seems random: after some amount of time / usage of any console I use on Windows Vista, I lose the ability to use the command-line history (or command buffer, if you prefer...), and hitting the up or down arrow no longer does anything at all, meaning there are essentially 0 items available in the history.

I've seen this behavior in Cygwin, CMD, and PowerShell.

Sometimes this behavior applies to processes running inside the console - Python, MySQL client, etc.

Is this an inherent problem in Windows? A setting somewhere? Are there other consoles that don't have this problem?

ps - if you think this is non-programming related, please see other questions regarding programming tools, for instance, nearly anything tagged "eclipse" which deal with how to use the IDE rather than programming for it.

Two ways, I think.

1) Far Manager is a good choice for a shell. Even if you don't need it's all power (file/archive management, editor, huge number of plugins, macros), you can use it as a wrapper/executor for cmd, tcc, sh or any other program, shell or command processor.

For example

  • take 3 files from distrib (Far2 or Far3): far.exe, fareng.lng and (optionally) fareng.hlp
  • run "far.exe /w", press Ctrl+B Ctrl+O
  • type any command in command line: dir, set, notepad, putty, an so on...

History will be preserved between sessions, you may access it by Alt+F8 (press Ctrl+Alt+F to switch history into filter mode).

Far 2.0 stores history (and settings) in the registry.

Far 3.0 stores history in SQLite database, and may be portable.

Also, Far can autocomplete commands from history or files/paths from disk. And there is plugin (YAC) which can autocomplete from aliases and so on...

2) I have found recently interesting project: Clink - Bringing Bash's powerful command line editing to Microsoft Windows' cmd.exe

  • Powerful Bash-like line editing from GNU's Readline library. Read more on Readline's keyboard shortcuts.
  • Superior path completion ( TAB ).
  • Paste from clipboard ( Ctrl-V ).
  • Support for the completion of executables/commands, and environment variables.
  • Undo/Redo ( Ctrl-_ or Ctrl-X , Ctrl-U )
  • Improved command line history.
    • Persists across sessions.
    • Searchable ( Ctrl-R and Ctrl-S ).
    • History expansion (eg !!, !, and !$).
  • Scriptable completion using Lua.

It seems a young but budding project, so I had created an option for use it in my Console Emulator .

Windows“DOS框”中的历史缓冲区的最大大小 - 左上角图标,右键单击,选择属性,您将看到命令历史的缓冲区大小默认为50。

Check your Scroll Lock key. DOH! I hit mine by accident so that up and down arrow just scrolled the window.

In PowerShell you can control the maximum size of history via the MaximumHistoryCount automatic variable (default is 64)

PS > $MaximumHistoryCount

I have encountered this issue for years. Mostly with python used from the command line in a bash shell. After some experimentation today when this re-occurred for the 1000th time (more or less) I was able to fix the issue by setting the Command History for the shell to have buffer size of 999 and Number of Buffers to 999 (I picked the largest possible setting for each, it's possible any setting would have worked other than what I was prior, which was 50/50). To access the Command history, right click in in the upper left corner of the shell window, select Properties, then the Options tab. I'm not sure the ramifications of these settings to performance of the shell, but thus far nothing else seems affected and command line history is once again working for me in the same shell it previously wasn't working in. I realize this answer is similar to the answer by steve-gilham, but perhaps gives a bit more background.

Admittedly, my answer involves 1 experiment that worked.

All history in console is provided by Doskey . It also is always available for all character-based, interactive programs (such as program debuggers or file transfer programs), and maintains a command history buffer and macros for each program that it starts.

In DOS era, one can remember softwares that after execution cleared the history. I'm not sure they were bugs, expected behavior, or the mix of two. But some Doskey bugs were know as you can see in the end of this link .

I suggest anyone who get this problem to try Doskey command options and see if (s)he can correct it.

I've never experienced this, but I wonder if pressing the key combination ALT+F7 , which clears command history according to DOSKEY /? , might be the cause.

If "suddenly" all history is gone, maybe a new cmd-shell starts? Test:

C:\Users\Guest\>doskey /history
 …long history…
C:\Users\Guest\>cmd
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.
C:\Users\Guest\>doskey /history
 …no history…
C:\Users\Guest\>exit
C:\Users\Guest\>doskey /history
 …old long history…

Of cause in this example you'l see the new cmd-Start but if you try out this:

doskey /history
  …history list…
cmd echo /k
doskey /history
  …no list !

You don't see

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

But a new CMD-Shell was started. So "exit" may help in this case!

This happened to me when I'm in a Rez environment on Windows 10.

As per @user1769064 's answer above setting the buffers to 999 and restarting the shell with a new environment the issue went away. There's probably a lower number than 999 with which it works as well, but haven't tried it.

  1. right-click on the top-left corner of the Windows Command Prompt on the C:\\ icon.
  2. Choose Properties
  3. In the Options tab, find Command History setting
  4. Change the Buffer size to 999
  5. Close the terminal and relaunch it again

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM