-3

In PowerShell at the command line, how do I enter a tab indentation for multiple line commands?

enter image description here

Obviously, [tab] or [shift] + [tab] does not work, otherwise I would not ask this question.

Kellen Stuart
  • 7,775
  • 7
  • 59
  • 82

1 Answers1

2

Using PSReadline (built-in to PS 5.1 or available via Install-Module) you can make a custom key handler:

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, "    ")
}

Then Ctrl+Tab will indent the line which the cursor is on, no matter where in the line the cursor is.

Extending this to multiple lines, when you can't really select multiple lines in the console, is left as an exercise for the reader.

TessellatingHeckler
  • 27,511
  • 4
  • 48
  • 87