5

I'm not entirely sure if this question belongs on stackoverflow or superuser (is there an emacs stack exchange?). Based on the meta.stackoverflow post I'll assume that it does.

My emacs defaults header files (of the .h variety) to c mode. I can easily type M-x c++-mode and get my highlighting back, but because I program more often in c++ than c. I was wondering what I needed to change to add .h to the c++ group.

JSchlather
  • 1,564
  • 2
  • 13
  • 22

2 Answers2

8

Here's what I have in my .emacs file:

; Make .h files be C++ mode
(setq auto-mode-alist(cons '("\\.h$"   . c++-mode)  auto-mode-alist))

There might be an easier way, but this works.

Ron Romero
  • 9,211
  • 8
  • 43
  • 64
  • That's the best way to do it. – Omnifarious Aug 31 '10 at 06:09
  • 1
    Although this wouldn't actually be a problem unless you had filenames containing a newline, it is that much more robust (and consistent with the rest of `auto-mode-alist`) to use `\\'` instead of `$` to denote the end of the filename in the pattern. – phils Aug 31 '10 at 09:53
  • 1
    For that matter, you can say (setq auto-mode-alist (cons `(,(rx ".h" eos) . c++-mode) auto-mode-alist)) "rx" is a handy way to specify regular expressions using s-expressions, rather than hard-to-read strings. – offby1 Aug 31 '10 at 15:56
3

Well, here is what a lot of people do:

 // -*-c++-*-

That line at the top of your header tells emacs its C++.

I like that better than just calling all .h files C++ because some aren't. Though I have to admit, though it pains me because its ugly, I have taken to calling my header files .hpp. :-/

Omnifarious
  • 54,333
  • 19
  • 131
  • 194