When I add a member initialization list to my class constructor, Emacs indents the body like this:

Vector::Vector(int s) : elem{new double[s]}, sz{s}


I expect it to be indented the same as when there is no member initialization list:

Vector::Vector(int s)


How can I accomplish this? Should I file a bug?

2 Answers 2


What indentation style do you have configured?

(see http://www.emacswiki.org/emacs/IndentingC)

I have style = "bsd" and emacs doesn't do that.

  • I'm using the default (gnu). I tried (setq c-default-style "bsd") but it's still misbehaving. Emacs version is 24.5. Nov 21, 2015 at 20:25
  • does it still indent like this when you start with emacs -Q ? Nov 30, 2015 at 12:12
  • I could be wrong, but I think this was an Emacs 24.5 issue in cc-mode that has been fixed in 25.1. Note that using initialization lists to initalize variables (curly-braces) is C++11 construct, only recently supported in Emacs.
    – Xaldew
    Feb 22, 2016 at 10:08
(setq c-default-style "linux")

You could tweak indentation of individaul c-syntactic-context by manipulating the variable c-offsets-alist. Read wiki and documentation. Usually that's enough.

More advanced method is reading the original Lisp code if Emacs itself has issue. Here is step by step guide http://blog.binchen.org/posts/ccjava-code-indentation-in-emacs.html

I strongly suggesting reading the article to understand why at first. Then insert below code into your ~/.emacs if ad only if changing c-default-style does NOT solve the issue:

(defun fix-c-indent-offset-according-to-syntax-context (key val)
  ;; remove the old element
  (setq c-offsets-alist (delq (assoc key c-offsets-alist) c-offsets-alist))
  ;; new value
  (add-to-list 'c-offsets-alist '(key . val)))

(add-hook 'c-mode-common-hook
          (lambda ()
            (when (derived-mode-p 'c-mode 'c++-mode 'java-mode)
              ;; indent
              (fix-c-indent-offset-according-to-syntax-context 'func-decl-cont 0))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.