Skip to content

Frequently Asked Questions

  • How can I change the indent level from 4 spaces to 2 spaces?

    Set the variable c-basic-offset. See Getting Started.

  • Why does/doesn’t the RET key indent the new line?

    Emacs’s convention used to be that RET just adds a newline, and that C-j adds a newline and indents it. In Emacs-24.4, this convention was reversed.

    If you use an older Emacs and you want RET do this too, add this to your c-initialization-hook:

    emacs-lisp
    (define-key c-mode-base-map "\C-m" 'c-context-line-break)

    See Getting Started. This was a very common question.

  • How do I get block comments in my C++ files?

    Interactively, change the comment style with C-c C-k. See Minor Modes.

    To configure this setting, say, for files within the gdb project, you could amend your C++ Mode hook like this:

    emacs-lisp
    (defun my-c++-mode-hook ()
      (if (string-match "/gdb/" (buffer-file-name))
          (c-toggle-comment-style 1)))
    (add-hook 'c++-mode-hook 'my-c++-mode-hook)
  • How do I stop my C++ lambda expressions being indented way over to the right?

    This is now the default, so you don’t need to do anything. To restore the previous default, indenting lambda expressions to the right of the constructs which introduce them, change the offset associated with inlambda from 0 to c-lineup-inexpr-block. For example, if you are setting offsets in a hook function you might include the following line:

    emacs-lisp
    (c-set-offset 'inlambda 'c-lineup-inexpr-block)

    For details of the different ways you can make this setting, Configuration Basics.

  • How do I stop my code jumping all over the place when I type?

    Deactivate “electric minor mode” with C-c C-l. See Getting Started.

  • How do I reindent the whole file?

    Visit the file and hit C-x h to mark the whole buffer. Then hit C-M-\. See Indentation Commands.

  • How do I reindent the current block?

    First move to the brace which opens the block with C-M-u, then reindent that expression with C-M-q. See Indentation Commands.

  • I put (c-set-offset 'substatement-open 0) in my .emacs file but I get an error saying that c-set-offset’s function definition is void. What’s wrong?

    This means that CC Mode hasn’t yet been loaded into your Emacs session by the time the c-set-offset call is reached, most likely because CC Mode is being autoloaded. Instead of putting the c-set-offset line in your top-level .emacs file, put it in your c-initialization-hook (see Hooks), or simply modify c-offsets-alist directly:

    emacs-lisp
    (setq c-offsets-alist '((substatement-open . 0)))
  • I have an open paren character at column zero inside a comment or multiline string literal, and it causes the fontification and/or indentation to go haywire. What gives?

    It’s due to the ad-hoc rule in (X)Emacs that such open parens always start defuns (which translates to functions, classes, namespaces or any other top-level block constructs in the CC Mode languages). See Left Margin Paren in GNU Emacs Manual, for details (See Defuns in GNU Emacs Manual, in the Emacs 20 manual).

    This heuristic is built into the core syntax analysis routines in (X)Emacs, so it’s not really a CC Mode issue. However, in Emacs 21.1 it became possible to turn it off[1] and CC Mode does so there since it’s got its own system to keep track of blocks.


  1. Using the variable open-paren-in-column-0-is-defun-start. ↩︎