我想知道是否可以在 Visual Studio 2013 中使用制表符来缩进我的 C# 代码,但保存文件时所有制表符都会自动转换为空格。我知道这可以在设置中更改,然后使用自动缩进将其修复到正确的位置,但这不是自动的。
这背后的原因是我目前在一个优先使用空格来缩进代码的组中工作,但这种设置对我来说并不方便 - 必须在超过制表符后单击退格键 4 次(或撤消 - 这几乎同样的不便,尽管不是那么多)很烦人。
我不想干扰我的组的设置(实际上我也不能),但想要一种更简单的方法来在本地遍历我的代码。我们使用 Git 进行项目共享,所以如果这不能在 VS 中完成,也许 Git 可以做到?
注意:我搜索了 Stack 和 Google,但由于关于哪种缩进技术更好的争论,我找不到合适的答案。这篇文章也不应该就此展开另一次讨论。
最佳答案
由于您使用的是 Git,您可以尝试 checkout 制表符并在 checkin 时转换为空格。这可能会导致其自身出现问题,但也可能会解决您的问题。 This question should tell you how to do that if you're interested.它处理 Python,但我想如果将 .py
替换为 .cs
,它也会对 C# 执行相同的操作。这是 accepted answer为了完整性:
In your repository, add a file
.git/info/attributes
which contains:*.py filter=tabspace
Linux/Unix
Now run the commands:
git config --global filter.tabspace.smudge 'unexpand --tabs=4 --first-only' git config --global filter.tabspace.clean 'expand --tabs=4 --initial'
OS X
First install coreutils with brew:
brew install coreutils
Now run the commands:
git config --global filter.tabspace.smudge 'gunexpand --tabs=4 --first-only' git config --global filter.tabspace.clean 'gexpand --tabs=4 --initial'
All systems
You may now check out all the files of your project. You can do that with:
git checkout HEAD -- **
虽然我也更喜欢选项卡,但我也建议了解如何根据项目的风格有效地使用您的工具。也许您可以找到一种方法,让退格键在找到时删除一组 4 个空格?
关于c# - 使用制表符进行缩进,但在 Visual Studio 中使用空格保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32205020/