ChatGPT解决这个技术问题 Extra ChatGPT

How do I type a TAB character in PowerShell?

Task: By default, pressing the TAB key while in Windows Command Prompt will output file names, while it does nothing in PowerShell. I want to be able to type the TAB character in interactive mode, not via scripts.

Research: I found similar questions on this site and via Google search. Solutions found were addressing Bash (Mac) or iterm (Linux), or suggested changing to another program such as TweakUI. My question is specific to Windows PowerShell or Command Prompt.

Clarification: A simple test for whether your answer works for my question is to type echo "1 TAB-method 2" into PS/CP, where TAB-method is your suggestion on how to insert a TAB character. If the echo gives 1 2 (i.e., 1 followed by a TAB space followed by 2). That's what I'm looking for.

It's not clear what you want to happen when you press tab and in what context. If you just want filename completion as per cmd, it should already do that.
Thanks for the clarification, I would like to insert the TAB character into the command prompt screen when I press TAB. For example, let's say I am running a program called file.exe and it expects an input, e.g. file.exe instring. I want instring as TAB when I invoke the program. Does that illustration help?

P
Peter Mortensen

If it helps, you can embed a tab character in a double-quoted string:

"`t hello"

Awesome, this works for PS! Now if only we can find a solution for Command Prompt too...
Note: `t gets treated literally as two characters inside single quotes. Just in case you're wondering: single quotes mean literal.
t
tommymaynard

Test with [char]9, such as:

$Tab = [char]9
Write-Output "$Tab hello"

Output:

     hello

Super - much prefer this approach
The example above works just fine in PowerShell ISE too.
P
Peter Mortensen

In the Windows command prompt you can disable tab completion, by launching it thusly:

cmd.exe /f:off

Then the tab character will be echoed to the screen and work as you expect. Or you can disable the tab completion character, or modify what character is used for tab completion by modifying the registry.

The cmd.exe help page explains it:

You can enable or disable file name completion for a particular invocation of CMD.EXE with the /F:ON or /F:OFF switch. You can enable or disable completion for all invocations of CMD.EXE on a machine and/or user logon session by setting either or both of the following REG_DWORD values in the registry using REGEDIT.EXE: HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\CompletionChar HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\PathCompletionChar and/or HKEY_CURRENT_USER\Software\Microsoft\Command Processor\CompletionChar HKEY_CURRENT_USER\Software\Microsoft\Command Processor\PathCompletionChar with the hex value of a control character to use for a particular function (e.g. 0x4 is Ctrl-D and 0x6 is Ctrl-F). The user specific settings take precedence over the machine settings. The command line switches take precedence over the registry settings. If completion is enabled with the /F:ON switch, the two control characters used are Ctrl-D for directory name completion and Ctrl-F for file name completion. To disable a particular completion character in the registry, use the value for space (0x20) as it is not a valid control character.


Thanks for the answer, this works without needing to change the registry. I was able to launch with the /f:off parameter and that disabled file name completion.
P
Peter Mortensen

The TAB key has a specific meaning in PowerShell. It's for command completion. So if you enter "getch" and then type a TAB, it changes what you typed into "GetChildItem" (it corrects the case, even though that's unnecessary).

From your question, it looks like TAB completion and command completion would overload the TAB key. I'm pretty sure the PowerShell designers didn't want that.


That's true, I'm sure there's a reason the designers need the separation. However, I need to type a TAB character into PS, and as the other links suggest, bash and iterm do have modes that allow inserting TAB as a character. I'm hoping there's a similar way in PS, such as CTRL + V + TAB?
PSReadline (github.com/lzybkr/PSReadline) sort of supports this right now - you would use Ctrl+I which is equivalent to Tab but inserts that tab instead of completing. At some point I'll fix that to behave just like tab, but I'll also add Ctrl+Q support like bash so you'd type Ctrl+Q,Tab to insert a tab.