Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to type a tab (indent) in comments in PowerShell ISE?

This is weird.

I know tab is for command completion in the PowerShell ISE and fine so. But, it also messes up the editing pane.

Do this:

  1. File > New (Untitled1.ps1 opens)

  2. Press tab (all fine, you get an indent)

  3. type enter, # (comment) and press tab after it expected: one would get indentation after the hash actual: one gets the hash replaced by $PSVersionTable or whatever the command prompt has in its history! (tab and Shift-tab circle through those)

Does this mean no-one uses tabs within comments in PowerShell scripts, or that no-one uses comments in PowerShell scripts?

Can I turn off this behavior anywhere?

Also, the behavior seems to be inconsistent. If I e.g. type ##, sometimes tab does not do the completion (it does not enter a tab either).

Can others reproduce this?

screenshot

System: Windows 8.1 Pro PowerShell ISE

like image 545
akauppi Avatar asked Jul 14 '14 11:07

akauppi


1 Answers

To answer the main question, you can enter Alt+09 (using numeric keypad) to enter <Tab>.

For the behavior described, I see this as expected behavior. You can get history completion by typing # and part of a previous command then pressing Tab repeatedly will cycle backwards through matching history. Typing # alone will match all history starting with the last command.

 Does this mean no-one uses tabs within comments in PowerShell scripts?

Anecdotal, but I've never used tabs in a single line comment, but I do often use tabs within multiline comments which are bracketed by <# and #>. E.g.

<#
Functions
    Get-Foo
    Get-Bar

Variables
    $Foo
    $Bar
#>

Function Get-Foo { ...

With multiline comments, the auto-completion will not be an issue.

, or that no-one uses comments in PowerShell scripts?

I don't know why this would be implied by the behavior; I always use a single space to start a line comment.

I find this helpful when developing a script as I often try expressions in the command pane if I'm unsure of the behavior, then add the expression to the script if it works.

So, my workflow would be:

  1. Ctrl-D to go to the Command Pane
  2. Type a command
  3. If the command did what I wanted, Ctrl-I to go to the Script Pane
  4. Type #<Tab>, and the line is added to the script.
like image 145
Rynant Avatar answered Nov 14 '22 22:11

Rynant