PowerShell history of commands - powershell

I use Bash and PowerShell interchangeably, and find it quite annoying when I can't do a Ctrl+R on my PowerShell Console.
Is there a plugin/alternate command that can help me switch between Bash and PowerShell seamlessly?
Update (2018)
PowerShell now supports Ctrl + R. Please see this answer.

An alternate command is to type e.g #ls and press Tab keep pressing tab to cycle through all command history that starts with ls.
In previous versions you could type ls then F8 to match history. Keep pressing F8 to cycle through multiple matches.
Note:ls is just a placeholder in this case. Replace it with any command you want.

As of today PowerShell supports the Ctrl + R shortcut.
Simply press Ctrl + R when in the PowerShell console and start typing any part of a command you have run before.
Alternatively:
Start typing part of a command you have run before, and press or hit F8.
Keep pressing F8 to cycle through similar commands.

Take a look at PSReadline: https://github.com/lzybkr/PSReadLine
This module supports interactive history search in emacs mode and you can bind Ctrl+R to ReverseHistorySearch in Windows mode if you prefer.
The long term goal of PSReadline is to make it much easier to switch from bash to PowerShell w.r.t. command line editing while providing a PowerShell experience, e.g. tab completion.

Related

VSCode Terminal - Clear Command Prompt

I'm running on Linux. I have the issue both in bash and pwsh shells.
How do I clear the current command prompt in the VS Code terminal?
For example, say you have copied and pasted a very long string into the terminal.
How do you clear what you just pasted?
The only way I know of for getting back to a an empty command prompt involves hitting backspace until you have deleted every character.
Is there any short cut for getting back to an empty prompt?
Thanks!
How do you clear what you just pasted?
There is no command to clear what you have already pasted. You can do Ctrl + C or Ctrl+D to get the next promt.
Now if you want a short-cut to clear command which we use in terminal, its Cmd + k for Mac OS
CTRL + shift + p, then write clear. You can use the clear command in bash terminal too.
Use Console.Clear() in code.
or use 'cls' cmd on console

Change Integrated Terminal title in vscode through Powershell script

I'm using VScode for running my same powershell script with different parameters in different powershell.
Is there any way I can name the terminal title through powershell script so that I can differentiate for what parameters I've run in which terminal ?
Instead of 1:PowerShell and 2:PowerShell I need alpha and beta from parameteres of powershell,
I've checked this answer Change Integrated Terminal title in vscode, but it requires manual intervention.
The numbers will remain but you can change the title by first opening command palette (Ctrl + Shift + P) and then search for Terminal: Rename.
Then you can type whatever name you want to give.

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.

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.

Use CTRL + D to exit and CTRL + L to cls in Powershell console

I am trying to make
CTRL + D - exit Powershell console
and
CTRL + L - clear the screen
like in bash.
So far, I have seen that we can define
function ^D {exit}
but that means I have to do CTRL+D and then hit enter for it to work.
Also, it doesn't even let me define
function ^L {exit}
Is there anyway to add these key bindings in the default Powershell console?
Old question, but with PowerShell 5.1 and PowerShell Core 6.x and later:
Set-PSReadlineKeyHandler -Key ctrl+d -Function ViExit
There is a new library PSReadline for Powershell v3.0 that emulates the GNU Bash tab completion and key bindings. Even CTRL + R for reverse incremental search works. Exactly what I wanted.
If you don't mind relying on an external program, you could do the following with AutoHotKey:
#IfWinActive ahk_class ConsoleWindowClass
^L::SendInput , {Esc}cls{Enter}
^D::SendInput , {Esc}exit{Enter}
#IfWinActive
The above will work with the PowerShell or CMD console. Otherwise the only thing I can think of would be to work up some P/Invoke magic with SetWindowsHookEx.
Edit: Fixed AutoHotkey script to pass through the shortcut keys to other programs.
The keybindings are controlled by PSReadLine. PSReadLine's default edit mode is Windows style, where Ctrl-D is unbound.
Set your edit mode to Emacs
Set-PSReadlineOption -EditMode Emacs
or bound the key
Set-PSReadLineKeyHandler -Key 'Ctrl+d' -Function DeleteCharOrExit
There is also a PowerShell snapin called PSEventing which will allow you to do this (see the demo on the front page:
http://pseventing.codeplex.com/releases/view/66587
# clear screen in response to ctrl+L, unix style
register-hotkeyevent "ctrl+L" -action { cls; write-host -nonewline (prompt) }
You can set your PSReadline to emacs mode, it will not only exit with ^D, you will be able to go to beginning of line with ^A, end of the line with ^E
Include this in your profile:
Set-PSReadlineOption -EditMode Emacs
I'm using cmder which uses ConEmu, find profile.ps1 with <appdir>/vendor/ for that case and you can add into that file.
Otherwise you can add to default locations where powershell loads it. One of tutorials HERE.