Get-World | ConvertTo-PowerShell
ISE: Add new line below/above current
Today I was playing with PowerShell v3 and was frequently working inside ISE (love it’s Intellisense feature). I was in this situation few times:

As you can see, cursor is at position 0. At this time I want to add new line, so it means go to end of current line and press Enter. I wanted to automate it. Fortunately ISE object model is self-descriptive, so I created this small Add-on:
$psise.CurrentPowerShellTab.AddOnsMenu.Submenus.Add(
'Insert line below',
{
$psise.CurrentFile.Editor.Select(
$psise.CurrentFile.Editor.CaretLine,
$psise.CurrentFile.Editor.GetLineLength($psise.CurrentFile.Editor.CaretLine)+1,
$psise.CurrentFile.Editor.CaretLine,
$psise.CurrentFile.Editor.GetLineLength($psise.CurrentFile.Editor.CaretLine)+1)
$psise.CurrentFile.Editor.InsertText("`n")
}, "CTRL+ENTER")
$psise.CurrentPowerShellTab.AddOnsMenu.Submenus.Add(
'Shift line',
{
$psise.CurrentFile.Editor.Select(
$psise.CurrentFile.Editor.CaretLine, 1,
$psise.CurrentFile.Editor.CaretLine, 1)
$psise.CurrentFile.Editor.InsertText("`n")
$psise.CurrentFile.Editor.Select(
$psise.CurrentFile.Editor.CaretLine-1, 1,
$psise.CurrentFile.Editor.CaretLine-1, 1)
}, "CTRL+SHIFT+ENTER")
I added two features: add line below and before (shift line). You can call it either from menu:

or via keyboard shortcuts.
| Print article | This entry was posted by makovec on 28/12/2011 at 17:18, and is filed under PowerShell. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |

about 4 months ago
Great tip!
I added it to my $profile like this.
function InsertLineBelow {
$editor = $psise.CurrentFile.Editor
$currentLine = $editor.CaretLine
$length = $editor.GetLineLength($currentLine) + 1
$editor.Select($currentLine,$length,$currentLine,$length)
$editor.InsertText(“`n”)
}
$psise.CurrentPowerShellTab.AddOnsMenu.Submenus.Add(‘Insert line below’,{InsertLineBelow},”CTRL+ENTER”)