How to clear terminal command history in VS code? - powershell

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?

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.

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

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.

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.

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

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:
Press the Windows key + R at the same time to launch the Run dialog.
Copy and paste the following path: %userprofile%\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadline
Press Enter.
The File Explorer will open the specified path.
Edit the ConsoleHost_history.txt and Visual Studio Code Host_history.txt files manually to remove specific lines or all the content.

Related

How to clear the entire terminal (PowerShell)

I had an issue. Using the clear or cls command in powershell clears only the visible portion of the terminal,I would like to know how to clear the entire terminal?
I use VSCode by the way.
To also clear the scrollback buffer, not just the visible portion of the terminal in Visual Studio Code's integrated terminal, use one of the following methods:
Use the command palette:
Press Ctrl+Shift+P and type tclear to match the Terminal: Clear command and press Enter
Use the integrated terminal's context menu:
Right-click in the terminal and select Clear from the context menu.
On Windows, you may have to enable the integrated terminal's context menu first, given that by default right-clicking pastes text from the clipboard:
Open the settings (Ctrl+,) and change setting terminal.integrated.rightClickBehavior to either default or selectWord (the latter selects the word under the cursor before showing the context menu).
Use a keyboard shortcut from inside the integrated terminal (current as of v1.71 of VSCode):
On macOS, a shortcut exists by default: Cmd+K
On Linux and Windows, you can define an analogous custom key binding, Ctrl+K, as follows, by directly editing file keybindings.json (command Preferences: Open Keyboard Shortcuts (JSON) from the command palette), and placing the following object inside the existing array ([ ... ]):
{
"key": "ctrl+k",
"command": "workbench.action.terminal.clear",
"when": "terminalFocus && terminalHasBeenCreated || terminalFocus && terminalProcessSupported"
}
Using a command you can invoke from a shell in the integrated terminal:
Note: A truly cross-platform solution would require executing the VSCode-internal workbench.action.terminal.clear command from a shell, but I don't know how to do that / if it is possible at all - do tell us if you know.
Linux (at least as observed on Ubuntu):
Use the standard clear utility (/usr/bin/clear), which also clears the scrollback buffer.
From PowerShell, you may also use Clear-Host or its built-in alias, cls.
By contrast, [Console]::Clear() does NOT clear the scrollback buffer and clear just one screenful.
macOS:
Unfortunately, neither /usr/bin/clear nor PowerShell's Clear-Host (cls) nor .NET's [Console]::Clear() clear the scrollback buffer - they all clear just one screenful.
Print the following ANSI control sequence: '\e[2J\e[3J\e[H' (\e represents the ESC char. (0x1b, 27); e.g., from bash: printf '\e[2J\e[3J\e[H'; from PowerShell: "`e[2J`e[3J`e[H"
You can easily wrap this call in a shell script for use from any shell: create a file named, say, cclear, in a directory listed in your system's PATH variable, then make it executable with chmod a+x; then save the following content to it:
#!/bin/bash
# Clears the terminal screen *and the scrollback buffer*.
# (Needed only on macOS, where /usr/bin/clear doesn't do the latter.)
printf '\e[2J\e[3J\e[H'
Windows:
NO solution that I'm aware of: cmd.exe's internal cls command and PowerShell's internal Clear-Host command clear only one screenful in the integrated terminal (not also the scrollback buffer - even though they also do the latter in a regular console window and in Windows Terminal).
Unfortunately, the escape sequence that works on macOS ("`e[2J`e[3J`e[H" or, for Windows PowerShell, "$([char]27)[2J$([char]27)[3J$([char]27)[H") is not effective: on Windows it just clears one screenful.
(By contrast, all of these methods do also clear the scrollback buffer in regular console windows and Windows Terminal.)
right click on the powershell button,
then select clear,
when you are at the command window, type "clear" command, to clear the terminal window.

VSC Command Log

Trying to figure out how to create a key binding for a specific command.
I can see the command in the pallete but it has no bindings so I can't see it in the keybindings.json.
Is there any way I can LOG all commands being executed within VSC so that I can perform the command and see the command name in the log?
Edit:
To make it clear:
I want general method to know how I can find what commands were issued along with their command IDs.
I already know I find some commands in the default keybindings file and that's NOT a general way to find the command you have run. It doesn't list all command not does it explain what they each are and you can't use it to verify that it is the command you executed.
You can create binding using Keyboard Shortcuts as well (Preferences: Open Keyboard Shortcuts command). You can search by command name and and even check its corresponding id:
Second option is that you can enable tracing and see executed commands in output panel (it looks like it doesn't log commands executed from command palette though):
Set log level to trace.
You can either pass --log argument when starting VS Code like: code --log trace (doesn't work for me for some reason) or execute Developer: Set Log Level... from command palette:
After that, you should see executed commands in output panel for Window process.
The "Toggle Line Comment" command's id is editor.action.commentLine.
You can find all the command ids by using the search feature in the Keyboard Shortcuts Editor (on macOS you can open it via Application Menu > Preferences > Keyboard Shortcuts or using the shortcut Cmd+K Cmd+S).
In the keybindings.json you can trigger the autocomplete help (i.e. Ctrl+Space) and search all available commands.

Ctrl+c not working in integrated terminal which uses Powershell

I'm using Powershell in the integrated terminal by adding the following line to the settings.json file.
"terminal.integrated.shell.windows": "C:\\WINDOWS\\system32\\WindowsPowershell\\v1.0\\powershell.exe",
It works very well, but usually, when I'm in Powershell, typing ctrl+c cancels what I had typed and opens a new line.
But in the integrated terminal it just prints ^C.
Is there a way to fix it or find an alternative method to achieve this?
Thanks
This is with VSCode and not necessarily with the PowerShell Extension. You can see this by just using the default cmd.exe terminal, CTRL+C does nothing. It does not print the ^C at all, and creates no new line.
If you want this to work as expected in the normal command prompt or PowerShell.exe you will need to submit an issue to VSCode repository and request it.
I would expect this is all tied to the keybindings.json file. I went through that file but could not find a command available to the same function that occurs in the full command prompt or console. So this will likely need a new command added for VSCode.
If you search through the keybindings file you can see the terminal has that key CTRL+C bound to copySelection when terminalFocus && terminalTextSelected. This is why the ^C is being output, and no new line is being added.
A workaround:
Pressing Esc will erase the line back to the beginning.

Command to clear the Git Bash screen, including output buffer

Is there any command in Git, that clear the screen. for example in window command line after execute a lot of code, if you type cls, then it will clear all the previous code. so i want the same type of functionality in Git. so can anybody will tell me the command name.
Actually you are looking for a Unix user environment command
clear
or you can use the keyboard shortcut
ctrl+l
http://en.wikipedia.org/wiki/Clear_(Unix)
To clear entire command history in Git Bash.
history -c
try using reset command, it will absolutely clean your screen but you will still have access to previous commands
reset
Neither clear nor history -c does the work actually.
Scroll up, all commands will be visible.
Solution:
If you are in Windows 10, and using mintty 2.7.9 (or above ?) for git bash,
use Alt + F8 ... this will work.
Best of luck.
Happy coding.
Reference: here (Perhaps it didn't work for Windows 7)
Neither clear nor history -c was clearing the history permanently.
All commands will be visible when scrolled up.
So, I solved the issue by:
In my instance the path for bash history was:
/c/Users/<your_username>/.bash_history
I removed the file by the following commands:
rm ~/.bash_history
After that, I restarted the terminal. the commands were gone.
CTRL + L
search for more shortcuts in: here
Another option is modify (or create in your user folder) your .bash_profile and add this:
alias cls='clear';
With this you can clear the bash with a 'Windows' command.
At the moment I use
clear;reset;clear
(in one line) and it sort of works (git version 2.32.0.windows.1).
Most times clr, clear and cls doesn't work use ctrl c to continue writing commands
use clear only without git command
" clear "

Is there any way to automatically save command history to a file in cmd.exe, similar to bash's bash_history?

I am aware that one can do
doskey /history
to save the command history at a particular point in time, but I wonder if there's a way to proactively save command history to a file, as the commands are being issued.
Once a command prompt is closed, the history is lost, so it's easy to accidentally close a command prompt when one is done.
I'd like to be able to say something like:
log Commands.log
and then issue my commands, and have the commands be saved to Commands.log.
You could create a doskey macro to remap the EXIT command, as follows:
doskey exit=doskey/history$g$gc:\temp\commands.log$texit $1 $2
This would append the contents of your command history into a file named "c:\temp\commands.log" each time you exit the prompt by typing "exit".
CAUTION: I haven't tested potential side effects of using this with EXIT's "/B exitCode" parameters, but there shouldn't be any since you're capturing the parameters with "$1 $2" anyway.
This doesn't do exactly what you're looking for, but it does capture your command history as long as you exit using EXIT (vs. just closing the window).
More info and samples on DOSKEY macros are here:
http://technet.microsoft.com/en-us/library/bb490894.aspx
I know it is late but I can see two alternatives.
Firt clink or something more complete that include cmder
Each time you will open cmd it will be clink and it works great
As Novy has already said, try Clink; but also use the following link so it also works in admin mode.
run cmd with clink as administrator in windows to save your command history permanently