【问题标题】:PowerShell / PowerShell ISE - How to enter a tab indentation in the command window?PowerShell / PowerShell ISE - 如何在命令窗口中输入制表符缩进?
【发布时间】:2018-04-18 17:17:46
【问题描述】:

在 PowerShell 的命令行中,如何为多行命令输入制表符缩进?

显然[tab][shift] + [tab]是不行的,否则我不会问这个问题。

【问题讨论】:

  • 您是否尝试过使用空格字符?
  • @lit 我想这会起作用,但 tab 更好,对吧?
  • 如果你只缩进 1,那么 SPACE 与 TAB 没有太大区别。如果需要这么多脚本,为什么不在编辑器窗口中启动一个新脚本呢?
  • 此外,缩进在 PowerShell 中不像在 Python 中那样计算在内。
  • 呃,命令行不是编辑器

标签: powershell command-line format powershell-ise


【解决方案1】:

使用 PSReadline(内置于 PS 5.1 或通过 Install-Module 获得)您可以制作自定义密钥处理程序:

Set-PSReadlineKeyHandler -Chord 'ctrl+tab' -ScriptBlock {
    $text = ''
    $cursor = 0
    [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$text, [ref]$cursor)
    $lastNewLine = [math]::max(0, $text.LastIndexOf("`n", $cursor - 1))
    [Microsoft.PowerShell.PSConsoleReadLine]::Replace([math]::min($cursor, $lastNewLine + 1), 0, "    ")
}

然后Ctrl+Tab 将缩进光标所在的行,不管光标在哪一行。

当您无法在控制台中真正选择多行时,将其扩展到多行,留给读者作为练习。

【讨论】:

    猜你喜欢
    • 2014-09-04
    • 2015-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-08
    • 1970-01-01
    • 1970-01-01
    • 2012-01-10
    相关资源
    最近更新 更多