3

I was wondering if it's possible to use tabs for indenting my C# code in Visual Studio 2013, but save the file with all tabs converted into spaces automatically. I know this can be changed in settings and then autoindenting used to fix it to the right one, but this isn't automatic.

The reason behind this is that I am currently working in a group where spaces are preffered way of indenting code, but this setup isn't convenient for me - having to click backspace 4 times after an exceeding tab (or undoing - which is almost the same inconvenience, albeit not that much) is quite annoying.

I don't want to interfere with my group's setup (nor could I, actually), but would like an easier way to traverse my code locally. We use Git for project sharing, so maybe if this cannot be made in VS maybe Git can do it?


Note: I searched Stack and Google, but couldn't find adequate answer due to arguments over which indentation technique is better. This post is not supposted to start another discussion about this either.

Asunez
  • 2,327
  • 1
  • 23
  • 46
  • You can optionally use http://editorconfig.org/ to make sure your settings are saved per project/file – vc 74 Aug 25 '15 at 13:17
  • 1
    @Asunez You can use `Shift + Tab` to delete to the previous tab indent, regardless of whether it is a four-space tab or an actual tab-character tab. – Der Kommissar Aug 25 '15 at 13:18
  • @EBrown I know, yet this isn't as convenient as backspacing once. Also traversing through code with spaces isn't solved with your advice. – Asunez Aug 25 '15 at 13:19
  • Why would you want to traverse through the code with spaces or tabs, rather than just skip to the end either way? – Jon Hanna Aug 25 '15 at 13:20
  • @Asunez I fail to see where the issue is. There are hotkeys and button groups specifically for this sort of thing. I.e.: `CTRL + ARROW` traverses word-by-word through code, `TAB` adds a tab indent, `SHIFT + TAB` takes one away. The nice thing about `SHIFT + TAB` is that if you select more than one line, it takes an indentation level away from **all** the selected lines. With `TAB`, if you select multiple lines, it adds an indent level to **all** the selected lines. Using `BACKSPACE` will not allow you to do this, as you can only use it one line at a time. – Der Kommissar Aug 25 '15 at 13:22
  • @JonHanna Say that I have multiple lines with spaces at the beginning, and many nested indentations. When using tabs to move left and right I would have to click arrow keys _n_ times, but with spaces this would require _n*4_ times (4 spaces per tab, since that's the settings my group uses). – Asunez Aug 25 '15 at 13:22
  • @EBrown Okay, I didn't know about the `CTRL + ARROW` shortcut, this might be something I could use. – Asunez Aug 25 '15 at 13:24
  • Also, the `Home` button will alternate (on the same line) from the begginning of the line to the beginning of the text on that line. – crashmstr Aug 25 '15 at 13:25
  • And `end` button will get you to end of line. – Waters Aug 25 '15 at 13:32
  • @crashmstr, Waters, EBrown: Although all your advices help (and I am grateful for them, will definetely try them out), they are not the answer to my question. I just want to use the code as if it was indented with Tab, but save it with Space. – Asunez Aug 25 '15 at 13:52
  • 1
    For VS 2015 there is [Tab Sanity](https://visualstudiogallery.msdn.microsoft.com/ac4d4d6b-b017-4a42-8f72-55f0ffe850d7) – qujck Aug 26 '15 at 08:06
  • @qujck Really good to know, thank you! Will definetely give it a try at my home rig. – Asunez Aug 26 '15 at 11:29

2 Answers2

1

Since your primary issue deals with formatting concerns, I would recommend using Format Document (Ctrl+K,Ctrl+D or Edit>Advanced>Format Document or Format Selection) to fix this.

Its fast and will correct indentation for the whole file/section at once, and you don't have to worry about altering the IDE save behaviors.

If you are trying to keep everything well organized as-you-type-it, you may not be using the provided tools efficiently.

StingyJack
  • 19,041
  • 10
  • 63
  • 122
  • I'm aware of the CTRL+K, CTRL+D combination, and this would be it, only if it upon opening would run with my settings with Tabs, then on exit/save with Spaces. Your answer would fit perfectly in, only if it were automatically applied. – Asunez Aug 25 '15 at 13:56
  • 3
    I think you are being overaggressive with "tabs vs spaces" and not considering the more efficient work approach. Microsoft's paradigm for text editing over the last decade has been "write first, format later" and is present in pretty much every product - office, visual studio, etc. You should strongly consider using that approach instead of being concerned with tabs vs spaces. – StingyJack Aug 25 '15 at 14:06
  • Finally a comment that instead of telling me to do the opposite, explains why should I do so. If using the approach of "write first, format later", how can I set the VS to use Tabs for indentation, but then (preferably with a shortcut like CTRL+K, D) format my file to use spaces? Should you help me with this, the green tick of gratitude is yours. – Asunez Aug 25 '15 at 14:14
  • Aside from writing a VS plugin and linking into the save (like this - https://visualstudiogallery.msdn.microsoft.com/3ea1c920-69c4-441f-9979-ccc2752dac56), you cannot do what you want using the IDE as shipped. – StingyJack Aug 25 '15 at 14:18
1

Since you're using Git, you could try checking out tabs and converting to spaces on checkin. This might cause issues of its own, but it might also solve your problem. This question should tell you how to do that if you're interested. It deals with Python, but I imagine it would do the same for C# just fine if you replace .py with .cs. Here's the accepted answer for completeness:

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 -- **

Although I too prefer tabs, I also suggest finding out how to use your tools effectively with the project's style. Maybe you could just find a way to make backspace delete sets of 4 spaces when found?

Community
  • 1
  • 1
31eee384
  • 2,748
  • 2
  • 18
  • 27
  • This might be the answer I'm looking for. I will have to test it out on a sandbox set, but it seems very promising though. Should it work as I expect it to, I will mark your answer as accepted. – Asunez Aug 25 '15 at 14:49