Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I trim trailing whitespace in Visual Studio 2012 on save?

Now that macros are gone from Visual Studio 2012, what is the best way to automatically trim trailing whitespace whenever a file is saved?

I'm aware that Format Document (Ctrl + K, Ctrl + D) will do this, but it presumably changes the file in other ways, and it's not automatic either.

The motivation is given here:

Trailing whitespace is evil. Don't commit evil into your repo

like image 452
Hemmer Avatar asked Aug 25 '12 14:08

Hemmer


People also ask

How do you get rid of leading and trailing white spaces?

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.

Does trim remove leading and trailing spaces?

TRIM function - remove extra spaces in Excel You use the TRIM function in Excel removes extra spaces from text. It deletes all leading, trailing and in-between spaces except for a single space character between words.


2 Answers

There are at least two extensions that can do this. One is CodeMaid which explicitly will trim trailing blanks on save, and the other is Productivity Power Tools which can run the Format Document automatically on save.

To add an extension from within Visual Studio 2012, select menu ToolsExtensions and Updates.... In the window, select Online on the left and enter your search string for CodeMaid or Productivity Power Tools in the box on the upper right.

like image 109
Bill A Avatar answered Sep 22 '22 09:09

Bill A


Ctrl + H
Find what:
[\u0020]+\r?\n
Replace with:
\r\n
\s instead of [\u0020] delete empty line feeds, probably because it matches line feeds...

like image 29
groch Avatar answered Sep 21 '22 09:09

groch