windows 如何在 PowerShell 中键入 TAB 字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20691068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 11:12:02  来源:igfitidea点击:

How do I type a TAB character in PowerShell?

windowspowershellcommand-prompt

提问by Hamman Samuel

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.

任务:默认情况下,在 Windows 命令提示符下按 TAB 键将输出文件名,而在 PowerShell 中它什么也不做。我希望能够以交互模式而不是通过脚本键入 TAB 字符。

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.

研究:我在这个网站上和通过谷歌搜索发现了类似的问题。找到的解决方案是解决bash(Mac) 或iterm(Linux),或建议更改为另一个程序,例如TweakUI。我的问题特定于 Windows PowerShell 或命令提示符。

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. Hope that helps, and thanks everyone for taking the time to look at the question.

澄清:一个简单的测试你的答案是否适用于我的问题是输入echo "1 TAB-method 2"PS/CP,其中 TAB 方法是你关于如何插入 TAB 字符的建议。如果回声给出1 2(即 1 后跟 TAB 空格后跟 2),这就是我要找的。希望有所帮助,并感谢大家花时间看问题。

回答by Shay Levy

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

如果有帮助,您可以在双引号字符串中嵌入制表符:

PS> "`t hello"

回答by tommymaynard

Test with [char]9, such as:

用[char]9测试,如:

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

Output:

输出:

     hello

回答by zdan

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

在 Windows 命令提示符中,您可以通过启动它来禁用选项卡完成:

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.exehelp page explains it:

cmd.exe帮助页面解释它:

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.

您可以使用 /F:ON 或 /F:OFF 开关为 CMD.EXE 的特定调用启用或禁用文件名完成。通过使用 REGEDIT.EXE 在注册表中设置以下任一或两个 REG_DWORD 值,您可以为计算机和/或用户登录会话上的所有 CMD.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

带有用于特定功能的控制字符的十六进制值(例如 0x4 是 Ctrl-D,0x6 是 Ctrl-F)。用户特定设置优先于机器设置。命令行开关优先于注册表设置。

如果使用 /F:ON 开关启用补全,则使用的两个控制字符是 Ctrl-D 用于目录名补全,Ctrl-F 用于文件名补全。要禁用注册表中的特定完成字符,请使用空格 (0x20) 值,因为它不是有效的控制字符。

回答by Walter Mitty

TAB 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).

TAB 在 PowerShell 中具有特定含义。它用于命令完成。因此,如果您输入“getch”,然后键入 TAB。它会更改您在“GetChildItem”中输入的内容(它会纠正这种情况,即使这是不必要的)。

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.

从您的问题来看,TAB 完成和命令完成似乎会使 TAB 键过载。我很确定 PowerShell 设计人员不希望那样。