Customizing PowerShell ISE with Yank Line CustomMenu Item

I was excited to hear that the PowerShell ISE editor was based on the new WPF editor that will ship in Visual Studio 2010.  However I was disappointed when I couldn’t find one of my favorite shortcuts in the script editor: Ctrl+L a.k.a. yank line.  Fortunately the object model for the editor is capable enough to support this capability.  Here’s a pretty simple custom menu entry with an assigned keyboard shortcut (Ctrl+L) to perform a yank line in the editor:

   1: [void]$psISE.CustomMenu.Submenus.Add(
   2:     'Yank Line', 
   3:     {
   4:       $editor = $psise.CurrentOpenedFile.Editor
   5:       $line = $editor.CaretLine
   6:       $lineLen = $editor.GetLineLength($line)
   7:       if ($line -lt $editor.LineCount) 
   8:       {
   9:         $editor.Select($line, 1, $line + 1, 1)
  10:       }
  11:       else
  12:       {
  13:         $editor.Select($line, 1, $line, $lineLen + 1)
  14:       }
  15:       [System.Windows.Clipboard]::SetText($editor.SelectedText)
  16:       $editor.InsertText('')
  17:     }, 
  18:     'Ctrl+L'
  19: )

I find this handy for copying an entire line.  I yank the line that I want to copy to the clipboard.  Once it is yanked, I can paste it back as many times as I need.

psmdtag:ise – CustomMenu

This entry was posted in PowerShell. Bookmark the permalink.

Leave a comment