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
- function Delete-TrailingBlanks
- {
- $editor = $psISE.CurrentFile.Editor
- $caretLine = $editor.CaretLine
- # First trial. Works.
- # $newText = @()
- # foreach ( $line in $editor.Text.Split("`n") )
- # {
- # $newText += $line -replace ("\s+$", "")
- # }
- # $editor.Text = [String]::Join("`n", $newText)
- # 2nd trial, but deletes empty lines too \s matches `r and `n
- # $editor.Text = $editor.Text -replace '(?m)\s*$', ''
- # 3rd working again, but doesn't it look like perl ?
- # $editor.Text = $editor.Text -replace '(?m)[ \t]+(?:\r?)$', ''
- # the solution is so simple
- $editor.Text = $editor.Text -replace '(?m)\s*?$', ''
- $editor.SetCaretPosition($caretLine, 1)
- }
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.
PowerShell Code Repository