6

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

akauppi
  • 17,018
  • 15
  • 95
  • 120
  • 1
    I can reproduce this. And the TAB completion is even working in the comments! – Alireza Jul 14 '14 at 11:42
  • But only if I type a blank after the # – Alireza Jul 14 '14 at 11:44
  • 1
    Thanks. So is it a bug..? I have no idea why anyone would want that. PowerShell is very annoying to get started, anyways. This is simply one more little rock more in the boot. – akauppi Jul 14 '14 at 11:45
  • I consider it a bug. And I think they have forgotten that auto-complete is not needed in this context. Another funny thing is the TAB's behaviour. It isn't context-aware and just brings (On my PC) the User profile's subfolders! – Alireza Jul 14 '14 at 11:48
  • You can always copy/paste that from notepad. Imagine you are typing a serious comment and need a TAB :D – Alireza Jul 14 '14 at 11:52
  • 1
    @Alireza I don't think it's a bug. As mentioned in my answer, I find it useful behavior. – Rynant Jul 14 '14 at 14:12
  • 1
    It shouldn't matter anyway as PSISE replaces the tab with 4 spaces (or just enough spaces to reach the nearest `Column%4 -eq 0` or multiple of 4). If you need a tab, use spaces. Or enter the tab first, then add the #. Or paste the Tab as has been suggested. – dwarfsoft Jul 15 '14 at 01:14

1 Answers1

6

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.
akauppi
  • 17,018
  • 15
  • 95
  • 120
Rynant
  • 23,153
  • 5
  • 57
  • 71
  • 2
    Thanks, great comment that sheds light to how it works the way it does. Also, I had not learned the syntax for multi-line comments, yet. Makes sense, in a bizarre way. – akauppi Jul 14 '14 at 14:35
  • When there is no autocomplete candidate, TAB still doesnt work as TAB inside inline comments or block comments. Seems to behave correctly outside of the comments.. – StingyJack Dec 24 '17 at 13:54