Search code examples
phpemacssyntaxindentation

How to make cases in switch statement to be indented in Emacs


How to make Emacs to indent cases like this

switch ($foo) {
    case "foo":
        $foo .= " bar";
        break
    case "bar":
        $foo .= " baz";
        break
    default:
        $foo .= " undefined";
}

instead of

switch ($foo) {
case "foo":
    $foo .= " bar";
    break
case "bar":
    $foo .= " baz";
    break
default:
    $foo .= " undefined";
}

Solution

  • You need to add something like this to your .emacs (either as a general setting or for the specific programming modes you care about):

    ;; set this in all c-based programming modes
    (add-hook 'c-mode-common-hook
              (lambda ()
                 (c-set-offset 'case-label '+)))
    

    to add this to another mode, use the same pattern above with the relevant mode name substituted in for the hook, e.g.: <mode-name>-hook.