I don't know much about Emacs, but after some googling, I edited my .emacs
file to be as follows:
(setq c-default-style "bsd" c-basic-offset 4)
My goal was to get Allman-style indenting with 4-spaced tabs. It works as expected, but now my //
comments aren't indented with my code. Before I changed this, when I would type //
, it would get auto-indented to be in line with the rest of the code in the function. How can I get Emacs to auto-indent //
comments?
I've tried adding c-indent-comments-syntactically-p 1
to the above .emacs
file, but that didn't change it...
For example:
int main()
{
// I'd like this line to be auto-indented to match the block
for (int i = 0; i < 10; ++i)
{
// And this line to be auto-indented to match the block
doStuff();
}
}
Currently, TAB
does not indent my //
comment, and it doesn't automatically indent either.
Start by pressing the CTRL + U key, followed by the number of lines to comment out. As you can see from the screenshot above, Emacs comments out 10 lines from the current cursor position.
use Ctrl + ] to indent them.
To delete just the indentation of a line, go to the beginning of the line and use M-\ ( delete-horizontal-space ), which deletes all spaces and tabs around the cursor. If you have a fill prefix, M-^ deletes the fill prefix if it appears after the newline that is deleted.
You can inspect and change the value of the current indent by placing point on the concerned line and pressing C-c C-o
. Adjust the relevant symbols to your liking.
This wont be permanent. Use direct invocation of the function c-set-offset
in your .emacs
to make the changes globally.
Simple example:
int main() {
//
}
This is my default indent. After moving the cursor to line 2 I see that the relevant symbol is comment-intro
.
Using:
(c-set-offset 'comment-intro 6)
I get:
int main() {
//
}
Offset accumulates across symbols:
int main() {
//
{
//
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With