0

How can I configure my ~/.emacs so as to get the following effect? In C++ mode, I want C++ comments (but not C-style comments), to always be unindented, i.e., the double slashes should always be first in a line, not preceded by whitespace. By default, the Emacs C++ mode will indent C++ comments in the same way as it would any single-line statement. I.e., I want code to look like:

int main() {
        /* x is the number of foos */
        int x = 1; 
//      int x = 2;
        if (x == y) {
                 /* a is the number of bars */
                 int a = x;
//               int a = 2 * x;
        }
}

Note that C-style comments should still be indented normally.

Background: I have a C++ codebase in which all "normal" comments are in C-style, and C++ comments are only used to comment out code. This means that I can search for the string "//" within the cleanup to remove all such comments before I commit my code to SVN. When commenting out code using C++ comments, the default behavior of Emacs makes it harder to see the old (commented-out) and new code side by side. I.e., Emacs would give this:

int main() {
        /* x is the number of foos */
        int x = 1; 
        //      int x = 2;
        if (x == y) { 
                 /* a is the number of bars */
                 int a = x;
                 //               int a = 2 * x;
        }
}

Note also that C++ comments that follow code on a line should not be affected.

  • What commands do you use currently that result in the comments appearing as in the second listing? It's really hard to answer this without knowing how you're adding the comments. – Qudit May 22 '15 at 0:42
  • When I add // to the beginning of a line, the line gets automatically indented. I.e., the line is a normal non-comment line, I put the cursor at the beginning of that line (column 0), and enter //. Then the line gets automatically indented. – Jérôme Kunegis May 27 '15 at 12:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.