Add ability to add tabs to the end of a line in Windows PowerShell ISE
In the preamble of an earlier post I mentioned that one of the little things that bugs me about Windows PowerShell ISE is that you can add tabs to the start of a line, but not to the end of a line. This is likely because it would interfere with the tab-completion feature. I still like to be able to put tabs on the end of my code lines though so that I can easily line up my comments, like this:
$processes = Get-Process # Get all of the processes.
$myProcesses = $processes | Where {$_.Company -eq "MyCompany" } # Get my company's processes.
We can add the functionality to allow us to insert a tab at the end of a line, but it involves modifying the PowerShell ISE profile, so opening that file for editing is the first step.
To edit your PowerShell ISE profile:
- Open Windows PowerShell ISE (not Windows PowerShell, as we want to edit the ISE profile instead of the regular PowerShell profile).
- In the Command window type:
psedit $profile
- If you get an error that it cannot find the path, then first type the following to create the file before trying #2 again:
New-Item $profile -ItemType File -Force
- If you get an error that it cannot find the path, then first type the following to create the file before trying #2 again:
And now that you have your PowerShell ISE profile file open for editing, you can append the following code to it:
# Add a new option in the Add-ons menu to insert a tab.
if (!($psISE.CurrentPowerShellTab.AddOnsMenu.Submenus | Where-Object { $_.DisplayName -eq "Insert Tab" }))
{
$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Insert Tab",{$psISE.CurrentFile.Editor.InsertText("`t")},"Ctrl+Shift+T")
}
This will allow you to use Ctrl+Shift+T to insert a tab anywhere in the editor, including at the end of a line. I wanted to use Shift+Tab, but apparently that shortcut is already used by the editor somewhere, even though it doesn’t seem to do anything when I press it. Feel free to change the keyboard shortcut to something else if you like.
I hope this helps make your PowerShell ISE experience a little better.
Happy coding!
Comments
Tony
Just fyi, Shift-Tab reverses through the tab completion list (goes backwards after pressing Tab first. eg, Type “Get-“ then press Tab a few times. Now press Shift-Tab and the auto-completion will back up to the previous option each time you press it.
It’s useful when you Tab too quick and go past the option you wanted.
Leave a Comment
Your email address will not be published. Required fields are marked *