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

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:
File > New (Untitled1.ps1 opens)
Press tab (all fine, you get an indent)
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?
System:
Windows 8.1 Pro
PowerShell ISE

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:
Ctrl-D to go to the Command Pane
Type a command
If the command did what I wanted, Ctrl-I to go to the Script Pane
Type #<Tab>, and the line is added to the script.

Related

Powershell 7.3.0 Tab completion not working

I've recently upgraded PowerShell to version 7.3.0 version and now, when I type a command, I see its suggestions like when I'm typing pip it adds list like in this image. Or when I type start of the command it suggests its full name.
The problem is that when I press Tab it doesn't complete the command, instead it just starts listing current directories, i.e. here is an image after pressing Tab once.
Also even when I start typing the full name of the command like pip li it still shows the ending, but when pressing Tab it just does nothing.
I expected this to complete the current command with the suggestion after Tab is pressed.
I've tried to google this problem but haven't found the exact same case I have with 7.3.0 version.
Just press -> (right arrow) key
If you want to change key bindings:
source: https://devblogs.microsoft.com/powershell/announcing-psreadline-2-1-with-predictive-intellisense/
Key Bindings for Predictions
Key bindings control cursor movement and additional features within the prediction. To support users running Predictive IntelliSense on multiple platforms, key bindings are user-settable from the command line or your profile script.
PSReadLine contains functions to navigate and accept predictions. As an example, to accept a displayed prediction, PSReadLine contains functions:
AcceptSuggestion – Accept the current inline suggestion
AcceptNextSuggestionWord – Accept the next word of the inline suggestion
AcceptSuggestion is built within ForwardChar, which by default is bound to RightArrow. Pressing RightArrow accepts an inline suggestion when the cursor is at the end of the current line.
AcceptNextSuggestionWord is built within the function ForwardWord, which can be bound with Ctrl+f by Set-PSReadLineKeyHandler -Chord "Ctrl+f" -Function ForwardWord. Pressing Ctrl+f accepts the next word of an inline suggestion when the cursor is at the end of current editing line.
As a user, you can bound other keys to AcceptSuggestion and AcceptNextSuggestionWord for similar functionalities. Search for ForwardCharAndAcceptNextSuggestionWord in SamplePSReadLineProfile.ps1 for an example to make RightArrow accept the next word from inline suggestion, instead of the whole suggestion line.
List of additional suggested key bindings defined in PSReadLine SamplePSReadLineProfile.ps1

Powershell equivalent of MATLAB keyboard() command for debugging?

MATLAB has a feature to allow users to introduce a command in the script/function called keyboard and have the code stop there with a console/command prompt that gives direct access to the interpreter and the variable space. MATLAB keyboard() command docs
Is there something similar in Powershell? I don't want to deal with Powershell ISE as I don't like it's syntax highlighting (doesn't highlight all occurrences of selected phrase like Notepad++ does) and the macros is a lot weaker than what the plain Powershell console itself (like lacking F8 for recalling).
Stepping with a debugger is not quite as useful when you need to explore complex object structures and try a few things out with the current variable states, instead of just reading what the variables and values are in a docked window. It's much easier to work with the state of mind of the interpreted language at any point by interacting with it instead of ploughing through stack and trace information.
In both Powershell ISE and VSCode, you can read or update the same variables in your session from the terminal window at the bottom, even during debugging.

Is it possible to take control of sublimetext3 tabs with cmd

Good Day.
I am a hobby programmer, and am just looking for creative ways to get things done.
In particular, closing a tab or tabs in sublimetext3 through a cmd line method.
The even cooler thing would be, if so, is there a way to decide in cmd which tab to close e.g. 3rd from left/right etc?
In keeping with the idea that this is mostly curiosity and preference than anything else, so creative replies are definitely appreciated as well.
You can execute any command* from the command line, so this is very much possible.
For example, to close the active tab of the most recently used Sublime Text window, you can run subl --command close.
To close a specific tab, for example the 18th tab, which will have index 17 because it is zero-based:
subl --command 'close_by_index {"group": 0, "index": 17}'
(This is assuming bash syntax - for Windows cmd syntax, you may need to do some creative escaping because the command arguments have to be valid JSON and quoted as part of the same argument as the name of the command you want to execute.)
This is exactly the same command that would be run when right-clicking on the tab and picking Close Tab in the context menu. This command can be seen by inspecting Packages/Default/Tab Context.sublime-menu using the built-in View Package File functionality in the Command Palette. You can check this file to see other pre-defined entries like closing all other tabs, or tabs to the right etc.
*: Caveats being if ST is not already running, then it tries to run the command before plugins are loaded.

Double angle bracket mode

I'm using PowerShell 7.1.3 and have noticed a strange behavior when entering a single quote to my prompt:
PS: '
>>
>>
The prompt is styled with >> and no matter what I type, nothing gets executed (or that's how it looks). I was wondering:
Is this activated by entering ' on the shell, or is this a side effect?
Is this mode called somehow?
Is this mode useful for a specific case?
Is this activated by entering ' on the shell, or is this a side effect?
It's an intentional side-effect of entering an unmatched ' as a command, yes.
It's technically not PowerShell itself, but rather the PSReadLine module (which ships with PowerShell 7 and is enabled by default).
PSReadLine provides a number of command line editing improvements - including syntax highlighting, customizable tab- and menu-completion, and multi-line editing - and it works by continously feeding your (partial) input to PowerShell's parser, and then uses the results returned by the parser to provide the correct syntax highlighting etc.
So when you press [Enter] after having typed a single ', PSReadLine sends your input to the parser like this:
# Parse the input script the same way PSReadLine would
$ParserErrors = #()
$AST = [System.Management.Automation.Language.Parser]::ParseInput("'", [ref]$null, [ref]$ParserErrors)
# Let's inspect any parser errors
$ParserErrors
If you run this in PowerShell 7.1.3, you'll see the following errors were reported:
Extent ErrorId Message IncompleteInput
------ ------- ------- ---------------
' TerminatorExpectedAtEndOfString The string is missing the terminator: '. True
Notice the last property of the error, IsInputIncomplete - PSReadLine sees that what you've typed in the prompt is incomplete - and it therefore assumes you're not done writing your command and continues the prompt on the next line for you.
[...] no matter what I type, nothing gets executed
That's not quite true - if you enter something that completes the input (eg. makes it valid PowerShell again), it'll let you submit it - in your case that would be by typing a closing ':
Is this mode called somehow?
No idea, I tend to think of >> as a "prompt continuation indicator", but it's not really a distinct mode per se.
Is this mode useful for a specific case?
Well, as shown above it prevents you from trying to execute syntactically invalid or incomplete code, that's pretty useful in itself I think.
The overall ability to do multi-line editing at the prompt is, in my personal opinion, one of the biggest strengths of PowerShell - you can start prototyping small functions and compose quite complex pipelines without having to switch back and forth between an editor.
As beatcracker mentions, you can also get prompt continuation (at least in the default Windows Console Host) by pressing Shift+Enter - this works regardless of whether the existing input is incomplete or not.
You can basically write and edit a whole script at the prompt if you wanted to.

How to hide "PS C:\>" in powershell?

So you know how in powershell if you enter a command infront of it there will always be "PS (directory)>".
I know its possible in cmd with a simple command but how do you do this in powershell?
so:
Hide the text(PS C:>) thats infront of commands
If possible through a command
If any of you know the fix please let me know.
If i find it first i will self anwser.
(I later found this dup):Windows PowerShell: changing the command prompt
Just like Calculuswhiz mentioned, the following should work:
Function Prompt {" "}
Simply change it by adding anything else inside the double quotes.
Abraham Zinala's helpful answer shows that it is the prompt function - described in the about_Prompts conceptual help topic - that determines the string that PowerShell prints in interactive sessions when prompting the user for commands.
As Calculuswhiz notes, outputting the empty string ('') is not an option for not printing a prompt string at all, because PowerShell then defaults to PS>.
Printing a single space is one way to work around the problem, but that space does print. To prevent that, Dabombber proposes workarounds in a comment on Abraham's question, but the simplest solution as of Windows 10 is:
function prompt { "`0" } # Fully HIDES the prompt string.
"`0" outputs a NUL (null character), which effectively hides the prompt string, without side effects, including in Windows Terminal and ConEmu.[1]
On Windows 7, the NUL prints as a space, in which case Dabombber's alternative helps:
# A space followed by a backspace char. in effect amounts to the empty string.
function prompt { " `b" }
Add this function to your $PROFILE file to hide the prompt string in all future sessions.
[1] Verified on version v20.11.24.0. Dabombber mentions that ConEmu prints NUL characters as a space, but, given that ConEmu is conhost.exe-based, just like regular console windows, that applies only up to Windows 7.