It is actually ALT + SHIFT + F in the latest versions.
To remove leading and trailing spaces in Java, use the trim() method. This method returns a copy of this string with leading and trailing white space removed, or this string if it has no leading or trailing white space.
To do this, make sure the Home tab is active on the ribbon and click the “Show/Hide ¶“ button in the Paragraph section. Spaces are shown as dots. Select the lines that contain spaces you want to delete and center the lines by pressing Ctrl+E, or clicking the Center button in the Paragraph section of the Home tab.
Find/Replacing using Regular Expressions
In the Find and Replace dialog, expand Find Options, check Use, choose Regular expressions
Find What: ":Zs#$
"
Replace with: ""
click Replace All
In other editors (a normal Regular Expression parser) ":Zs#$
" would be "\s*$
".
CodeMaid is a very popular Visual Studio extension and does this automatically along with other useful cleanups.
I set it to clean up a file on save, which I believe is the default.
You can create a macro that executes after a save to do this for you.
Add the following into the EnvironmentEvents Module for your macros.
Private saved As Boolean = False
Private Sub DocumentEvents_DocumentSaved(ByVal document As EnvDTE.Document) _
Handles DocumentEvents.DocumentSaved
If Not saved Then
Try
DTE.Find.FindReplace(vsFindAction.vsFindActionReplaceAll, _
"\t", _
vsFindOptions.vsFindOptionsRegularExpression, _
" ", _
vsFindTarget.vsFindTargetCurrentDocument, , , _
vsFindResultsLocation.vsFindResultsNone)
' Remove all the trailing whitespaces.
DTE.Find.FindReplace(vsFindAction.vsFindActionReplaceAll, _
":Zs+$", _
vsFindOptions.vsFindOptionsRegularExpression, _
String.Empty, _
vsFindTarget.vsFindTargetCurrentDocument, , , _
vsFindResultsLocation.vsFindResultsNone)
saved = True
document.Save()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.OkOnly, "Trim White Space exception")
End Try
Else
saved = False
End If
End Sub
I've been using this for some time now without any problems. I didn't create the macro, but modified it from the one in ace_guidelines.vsmacros which can be found with a quick google search.
Before saving you may be able to use the auto-format shortcut CTRL+K+D.
You can do this easily with these three actions:
Ctrl + A (select all text)
Edit -> Advanced -> Delete Horizontal Whitespace
Edit -> Advanced -> Format Selection
Wait a few seconds and done.
It's Ctrl + Z'able in case something went wrong.
Taking elements from all the answers already given, here's the code I ended up with. (I mainly write C++ code, but it's easy to check for different file extensions, as needed.)
Thanks to everyone who contributed!
Private Sub DocumentEvents_DocumentSaved(ByVal document As EnvDTE.Document) _
Handles DocumentEvents.DocumentSaved
Dim fileName As String
Dim result As vsFindResult
Try
fileName = document.Name.ToLower()
If fileName.EndsWith(".cs") _
Or fileName.EndsWith(".cpp") _
Or fileName.EndsWith(".c") _
Or fileName.EndsWith(".h") Then
' Remove trailing whitespace
result = DTE.Find.FindReplace( _
vsFindAction.vsFindActionReplaceAll, _
"{:b}+$", _
vsFindOptions.vsFindOptionsRegularExpression, _
String.Empty, _
vsFindTarget.vsFindTargetFiles, _
document.FullName, _
"", _
vsFindResultsLocation.vsFindResultsNone)
If result = vsFindResult.vsFindResultReplaced Then
' Triggers DocumentEvents_DocumentSaved event again
document.Save()
End If
End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.OkOnly, "Trim White Space exception")
End Try
End Sub
I personally love the Trailing Whitespace Visualizer Visual Studio extension which has support back through Visual Studio 2012.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With