How to tell emacs to open .h file in C++ mode?


Emacs Problem Overview

What lines should I add to my _emacs (on Windows) file to have it open .h files in C++ mode? The default is C mode.

Emacs Solutions

Solution 1 - Emacs

Try this:

(add-to-list 'auto-mode-alist '("\\.h\\'" . c++-mode))

Whenever you open .h files, C++-mode will be used.

Solution 2 - Emacs

Another approach for using both c-mode and c++-mode as appropriate, is to use">directory local variables to set the mode.

Directory variables are evaluated after the mode has been set1, so you can actually write a .dir-locals.el file for your C++ project containing this:

((c-mode . ((mode . c++))))

And Emacs will change the mode to c++-mode whenever it had initially set it to c-mode.

If you work with a mix of C and C++ projects, this makes for a pretty trivial solution on a per-project basis.

Of course, if the majority of your projects are C++, you might set c++-mode as the default2, and you could then use this approach in reverse to switch to c-mode where appropriate.

1 normal-mode calls (set-auto-mode) and (hack-local-variables) in that order. See also:

2 To do so, add

(add-to-list 'auto-mode-alist '("\\.h\\'" . c++-mode))

to your .emacs file which open .h files in C++ mode by default.

Solution 3 - Emacs

If you don't want this to apply to every .h file, you can add the following to the bottom of your C++ header files.

// Local Variables:
// mode: c++
// End:

This will work for any Emacs variables that you want to set on a per file basis. Emacs ignores the leading characters, so use whatever comment characters are appropriate for the file type.

Solution 4 - Emacs

Apparently you can also put this at the top of the file:

// -*-c++-*-

to tell Emacs it's a C++ file.

I use this since I quite frequently end up on a vanilla Emacs and it works without configuring Emacs in any way.

Solution 5 - Emacs

Since I use both C and C++ regularly, I wrote this function to try and "guess" whether a .h file is meant to be C or C++

;; function decides whether .h file is C or C++ header, sets C++ by
;; default because there's more chance of there being a .h without a
;; .cc than a .h without a .c (ie. for C++ template files)
(defun c-c++-header ()
  "sets either c-mode or c++-mode, whichever is appropriate for
  (let ((c-file (concat (substring (buffer-file-name) 0 -1) "c")))
    (if (file-exists-p c-file)
(add-to-list 'auto-mode-alist '("\\.h\\'" . c-c++-header))

And if that doesn't work I set a key to toggle between C and C++ modes

;; and if that doesn't work, a function to toggle between c-mode and
;; c++-mode
(defun c-c++-toggle ()
  "toggles between c-mode and c++-mode"
  (cond ((string= major-mode "c-mode")
        ((string= major-mode "c++-mode")

It's not perfect, there might be a better heuristic for deciding whether a header is C or C++ but it works for me.

Solution 6 - Emacs

I could swear I saw this question answered appropriately already? Weird.

You want this:

(add-to-list 'auto-mode-alist '("\\.h\\'" . c++-mode))


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkujawkView Question on Stackoverflow
Solution 1 - EmacsfunctionalView Answer on Stackoverflow
Solution 2 - EmacsphilsView Answer on Stackoverflow
Solution 3 - EmacsKeithBView Answer on Stackoverflow
Solution 4 - EmacsProf. FalkenView Answer on Stackoverflow
Solution 5 - EmacsBorbusView Answer on Stackoverflow
Solution 6 - EmacsphilsView Answer on Stackoverflow