c# - 使用制表符进行缩进,但在 Visual Studio 中使用空格保存

标签 c# visual-studio visual-studio-2013

我想知道是否可以在 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/

相关文章:

c# - ASP.NET MVC - 操作名称如何影响 url?

visual-studio - Visual Studio 2015 Community Edition 有 MVC 模板吗?

c# - 通过 visual studio 并行测试执行,测试被跳过

c++ - 如何更改静态链接库中 const 字符串数组的 Visual Studio C++ 初始化顺序

c# - 启动 Visual Studio 2008 时不会加载 ReSharper

azure - 连接到 Microsoft Azure 订阅失败

c# - azure "The remote server returned an error: (530) Not logged in."

C#并行写入Azure Data Lake File

c# - 如何在 C# 中将字符串转换为位

c++ - 在涉及 <cmath> 操作的简单基准测试中,AVX 比 IA32 慢 3.6 倍 - 为什么会这样? (VS2013)