vscode: Some shells don't remember history
Does this issue occur when all extensions are disabled?: Yes
- VS Code Version:
- OS Version:
Steps to Reproduce:
- Open VS code
- Please see the below images.
When I pressed the top arrow key It shows like so. But no idea from where that CLI is coming. I would like to have npx cap open android
CLI here.
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 2
- Comments: 24 (6 by maintainers)
The SHARE_HISTORY option seems to work for me as a fix. Closing the terminal by typing “exit” also seems to work. This wasn’t necessary earlier though.
As far as I know I’m using defaults for the terminal. I just checked it with a VSCode Insiders download without any plugin or setting changes and I had the same problem there.
Tldr, for now use “exit” every time to save history, or:
open ~/.zshrc
And add:setopt SHARE_HISTORY
Shells generally have different behaviour concerning histories and VSCode simply exacerbates it by not closing them correctly, especially on Windows in particular (afaik).
Bash will generally only save the history for the last interactive (
-i
) login (--login
) shell that has been properly terminated. Otherwise theexport PROMPT_COMMAND='history -a'
workaround is required, regardless of VSCode or not. See interactive behavour in the manual.In case of ZSH there are options such as setting
SHARE_HISTORY
that may improve the experience beyond what Bash offers natively for example.To verify what goes wrong on macOS and help debugging efforts you should verify the Terminal setup in VSCode and add that information here.
Thanks that worked! Neither file existed, so I created
~/.bash_profile
and savedPROMPT_COMMAND='history -a'
in it and now the history is kept. Thanks a lot!@sfboss
export PROMPT_COMMAND='history -a'
is a documented workaround. see also my prior comment, what states that. As far as I understand, this issue ticket is about fixing the problem instead of using workarounds.A couple I found by googling “
site:github.com/microsoft/vscode/issues shell closing window history
”: #37273, #85831, #114164. All about Git Bash. Also related: vscode-docs f0876ad, which changed docs to say this is by-design, and to workaround usingexport PROMPT_COMMAND='history -a'
.Today I encountered a Stack Overflow question about a user experiencing the same issue with a Zsh shell.
I found I that I experience the same issue with a Bash shell on Ubuntu with VS Code 1.77.1 installed as a Snap. My Bash shell in the VS Code interactive terminal is an interactive, non-login shell. Opening a login sub-shell with
bash -l
makes no difference: History is not saved to the histfile when closing the VS Code window. I have not setexport PROMPT_COMMAND='history -a'
.Let me know if I should create a new issue ticket that is not specific to Windows (since this one currently is).
Loosely related in the Window Terminal repo: microsoft/terminal#7826.
This type of issue has been reported a few times and I think we’ve closed them all out, let’s track in this one. This works fine in pwsh, I believe this is because pwsh persists history to disk as you type, whereas git bash does it when the process shuts down. Something about how we’re shutting down the terminal processes doesn’t let git bash persist to disk.
Maybe the problem code is here where we kill all processes before closing the conpty pty: https://github.com/microsoft/node-pty/blob/1674722e1caf3ff4dd52438b70ed68d46af83a6d/src/windowsPtyAgent.ts#L161-L168
We tweaked what happens here a bunch to try stop processes leaking which is why we go through and kill all processes in
GetConsoleProcessList
. Maybe we could try callingnative.kill()
and then giving it some time to finish before going through and killing the process list?