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.
6 Answers
Try this:
(add-to-list 'auto-mode-alist '("\\.h\\'" . c++-mode))
Whenever you open .h files, C++-mode will be used.
-
this gives me error "Symbol's value as variable is void: \.h\" – vk-code Apr 28 '19 at 14:53
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: How can I access directory-local variables in my major mode hooks?
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.
-
3This is the most useful answer. Also, be aware `mode` is a special form in file-local variable lists and is only documented [here](http://www.gnu.org/software/emacs/manual/html_node/emacs/Specifying-File-Variables.html). – scry Mar 17 '15 at 02:48
-
I've added the footnote (2) so your answer is self-sufficient and doesn't depend on others. Now, it should be the accepted answer. Thx. – YSC Apr 04 '17 at 08:50
-
This solution causes c++-mode to be used also for .glsl files. Is there any way to force glsl-mode to be used, instead? – jco May 08 '17 at 08:30
-
-
This solution is great! However, header files that are loaded this way do *not* apply directory local values set via `(c++-mode .` (at least in Emacs 25), so you will have to apply these values via `(nil .`. – mzuther Apr 28 '20 at 16:55
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.
-
-
Did not know that worked in ever file. This could be very useful. Thanks :) – Plankalkül Aug 25 '15 at 08:11
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.

- 24,226
- 19
- 100
- 173
-
4Or also as: `// -*- mode: c++ -*-`. You can also add other variables in there such as `-*- coding: utf-8; mode: python; tab-width: 4; -*-` – donkopotamus Mar 08 '16 at 23:29
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
header"
(interactive)
(let ((c-file (concat (substring (buffer-file-name) 0 -1) "c")))
(if (file-exists-p c-file)
(c-mode)
(c++-mode))))
(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"
(interactive)
(cond ((string= major-mode "c-mode")
(c++-mode))
((string= major-mode "c++-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.

- 593
- 4
- 10
-
A better solution might be to encode the mode in the file (see my answer), and then define keys for adding the appropriate lines to the file. The only drawback is that people who don't use emacs will see this as well, but since its at the bottom of the file it shouldn't be much of an issue. – KeithB Jul 30 '10 at 18:42
-
1That's fine for your own projects but my solution is mainly for dealing with other people's projects. You could `cat` your local variables on to the end of the headers in a 3rd party project but this is way too much effort IMO. – Borbus Aug 03 '10 at 18:47
-
I actually like this solution a lot, it makes it easy to customize behaviour without modifying any source files. For instance, in my version I use things like `(string-match "llvm" (buffer-file-name))` to determine if a .h file is c++ or not (llvm is a c++ project). – zdav Oct 02 '14 at 13:21
-
I am like 7 years late, but I think a better approach to determine if the .h is in c++ is to scan the buffer for words like class, namespace, public, private etc.. Also to avoid labeling C headers as c++ just cause they happen to have a variable named class etc, you can check if those words are at the very start of the line (after indentation ofc). – The Gramm May 09 '17 at 10:50
I could swear I saw this question answered appropriately already? Weird.
You want this:
(add-to-list 'auto-mode-alist '("\\.h\\'" . c++-mode))

- 71,335
- 11
- 153
- 198