Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get Emacs to indent my // comments with my code?

Tags:

c++

c

emacs

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.

like image 855
Cornstalks Avatar asked Nov 26 '12 21:11

Cornstalks


People also ask

How do I comment multiple lines in Emacs?

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.

How do you indent a group of codes?

use Ctrl + ] to indent them.

How do I remove an indentation in Emacs?

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.


1 Answers

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() {
        //
  {
          //
  }
}
like image 62
pmr Avatar answered Nov 12 '22 03:11

pmr