Preprocessor directives indentation in Visual C++ 2010
Asked Answered
O

4

22

I often find myself in situations where I would like to indent preprocessor directives like the rest of the code (e.g. #if indented like if). It seems legal, it's common sense that it's sometimes a good thing, but Visual won't make it easy.

Is there a way to prevent the Visual C++ 2010 editor from sticking the code to the left each time I innocently press #? And to not break preprocessor directives indentation with auto-indent (CTRL+K, CTRL+F)? Or even better, to handle preprocessor directives like everything else?

Oboe answered 4/9, 2012 at 16:21 Comment(1)
For this reason I started using an external editor for editing heavy preprocessor code...Bujumbura
G
14

My approach is to keep the # in the first column and indent the subsequent word, as in:

#ifdef FIRST
#  include "first.h"
#else
#  include "second.h"
#endif
Gaucho answered 4/9, 2012 at 16:29 Comment(4)
Except you won't be able to select multiple lines and (Shift+)Tab them.Bujumbura
+1 for good preprocessor style. Boost uses this style and it's much clearer which lines are preprocessor directives while maintaining branching indentationPikestaff
+1 as it's indeed a good, often seen, workaround. But I still feel like proper indentation support would make me happier. So I'll wait and see if I get other answers.Oboe
Looks like that's the best solution until Microsoft decides to add the option.Oboe
C
12

At some point visualstudio (checked in vs2015) acquired options > text editor > c/c++ > formatting > indentation > position of preprocessor directives. Choose 'leave indented'. The sample doesn't look exactly like what you want, but it works, just try it.

Caller answered 3/5, 2016 at 16:43 Comment(2)
It looks like this setting was added in VS2015.Habit
This is clearly what the accepted answer should now be. :) Although it would be nice if the [Ctrl]+[K] [Ctrl]+[F] used this. :(Verlaverlee
M
10

In the Visual Studio 2010 options (Tools->Options)

Go to Text Editor -> C/C++ -> Tabs

Under indenting select Block instead of smart.

This will prevent the # key from forcing you to the start of the line, however if you use Ctrl+K Ctrl+F it will still apply smart formatting and remove the tabs.

Edit: This will also disable automatic indenting/unindenting in other places. Beware.

Moran answered 12/9, 2012 at 9:45 Comment(0)
N
3

Workaround: When you first type the # and Visual Studio removes your indentation, press ctrl+z to undo the auto formatting.

Nutt answered 28/11, 2015 at 18:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.