Copied-and-pasted tab characters not recognized by Powershell - powershell

Powershell ignores tab characters in copied-and-pasted code snippets. This is annoying because I like to write my Python code with single tabs instead of quadruple spaces, and in testing I frequently copy and paste snippets in to test them.
So far I've been using other terminals like Cygwin that properly address pasted tabs, but it would be more convenient for me to use Powershell. How can it be made to address these pasted tabs correctly?
The MWE is to copy and paste anything with a tab into Powershell. For example,
test
appears in Powershell as
>>> test
with no indentation. I've copied and pasted from both Notepad and Notepad++ with the same effect; I'm not using any formatted word processors or anything like that.

Caveat: The next section only applies to PowerShell's own interactive prompt. Different rules may apply to external programs launched from PowerShell that present their own interactive prompt, such as python and, preferably, ipython - see the bottom section for considerations specific to these programs.
Preserving tabs on pasting directly into PowerShell's interactive prompt:
Note: Strictly speaking, when it does work, tabs are converted to 4 spaces each on pasting.
Only works when all of the following prerequisites are met:
When running PowerShell (Core) 7+ (as opposed to Windows PowerShell)
When pasting via Ctrl-V rather than via right-click (the latter emulates typing and therefore triggers tab completion).
When running in one of the following console environments:
Regular console window (conhost.exe)
Windows Terminal
Note:
Does not work in Visual Studio Code's integrated terminal (which seemingly always emulates typing in PowerShell).
Haven't tried third-party consoles such as Cygwin and ConEmu.
Notably, this categorically excludes Windows PowerShell (where a tab ends up as ^I).
Preserving tabs on pasting into the interactive python / ipython REPL, from PowerShell or cmd.exe:
Note:
ipython provides a superior REPL experience compared to python.
Unlike the above, the following also applies to launching from Windows PowerShell, as well as from cmd.exe.
When it works, pasted tabs are converted to 4 spaces, except where noted otherwise.
ipython
Regular console windows (conhost.exe):
Works, with Ctrl-V only.
Windows Terminal:
Works, with Ctrl-V only.
Visual Studio Code's integrated terminal:
Does NOT work (strips tabs).
python
Regular console windows (conhost.exe):
Works, with both Ctrl-V and right-click, as long as Filter clipboard contents on paste is turned OFF on the Options tab of the console window's Properties dialog; pastes actual tabs.
Windows Terminal:
Works, with right-click only(!); pastes actual tabs.
Visual Studio Code's integrated terminal:
Works.

The only way I was able to make this work was by using Windows Terminal. Which is a new terminal application built by Microsoft that supports a bunch of new features.
I don't know the technical reasons why, but it appears it supports pasting both tabs and spaces appropriately, without loss or conversion into the Python CLI REPL.
I tested this using Windows Terminal while running Windows PowerShell, PowerShell Core 7+ and Command Prompt, while running the Python CLI and all supported pasting tabs.
You can install Windows Terminal through a number of sources...Windows Store, Binary download, WinGet, Choco, etc.
Here's the github repo with instructions on various ways to install.
https://github.com/microsoft/terminal

In general, control-v is better for pasting characters than right-click. Note control-v has to be bound to psreadline's paste function (so no emacs mode). Here's an example with an a and an em dash (0x2013). Right click will only paste the a.
a–

Related

PowerShell console in Visual Studio Code: How to copy-paste?

I cannot Copy-Paste from the Visual Studio Code console. In ISE one can copy-paste some of the output, but it does not seem to be possible in Visual Studio Code Terminal. How can I copy-paste the output from the console when running PowerShell commands? I have the PowerShell Extension.
Ctrl+C and Ctrl+V for copying / pasting work as-is in Visual Studio Code's integrated terminal.
By contrast, right-click behavior is configurable:
On Windows, the default behavior is to copy, if text is currently selected, and paste otherwise - as in regular console windows.
To get the same behavior as in the Windows PowerShell ISE, i.e. to instead show a shortcut menu, which contains Copy and Paste commands, add the following line to your settings.json file (before the closing }):
"terminal.integrated.rightClickBehavior": "default",
Alternatively, use the settings GUI (press Ctrl+,):
Note:
The screenshot was taken on macOS, where selectWord is the default setting; on Windows, it is copyPaste, with the behavior as described above.
Also note the GUI's convenient search feature: typing right click in the search field was sufficient to locate the relevant setting.

Dedicated Command History panel in VS Code

I'd find it convenient to be able to quickly recall past commands issued in the terminal. In Matlab one can simply select any number of those from the Command HIstory panel, and reissue them in the Terminal all at once:
In VS Code (on Windows), I know there is a command to pull up the log text file:
(Get-PSReadlineOption).HistorySavePath
But I find this extremely cumbersome as a solution to call up multiple lines at a time. Is there an VS Code addon that creates a Matlab-like Command History panel with timestamped commands (didn't find any searching myself)? Or is such feedback taken into consideration by Microsoft?
See v1.70 release notes:
Triggering the Terminal: Run Recent Command... will bring up a QuickPick panel of recent terminal commands in which you can search, fuzzy or not, through the recent commands.
There are some examples of commands for going to the next item in the list, for example.
See the supported shells and OS's mentioned below. I believe it is still accurate. Git Bash on Windows doesn't work with this new recent command functionality, but powershell does. Support for MacOS and linux is stronger: bash, powershell and zsh.
And see v1.69 release notes: run recent command:
Some other functionality of the command:
In the current session section, there is a clipboard icon in the right
of the Quick Pick that will open the command output in an editor. Alt
can be held to write the text to the terminal without running it. The
amount of history stored in the previous session section is determined
by the terminal.integrated.shellIntegration.history setting.
There is currently no keybinding assigned by default but it can be
hooked up to Ctrl+Space for example with the
following keybinding:
{
"key": "ctrl+space", // whatever keybinding you want
"command": "workbench.action.terminal.runRecentCommand",
"when": "terminalFocus"
}
This might help (coming to v1.64):
Terminal shell integration
The terminal now features experimental opt-in shell integration which
allows VS Code to gain insights on what is going on within the
terminal as it was previously a black box. When enabled using
"terminal.integrated.enableShellIntegration": true, arguments to run
a shell integration script will be injected into your terminal profile
if possible. The script itself mostly just injects invisible sequences
into your prompt, providing us with information like where the prompt,
command and command output is, what the current working directory
(cwd) is for each command and the exit code of each command.
Shell integration enables the following new features:
Run recent command: Since we know what commands are run, we have
exposed a command that allows you to view and run them again in a
quick pick.
Developer: Run Recent Command run this command from the Command Palette
The current shells supported are pwsh for Windows and pwsh, bash and
zsh for Linux and macOS.
from release notes: terminal shell integration
Although I switched to powerShell on W11 to test this feature - which should be supported - I can't get it to work just now. I wanted to see if you could pick multiple command entries in the QuickPick to run a series of them, but I doubt you can. But at least you get a nice list of recent commands.

Terminal vs PowerShell

I am been currently studying AWS, and my online instructor uses "Windows PowerShell", however, I have always used "Hyper Terminal" for MERN stack. What is the difference between the two, and what sets this two apart from the Command Prompt?
Here is the simple answer:
PowerShell Preview replaced PowerShell, and PowerShell replaced Command Prompt (CMD).
Windows Terminal is an application that simply provides tabs to open multiple instances of PowerShell (Preview or legacy) and Command Prompt, as well as Azure cloud shell.
In Linux. you'll often hear the terms 'terminal' and 'console' used interchangeably, but they mean the same in Linux as they do in Windows. That is, console is the Linux alternative to the DOS style command prompt.
I am not familiar with Hyper Terminal but I see there are terminal emulators online that go by that name therefore I assume you may be using a third party terminal emulator.
PowerShell is a newer shell and also happens to be an object-oriented scripting language.
As far as Windows shells go, I can use PowerShell to do anything I used to do with CMD. Generally speaking, I would say PowerShell really shines when it comes to Windows System administration which is my primary use for it and it saves me a great deal of time.
As far as terminal emulators go, I feel the default terminals for both PowerShell and CMD are pretty much the same and are very lacking compared to 3rd party offerings or even the default terminal that comes in any modern Linux OS.
For a free "Microsoft" alternative to replace your default terminal, I would recommend the open-source "Windows Terminal" app. The addition of tabs, panes, zoom function, easy to read text, and easy ways to start different shells (such and CMD or PowerShell) or even start a WSL session with your choice of Linux OS is a major improvement to the default Windows terminal app.

Using Powershell ISE's intellisense in other terminal

I'm a big fan of powershell ISE as a terminal. It has intellisense and syntax highlighting built in the shell and I just use it instead of the regular shell for everyday commands.
I would like to use Visual Studio Code instead, because the editor is much more powerfull and has support for a whole lot of languages and format. However the integrated shell is you basic Powershell terminal : no intellisense, no coloring (even with Powershell 5 installed on my computer).
Is there any way to add intellisense and coloring to my terminal, or use ISE's terminal in VSCode? or is there a plugin somewhere adressing these issues?
The integrated PowerShell terminal on Visual Studio Code does have intellisense, so I'm unclear what you require here, are you sure it's not set to cmd.exe instead ?
On another note, follow this setup guide. In case you haven't already installed the PowerShell Extension.

Using "Visual Studio code" with interactive Powershell window like Powershell ISE

I'm new to Visual Studio code so thanks in advance for the help.
I often use the PowerShell ISE with an interactive PowerShell window. I open it, type a few commands, then run a script (such as opening a PowerShell session with Office 365) and then type a few more commands.
Is there any way to do the same thing with Visual Studio code? I see a way to run scripts and debug them but not have them complete but the PowerShell window still stay around so I can continue working.
Decided to convert to answer, since this is a bit too much for comments:
Go to settings > user settings and change "terminal.integrated.shell.windows" to:
"C:\\WINDOWS\\Sysnative\\WindowsPowerShell\\v1.0\\powershell.exe",
Ctrl + Shift + ` opens the terminal. Using the + button on the terminal window you can launch additional powershell instances.
Also there's an extension that allows for launching several different terminals under the same VSCode process (cmd,powershell,bash,etc). Minor nuance, shells are being started with the same permissions VSCode has, but you can re-elevate once inside the shell.
References:
https://code.visualstudio.com/docs/editor/integrated-terminal
https://code.visualstudio.com/docs/customization/userandworkspace
https://github.com/Microsoft/vscode/issues/10893
Edit: but I would say its not really convenient, terminal takes away too much space on the screen (well, unless you have like 2k or 4k display), so I use powershell + vscode.