1

Short version: my company is enforcing a coding guideline rule that prevents code from being submitted that has trailing whitespace characters.

Is there a plugin for Visual Studio that removes trailing whitespaces whenever a file is saved?

Long version:

There are many good reasons to have trailing whitespace removed from source code:

  • it takes more storage space than necessary
  • when you hit the End key in some editors, the cursor does not go to the expected place
  • everyone in with desktop publishing background hates duplicate spaces and trailing whitespace because it makes text break weird
  • the compiler needs more time to compile
  • some editors might apply a line wrap due to the additional character when it reaches the character limit
  • it breaks vim
  • some programming languages like Python may fail to compile with whitespace in multiline strings
  • TCL fails to compile with whitespace
  • it may mess up the diff

Although some of the reasons could be avoided by technical means, I want to respect the coding guidelines. But still, I'm reluctant to scanning for whitespace with my eyes. It's so ... invisible.

The plugin must

  • work in VS 2015 and later
  • remove irrelevant trailing whitespace
  • keep potentially relevant whitespace (e.g. in multiline strings)

Ideally (optional, but preferred):

  • highlights irrelevant and potentially relevant whitespace in the editor while typing
  • Python nit: Anything goes in a multiline string. But trailing whitespace after an explicit line continuation character will cause a problem. – John Y Sep 26 '17 at 23:32
  • I don't mean any disrespect, but are you sure VS doesn't come with a setting for this? It's a very common feature in editors large and small. The "display whitespace" option is also pretty darn common. Though I will say that the autotrim feature is virtually always (if not always) implemented without awareness of whether the whitespace is logically relevant. It just goes by physical lines. – John Y Sep 26 '17 at 23:39
1

Well, it's still hard for me to believe any serious programmer's editor, let alone a full-featured IDE, is lacking this feature, but if you really need it, there is Trailing Whitespace Visualizer, which allows you to highlight whitespace and/or trim trailing whitespace upon saving (or on demand). It says it works with VS 2013, 2015, and 2017.

I cannot be sure this will preserve logically significant whitespace. But I don't know of any whitespace trimmer that checks this (it would be relatively complicated and dependent on the programming language). In my experience, there isn't much code in the wild which requires trailing whitespace to be preserved, even in multiline strings. (If they are multiline, they are typically meant just to be displayed on a screen, where whitespace either doesn't matter, or is automatically removed anyway. If precise whitespace control is required, alter your code to use multiple concatenated single-line strings.)

Older questions, but also worth checking out for other options:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.