Search code examples
c#visual-studiovisual-studio-2013

Use tabs for indentation but save with spaces in Visual Studio


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.


Solution

  • 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?