PoshCode Logo PowerShell Code Repository

Del. TrailingBlank (ISE) (modification of post by view diff)
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1626"></script>download | new post

This function is intended to be uses as ISE add on.
When working with ISE multiline texts take care of correct `r`n handling.
Finding the regular expression was a bit tricky.
Compare the different versions for yourself

  1. function Delete-TrailingBlanks
  2. {
  3.     $editor = $psISE.CurrentFile.Editor
  4.     $caretLine = $editor.CaretLine
  5.  
  6. #     First trial. Works.  
  7. #     $newText = @()
  8. #     foreach ( $line in $editor.Text.Split("`n") )
  9. #     {
  10. #         $newText += $line -replace ("\s+$", "")
  11. #     }
  12. #     $editor.Text = [String]::Join("`n", $newText)
  13.  
  14. #    2nd trial, but deletes empty lines too  \s matches `r and `n    
  15. #    $editor.Text = $editor.Text -replace '(?m)\s*$', ''
  16.  
  17. #    3rd working again, but doesn't it look like perl ?
  18. #    $editor.Text = $editor.Text -replace  '(?m)[ \t]+(?:\r?)$', ''
  19.  
  20. #  the solution is so simple  
  21.     $editor.Text = $editor.Text -replace '(?m)\s*?$', ''
  22.  
  23.        
  24.     $editor.SetCaretPosition($caretLine, 1)
  25. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me