7

In VS Code Powershell Terminal, you can simply press up and down arrow keys to navigate through the history of commands entered, even after a restart. However, when there are same commands entered, it will also cycle through these duplicated histories instead of just making them distinct, making it hard to find cycle back to some old history. Is there a way to clear this history entirely?

Cerlancism
  • 2,803
  • 5
  • 14
  • 20

6 Answers6

3

As a conclusion to the answers: my actual process to prevent duplicates, delete history and clear:

Set-PSReadlineOption -HistoryNoDuplicates

Remove-Item (Get-PSReadlineOption).HistorySavePath

Alt-F7

Cerlancism
  • 2,803
  • 5
  • 14
  • 20
  • Previously, I could trace the command history. After I install vs code again, I can not trace commands. All the command history is gone after I restart vs code. Do you know how I can keep the command history even I close vs code? – blackman Apr 22 '20 at 08:55
  • @blackman Is your default terminal in VS Code changed to cmd instead of powershell? cmd doesn't keep history. – Cerlancism Jun 24 '20 at 09:07
3

In Windows platform press and hold ctrl+shift+P, in the pop up window write terminal:clear, you'll get it shown, assign shortcut key (crtl+K) for example and hit enter. Now every time you want to clear the terminal you can use the hotkey you just created. Do the same in Mac but using cmd+shift+P instead.

Laurel
  • 5,965
  • 14
  • 31
  • 57
HLTC
  • 31
  • 2
3

If you are using the VS Code PowerShell terminal you can clear the entire history of the terminal or even specific lines with these steps:

  1. Press the Windows key + R at the same time to launch the Run dialog.
  2. Copy and paste the following path: %userprofile%\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadline
  3. Press Enter.
  4. The File Explorer will open the specified path.
  5. Edit the ConsoleHost_history.txt and Visual Studio Code Host_history.txt files manually to remove specific lines or all the content.
Rafael Neto
  • 1,036
  • 10
  • 16
2

Try the following command:

Set-PSReadlineOption -HistoryNoDuplicates

It sets the HistoryNoDuplicates option to True and hides duplicate histories.

You can see the value of HistoryNoDuplicates with the following command:

(Get-PSReadLineOption).HistoryNoDuplicates

If you want to set it back to False:

Set-PSReadlineOption -HistoryNoDuplicates:$false

For more information, see Set-PSReadlineOption in Microsoft Docs.

matt9
  • 683
  • 5
  • 16
1

In v1.65 there will be this command:

workbench.action.terminal.clearCommandHistory

"Clear Command History"

In v1.65 there will also be a new setting:

terminal.integrated.shellIntegration.history

"Controls the number of recently used commands to keep in the terminal command history. Set to 0 to disable terminal command history."

I suppose to clear the terminal history you could set this to 0 (100 is the default), reload (I'll test this tomorrow to see if a reload is necessary, it may not be) and then reset the limit to 100 or whatever you want.

Mark
  • 143,421
  • 24
  • 428
  • 436
0

the Cmdlet Clear-History should do what you want https://learn.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Core/Clear-History?view=powershell-6

Guenther Schmitz
  • 1,955
  • 1
  • 9
  • 23
  • Ok this actually ran into this another [problem](https://stackoverflow.com/questions/13257775/powershell-clear-history-doesnt-clear-history). But seems to work based on one of the answer by using `Remove-Item (Get-PSReadlineOption).HistorySavePath` Then `Alt-F7` – Cerlancism Nov 23 '18 at 08:17