Tag Archives: Code Formatting

Removing trailing whitespace in Visual Studio

A pet peeve of mine is whitespace (spaces and tabs) at the end of lines in source code.  I used the SourceInsight editor when I worked at Microsoft which has a built-in feature (among other terrific features) to strip trailing whitespace when you save.  Visual Studio has plenty of other features that I can’t live without, so it was time to figure out how to add it there.

A decent solution is to add a subroutine to the EnvironmentEvents Module.  The articles I read made the assumption that anyone reading it would know where to find that.  I didn’t, so it took me some extra time to implement the feature.

Find EnvironmentEvents Module

The EnvironmentEvents Module is a macro module that is part of your Visual Studio environment and is accessible regardless of which project is loaded.  To edit that module, you need to bring up the Macros IDE, which can be launched at Tools » Macros » Macros IDE, or by pressing Alt+F11.

Macros IDE

In the Macros IDE you will see two entries in the Project Explorer (unless you’ve been here before and have been adding macros on your own):  MyMacros and Samples.  Double-click on MyMacros.  This will show you three entries:  References, EnvironmentEvents, and Module1.

Macros IDE Project Explorer - EnvironmentEvents

Double click on EnvironmentEvents.  You will now see the current contents of the EnvironmentEvents module.

Default EnvironmentEvents

DocumentEvents_DocumentSaved Subroutine

Now we come to the meat of the solution.  The idea is to implement the DocumentEvents_DocumentSaved subroutine to remove trailing whitespace.  This subroutine is called whenever a request is made to save a document.

Private Sub DocumentEvents_Documents(ByVal document As EnvDTE.Document) _
                                     Handles DocumentEvents.DocumentSaved
    Try
        ' Remove all the trailing whitespaces.
        If vsFindResult.vsFindResultReplaced = _
            DTE.Find.FindReplace( _
                            vsFindAction.vsFindActionReplaceAll, _
                            “{:b}+$”, _
                            vsFindOptions.vsFindOptionsRegularExpression, _
                            String.Empty, _
                            vsFindTarget.vsFindTargetFiles, _
                            document.FullName, , _
                            vsFindResultsLocation.vsFindResultsNone) Then
            document.Save()
        End If
    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.OkOnly, “Trim Whitespace exception”)
    End Try
End Sub

Once you’ve saved this, it will be immediately available to Visual Studio, whether it is open or not.

This subroutine searches for all instances of trailing whitespace (spaces and tabs) and replaces it with the empty string.  It works when you save an individual file and when you do a Save All.  If an error occurs in the FindReplace method, a message box will be displayed.

Thanks to Michael Urman, who posted this solution on StackOverflow.com (May 19 response).

Resources