2008-12-23

quickly switching between header and implementation

A trivial yet useful trick: when coding C/C++, you often jump from header file (.h, .hh etc.) to implementation file (.c, .cc etc.). Emacs has built-in support for this, using ff-find-other-file. We can add a key binding to .emacs:
(add-hook 'c-mode-common-hook
  (lambda() 
    (local-set-key  (kbd "C-c o") 'ff-find-other-file)))
Now, we can quickly switch between myfile.cc and myfile.h with C-c o. Note the use of the c-mode-common-hook, so it will work for both C and C++.

5 comments:

Zung! said...

Cool!

But shouldn't this be:

(add-hook 'c-mode-common-hook (lambda() (local-set-key (kbd "C-c o") 'ff-find-other-file)))

djcb said...

You are very right Zung!, updated, thanks. I had copied it out-of-context.

Anonymous said...

Nice post. Just to make it clear, c-mode-common-hook goes beyond just C and C++, and also applies to all major modes with roots in CC mode (C, C++, Java, Objective C, etc.). You can read it here

Alex Ott said...

For this task I use eassist-switch-h-cpp function from the eassist package, included into cedet.

djcb said...

@rodrigo: that's true of course; but I'm not sure what ff-find-other-file would do for these other languages.

@Alex: does that have any advantages? Cedet does scare me a bit...