使空格像 Visual Studio 中的选项卡一样工作的技巧

使空格像 Visual Studio 中的选项卡一样工作的技巧

问题描述:

在工作中,我们约定使用 4 个空格进行代码缩进.我习惯使用 tabs 进行缩进,但想遵守约定.

At work we have the convention on using 4 spaces for code indentation. I'm accustomed to using tabs for indentation, but want to follow the convention.

注意:我不打算在这里开始讨论空格与制表符.

Note: it is not my intention to start a discussion on spaces vs tabs here.

我调整了我的 Visual Studio 设置以用 4 个空格替换制表符,但我在调整使用空格时遇到了一些问题.

I adjusted my Visual Studio settings to replaces tabs with 4 spaces, but I have some issues adjusting to using spaces.

例如:

  • 如何轻松取消缩进代码?使用 tab 字符,我只需要使用 backspace 一次,有空格我需要使用 backspace4 次.

  • How can I easily un-indent code? with tab chararaters, I only needed to use backspace one time, with spaces I need to use backspace 4 times.

如何确保始终有正确的空格数(不是三个或五个)?

How can I make sure that there is always the correct amount of spaces (not three or five)?

如何使用选项卡尽可能快地浏览代码?(箭头向左或向右跳跃使用制表符移动到下一个缩进,但只移动一个带有空格的位置)

How can I navigate through my code as fast as I could with tabs? (arrow left or right jumps to the next indentation with tabs, but moves only a single position with spaces)

如何在比较文件时忽略空格更改?

How can I ignore whitespace changes when comparing files?

理想情况下,我希望这些用于缩进的 4 个空格与制表符一样有效.

Idealy, I would like these 4-spaces for indentation to work equally to tab characters.

我主要使用基于 c# 和 XML 的文件.

I work mainly with c# and XML-based files.

欢迎任何提示!

要取消缩进,突出显示该行并按 Shift+Tab.或者只是定位到行首并点击 Shift+Tab.

To un-indent, highlight the line and hit Shift+Tab. Or just position to the beginning of the line and hit Shift+Tab.

此外,点击 Tab 将输入正确数量的空格以对齐下一个 4 空格边界.

Also, hitting Tab will enter the correct number of spaces to align at the next 4-space boundary.

为确保缩进正确,您可以突出显示代码区域并选择编辑"->高级"->格式选择",或者您可以直接转到块的末尾,删除结束大括号,然后将其添加回来.IDE 将重新格式化您的代码.

To make sure indentation is correct, you can highlight an area of code and select Edit -> Advanced -> Format Selection, or you can just go to the end of a block, remove the ending brace, and add it back. The IDE will reformat your code.

所以,如果你有这个:

void foo()
{
  f();
     int q = 32;
   for (; q > 0; --q)
     {
    // really messed up indentation
   }
  }

然后删除并重新添加最终的 '}' 将重新格式化整个方法.

Then deleting and re-adding that final '}' will reformat the entire method.