Ctrl+c not working in integrated terminal which uses Powershell - 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.

Related

How to run edit, reload commands of an ahk file via a windows shortcut or command line?

I want to be able to launch the script file .ahk in edit mode (in sublime), reload, pause etc script via a windows shortcut.
Basically I want some of the following commands, (edit, reload):
Is there a flag like /e or --edit --reload that is specified in the windows shortcut editor dialog & command line so as to launch the any of above commands:
Thanks
There are built-in commands for both Reload and Edit
You can just assign hotkeys to the commands, to execute them directly inside of the code:
^!r::Reload ; Ctrl+Alt+R
^!e::Edit ; Ctrl+Alt+E

How to clear terminal command history in VS code?

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.

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.

How to exit Command Prompt after launching VSCode

Here are the steps to reproduce the problem:
Open Command Prompt. ( cmd )
Run code . to launching VSCode.
Type exit and hit Enter in the Command Prompt.
Then the Command Prompt is just paused. I have to wait VSCode exit to let Command Prompt window closed.
Does anyone know why? How can I close Command Prompt window without exiting VSCode?
I found a solution here: https://github.com/Microsoft/vscode/issues/6608. It involves changing the code.cmd file (usually found under "C:\Users\yourUsername\AppData\Local\Programs\Microsoft VS Code\bin").
Changing the fifth line in that file from
call "%~dp0..\Code.exe" "%~dp0..\resources\app\out\cli.js" %*
to
start "" "%~dp0..\Code.exe" "%~dp0..\resources\app\out\cli.js" %*
will make the cmd window close right away. But that change will apparently break some other things (specifically the --wait flag), so I figure it's wiser to leave the code.cmd file alone.
Instead I made a copy ("codeNoCommandPrompt.cmd") right beside it and changed the line in there. That works fine for my usecase, namely having VS Code start on a specific folder alongside a bunch of other programms via a script.
I just tested it (using the latest VSCode 1.24.1), and it does work: the CMD shell session closes immediately when typing "exit".
Try calling the code.cmd script with its full path to see if the issue persists:
"C:\Program Files\Microsoft VS Code\bin\code.cmd" .
Try also the same command after having simplified the PATH (for testing)
set PATH=C:\Program Files\Microsoft VS Code\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\
code .
Simply use this
code . && exit
Check your PATH variable, maybe you have duplicate entries for VS Code
Go System Properties -> Envieronment variables -> select PATH variable then click Edit.
Remove "C:**\Microsoft VS Code".
Leave intact "C:**\Microsoft VS Code\bin"
This happens when you reinstall vscode with "add to PATH" checked

Ho to abort a command in Ubuntu(16.04), where CTRL+C I am already using for copying?

I am currently using CTRL+C and CTRL+V for copy and paste respectively in Ubuntu (16.04). So in this case, how can I terminate /abort a command in Terminal.
NOTE: The control+C does not work for me neither quit() or Exit().
Please advise.
The default terminal (gnome-terminal) uses Ctrl+Shift+c and Ctrl+Shift+v for exactly this reason. If you are in a bash prompt (unclear from context here), you can quit by writing exit.