Reputation: 33668
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?
Upvotes: 22
Views: 4769
Reputation: 1667
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.
Upvotes: 12
Reputation: 394
Workaround: When you first type the #
and Visual Studio removes your indentation, press ctrl+z to undo the auto formatting.
Upvotes: 3
Reputation: 1158
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.
Upvotes: 10
Reputation: 18218
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
Upvotes: 14