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?
EDIT: 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.
I see no reason for this question to be downvoted. Would someone be so kind as to explain their problems with this question?
CodePudding user response:
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
CodePudding user response:
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 topython
.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.
-
- 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 theOptions
tab of the console window'sProperties
dialog; pastes actual tabs.
- Works, with both Ctrl-V and right-click, as long as
Windows Terminal:
- Works, with right-click only(!); pastes actual tabs.
Visual Studio Code's integrated terminal:
- Works.