visual-studio 如何在 Visual Studio 2008 中自动删除尾随空格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/82971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to automatically remove trailing whitespace in Visual Studio 2008?
提问by ChrisN
Is it possible to configure Visual Studio 2008 to automatically remove whitespace characters at the end of each line when saving a file? There doesn't seem to be a built-in option, so are there any extensions available to do this?
是否可以将 Visual Studio 2008 配置为在保存文件时自动删除每行末尾的空白字符?似乎没有内置选项,那么是否有任何扩展可以做到这一点?
采纳答案by arserbin3
CodeMaid is a very popular Visual Studio extension and does this automatically along with other useful cleanups.
CodeMaid 是一个非常流行的 Visual Studio 扩展,它会与其他有用的清理程序一起自动执行此操作。
- Download: https://github.com/codecadwallader/codemaid/releases/tag/v0.4.3
- Modern Download: https://marketplace.visualstudio.com/items?itemName=SteveCadwallader.CodeMaid
- Documentation: http://www.codemaid.net/documentation/#cleaning
- 下载:https: //github.com/codecadwallader/codemaid/releases/tag/v0.4.3
- 现代下载:https: //marketplace.visualstudio.com/items?itemName=SteveCadwallader.CodeMaid
- 文档:http: //www.codemaid.net/documentation/#cleaning
I set it to clean up a file on save, which I believe is the default.
我将它设置为在保存时清理文件,我认为这是默认设置。
回答by Greg Ogle
Find/Replacing using Regular Expressions
使用正则表达式查找/替换
In the Find and Replace dialog, expand Find Options, check Use, choose Regular expressions
在“查找和替换”对话框中,展开“查找选项”,选中“使用”,选择“正则表达式”
Find What: ":Zs#$"
查找内容:“ :Zs#$”
Replace with: ""
替换为:“”
click Replace All
点击全部替换
In other editors (a normalRegular Expression parser) ":Zs#$" would be "\s*$".
在其他编辑器(普通的正则表达式解析器)中,“ :Zs#$” 将是“ \s*$”。
回答by arserbin3
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.
将以下内容添加到您的宏的 EnvironmentEvents 模块中。
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.
我已经使用了一段时间了,没有任何问题。我没有创建宏,而是根据 ace_guidelines.vsmacros 中的宏对其进行了修改,该宏可以通过快速谷歌搜索找到。
回答by Vyrotek
Before saving you may be able to use the auto-format shortcut CTRL+K+D.
在保存之前,您可以使用自动格式化快捷方式CTRL+ K+ D。
回答by iPixel
You can do this easily with these three actions:
您可以通过以下三个操作轻松完成此操作:
Ctrl+ A(select all text)
Edit -> Advanced -> Delete Horizontal Whitespace
Edit -> Advanced -> Format Selection
Ctrl+ A(选择所有文本)
编辑 -> 高级 -> 删除水平空白
编辑 -> 高级 -> 格式选择
Wait a few seconds and done.
等待几秒钟就完成了。
It's Ctrl+ Z'able in case something went wrong.
它是Ctrl+ Z' 万一出现问题。
回答by ChrisN
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.)
从已经给出的所有答案中获取元素,这是我最终得到的代码。(我主要编写 C++ 代码,但很容易根据需要检查不同的文件扩展名。)
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
回答by Jorge Ferreira
You can use a macro like described in Removing whitespace and rewriting comments, using regex searches
您可以使用像Removing whitespace and rewriting comments, using regex search 中描述的宏
回答by Evgenii
I am using VWD2010 Express where macros are not supported, unfortunately. So I just do copy/paste into Notepad++top left menu Edit> Blank Operations> Trim Trailing Spacethere are other related operations available too. Then copy/paste back into Visual Studio.
不幸的是,我正在使用不支持宏的VWD2010 Express。所以我只是复制/粘贴到Notepad++左上角菜单Edit> Blank Operations>Trim Trailing Space还有其他可用的相关操作。然后复制/粘贴回 Visual Studio。
One can also use NetBeansinstead of Notepad++, which has "Remove trailing spaces" under the "Source" menu.
还可以使用NetBeans代替 Notepad++,后者在“源”菜单下具有“删除尾随空格”。
回答by pwhe23
I personally love the Trailing Whitespace VisualizerVisual Studio extension which has support back through Visual Studio 2012.
我个人喜欢Trailing Whitespace VisualizerVisual Studio 扩展,它通过 Visual Studio 2012 获得支持。
回答by Kevin Conner
Unless this is a one-person project, don't do it. It's got to be trivial to diff your local files against your source code repository, and clearing whitespace would change lines you don't need to change. I totally understand; I love to get my whitespace all uniform – but this is something you should give up for the sake of cleaner collaboration.
除非这是一个人的项目,否则不要这样做。将您的本地文件与您的源代码存储库进行比较非常简单,并且清除空格会更改您不需要更改的行。我完全明白;我喜欢让我的空白都统一——但为了更清晰的协作,你应该放弃这一点。

